[R] Legend Help
Hi to everyone and thanks for this service. I have a doubt with legend, I have seen ?legend, but I don't get the way to write in my code the legend that I want, This is my code: for( i in 1:4)} pdf(paste(plotImputed, i,.pdf,sep=)) plot(a[,6], type=l, main=paste( Imputed Data GRUPO,i) ) for(k in 3:9)(lines(a[, k], type=l, col=k)) dev.off() } Each line represent a column of my data set, they are called: IE.2004, IE.2006, IE.2007IE.2010 and this is (IE.2005, IE.2006...) what I would like to see in the graph Thanks a lot Best regards, Teresa [[alternative HTML version deleted]] __ R-help@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.
Re: [R] Legend Help
Hi put line legend(topright, legend=names(a)[3:9], lty=1, col=3:9) before dev.off() see ?legend for fine tuning. Regards Petr -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of Ma Teresa Martinez Soriano Sent: Tuesday, September 03, 2013 10:59 AM To: r-help@r-project.org Subject: [R] Legend Help Hi to everyone and thanks for this service. I have a doubt with legend, I have seen ?legend, but I don't get the way to write in my code the legend that I want, This is my code: for( i in 1:4)} pdf(paste(plotImputed, i,.pdf,sep=)) plot(a[,6], type=l, main=paste( Imputed Data GRUPO,i) ) for(k in 3:9)(lines(a[, k], type=l, col=k)) dev.off() } Each line represent a column of my data set, they are called: IE.2004, IE.2006, IE.2007IE.2010 and this is (IE.2005, IE.2006...) what I would like to see in the graph Thanks a lot Best regards, Teresa [[alternative HTML version deleted]] __ R-help@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. __ R-help@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.
Re: [R] legend help
On 05.02.2010 00:24, casperyc wrote: Yes, that is pretty much what I want. However, there was slightly a mistake. we need to use ''rate=rate[i] and shape=shape[i] because the default is == dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE) == if we use == dgamma(x, rate[i], shape[i]) == it actually takes shape=rate[i], shape[i]=rate ) Now I have another problem, I printed it out, the colorful does not seem to pretty, so i tried to use STRAIGHT lines for all of them == x- 5 * ppoints(100) rate- rep(c(2, 4), each = 3) shape- rep(c(1, 3, 5), 2) gamden- matrix(NA, nrow = 6, ncol = 100) for(i in 1:6) gamden[i, ]- dgamma(x, rate=rate[i], shape=shape[i]) matplot(x, t(gamden), type = 'l', col = 1:6, ylab = 'density') == this does not plot with straight line. i am guessing it is not controlled by lty?? It is, hence matplot(x, t(gamden), type = 'l', col = 1:6, ylab = 'density', lty=1) does the trick. Uwe Ligges now i have gone back and used this to produce the graph and sorted out. == plot(sin,xlim=c(0,4),ylim=c(0,3),type='n',ylab=density,las=1) i=1 for(rate in c(2,4) ){ for(shape in c(1,3,5) ){ curve(dgamma(x,rate=rate,shape=shape),col=i,lwd=2,add=T) i=i+1 } } rate- rep(c(2, 4), each = 3) shape- rep(c(1, 3, 5), 2) parText- function(names, pars) { p1- paste(* '= , pars[-length(pars)], , '*, sep = ) p2- paste(* '= , pars[length(pars)], ') p- c(p1, p2) txt- paste(names, p, collapse = ) parse(text = txt) } names- c('lambda', 'theta') vals- cbind(rate, shape) legnames- c(parText(names, vals[1, ]), parText(names, vals[2, ]), parText(names, vals[3, ]), parText(names, vals[4, ]), parText(names, vals[5, ]), parText(names, vals[6, ])) legend(locator(1), legend = legnames, lwd=rep(c(2),6), col = 1:6) == [[elided Hotmail spam]] Thanks! casper __ R-help@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.
Re: [R] legend help
This is very weird!! I know the 'lty=1' command and I tried yesterday , many times!! didnt work. However, today, it worked!!! Maybe the university's computer is stupid! Thanks! -- View this message in context: http://n4.nabble.com/legend-help-tp1461992p1470216.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] legend help
Yes, that is pretty much what I want. However, there was slightly a mistake. we need to use ''rate=rate[i] and shape=shape[i] because the default is == dgamma(x, shape, rate = 1, scale = 1/rate, log = FALSE) == if we use == dgamma(x, rate[i], shape[i]) == it actually takes shape=rate[i], shape[i]=rate ) Now I have another problem, I printed it out, the colorful does not seem to pretty, so i tried to use STRAIGHT lines for all of them == x - 5 * ppoints(100) rate - rep(c(2, 4), each = 3) shape - rep(c(1, 3, 5), 2) gamden - matrix(NA, nrow = 6, ncol = 100) for(i in 1:6) gamden[i, ] - dgamma(x, rate=rate[i], shape=shape[i]) matplot(x, t(gamden), type = 'l', col = 1:6, ylab = 'density') == this does not plot with straight line. i am guessing it is not controlled by lty?? now i have gone back and used this to produce the graph and sorted out. == plot(sin,xlim=c(0,4),ylim=c(0,3),type='n',ylab=density,las=1) i=1 for(rate in c(2,4) ){ for(shape in c(1,3,5) ){ curve(dgamma(x,rate=rate,shape=shape),col=i,lwd=2,add=T) i=i+1 } } rate - rep(c(2, 4), each = 3) shape - rep(c(1, 3, 5), 2) parText - function(names, pars) { p1 - paste(* '= , pars[-length(pars)], , '*, sep = ) p2 - paste(* '= , pars[length(pars)], ') p - c(p1, p2) txt - paste(names, p, collapse = ) parse(text = txt) } names - c('lambda', 'theta') vals - cbind(rate, shape) legnames - c(parText(names, vals[1, ]), parText(names, vals[2, ]), parText(names, vals[3, ]), parText(names, vals[4, ]), parText(names, vals[5, ]), parText(names, vals[6, ])) legend(locator(1), legend = legnames, lwd=rep(c(2),6), col = 1:6) == [[elided Hotmail spam]] Thanks! casper -- View this message in context: http://n4.nabble.com/legend-help-tp1461992p1469559.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
[R] legend help
i=1 for(rate in c(2,4) ){ for(shape in c(1,3,5) ){ curve(dgamma(x,rate,shape),xlim=c(0,3),ylab=,col=i,lty=i,add=T) i=i+1 } } How can I add some legend to represent these lines? i.e. the legend is displayed as col=1 lty=1 lambda=2 theta=1 col=2 lty=2 lambda=2 theta=3 col=3 lty=3 lambda=2 theta=5 col=4 lty=4 lambda=4 theta=1 col=5 lty=5 lambda=4 theta=3 col=6 lty=6 lambda=4 theta=5 i tried to use legend( locator(1), expression(lambda=i theta=i),col=i,lty=i) but did actually get it can someone help? Thanks! casper -- View this message in context: http://n4.nabble.com/legend-help-tp1461992p1461992.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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.
Re: [R] legend help
Hi: Try this, using the parText function given below: # Generate the x's, vectors of rate and shape, densities and matplot x - 5 * ppoints(100) rate - rep(c(2, 4), each = 3) shape - rep(c(1, 3, 5), 2) gamden - matrix(NA, nrow = 6, ncol = 100) for(i in 1:6) gamden[i, ] - dgamma(x, rate[i], shape[i]) matplot(x, t(gamden), type = 'l', lty = 1:6, col = 1:6, ylab = 'density') For the legend, we have this little function that takes the parameter (vector) as the first argument and its value (vector) as the second: parText - function(names, pars) { p1 - paste(* '= , pars[-length(pars)], , '*, sep = ) p2 - paste(* '= , pars[length(pars)], ') p - c(p1, p2) txt - paste(names, p, collapse = ) parse(text = txt) } We need to create a vector of names and a matrix of values to call the function: names - c('lambda', 'theta') vals - cbind(rate, shape) Now use it to create the rows of the legend: legnames - c(parText(names, vals[1, ]), parText(names, vals[2, ]), parText(names, vals[3, ]), parText(names, vals[4, ]), parText(names, vals[5, ]), parText(names, vals[6, ])) Finally, generate the legend: legend(locator(1), legend = legnames, lty = 1:6, col = 1:6) Hope this is what you wanted... Dennis On Wed, Feb 3, 2010 at 3:54 PM, casperyc caspe...@hotmail.co.uk wrote: i=1 for(rate in c(2,4) ){ for(shape in c(1,3,5) ){ curve(dgamma(x,rate,shape),xlim=c(0,3),ylab=,col=i,lty=i,add=T) i=i+1 } } How can I add some legend to represent these lines? i.e. the legend is displayed as col=1 lty=1 lambda=2 theta=1 col=2 lty=2 lambda=2 theta=3 col=3 lty=3 lambda=2 theta=5 col=4 lty=4 lambda=4 theta=1 col=5 lty=5 lambda=4 theta=3 col=6 lty=6 lambda=4 theta=5 i tried to use legend( locator(1), expression(lambda=i theta=i),col=i,lty=i) but did actually get it can someone help? Thanks! casper -- View this message in context: http://n4.nabble.com/legend-help-tp1461992p1461992.html Sent from the R help mailing list archive at Nabble.com. __ R-help@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. [[alternative HTML version deleted]] __ R-help@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.