[R] Depth vs Temp graph for different transects

Tinus Sonnekus tsonnekus at gmail.com
Mon Sep 1 12:52:17 CEST 2014

Hi All,

Have the following code. The graph works well plotting the 15 transect for
me however the legend shows a total of 22 transects. The original data has
22 transects numbered from 1 to 22. New data set got only 15. How can I get
the legend to show only the transects plotted.

# Create Line Chart

TAll <- read.csv("TAll Data.csv")

# convert factor to numeric for convenience
TAll$Tran <- as.numeric(TAll$Trans)
nTrans <- max(TAll$Trans)

# get the range for the x and y axis
xrange <- range(TAll$Temp)
yrange <- range(TAll$Depth)

# set up the plot
plot(xrange, yrange, ylim = rev(yrange), type="n", xlab="Temp (deg C)",
  ylab="Depth (m)" )
colors <- rainbow(nTrans)
linetype <- c(1:nTrans)
plotchar <- seq(1,1+nTrans,1)

# add lines
for (i in 1:nTrans) {
  tree <- subset(TAll, Trans==i)
  lines(tree$Temp, tree$Depth, type="b", lwd=1.5,
    lty=linetype[i], col=colors[i], pch=plotchar[i])

# add a legend
legend(xrange[-2], yrange[-2], 1:nTrans, cex=0.8, col=colors,
  pch=plotchar, lty=linetype, title="Transect")

Thanks for the help,

M.J. Sonnekus
PhD Candidate (The Phytoplankton of the southern Agulhas Current Large
Marine Ecosystem (ACLME))
Department of Botany
South Campus
Nelson Mandela Metropolitan University
PO Box 77000
Port Elizabeth
South Africa

Cell: 082 080 9638
E-mail: tsonnekus at gmail.com

	[[alternative HTML version deleted]]

More information about the R-help mailing list