On Sep 1, 2014, at 3:52 AM, Tinus Sonnekus wrote:
> 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")
You have set up a situation where we can only guess.
>
>
> # 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")
>
If nTrans is 22 then you are getting what you ask for. If you
subsetted a dataset where TAll$Trans was a factor then it's perfecty
possible that the legend would have more items than the subset.
Perhaps you should use `length` or length(unique(.))` rather than `max`.
--
David.
>
>
> Thanks for the help,
> Tinus
>
> --
> 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
> 6031
>
> Cell: 082 080 9638
> E-mail: tsonnekus at gmail.com
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
David Winsemius, MD
Alameda, CA, USA