Hi all, I am have some difficulty with the legend function. I need to add a legend to describe the different line types in a plot. The legend box is small. It did not include sufficient length of each line type to help distinguish the differnt line types. Is there a way to fix this. Thank you Hannah [[alternative HTML version deleted]]
Hi: It would really help to have a reproducible example to see exactly what problems you're having, but here's a simple manufactured example to illustrate how to produce a basic legend. The plot below is one with three different 'y' variables against the same x. The x and y limits are made wide enough to contain all the data with room for a legend. # Fake data: dd <- data.frame(x = 1:20, y1 = rnorm(20, 2, 1), y2 = rnorm(20, 4, 1), y3 = rnorm(20, 6, 1)) summary(dd) # check the ranges of the y-variables first plot(y1 ~ x, data = dd, type = 'l', xlim = c(0, 25), ylim = c(-1, 10)) lines(y2 ~ x, data = dd, lty = 'dotted') lines(y3 ~ x, data = dd, lty = 'dashed') lnames <- c('y1', 'y2', 'y3') legend('topright', legend = lnames, lty = c('solid', 'dotted', 'dashed')) If you want to increase the thickness of the legend lines to improve visibility, use lwd, as in legend('topleft', legend = lnames, lty = c('solid', 'dotted', 'dashed'), lty = 2) HTH, Dennis On Wed, Jul 21, 2010 at 9:06 PM, li li <hannah.hlx@gmail.com> wrote:> Hi all, > I am have some difficulty with the legend function. > I need to add a legend to describe the different line types in a plot. The > legend box is small. > It did not include sufficient length of each line type to help distinguish > the differnt line types. > Is there a way to fix this. > Thank you > Hannah > > [[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. >[[alternative HTML version deleted]]
Hi maybe less number of lines? regards Petr r-help-bounces at r-project.org napsal dne 22.07.2010 06:06:53:> Hi all, > I am have some difficulty with the legend function. > I need to add a legend to describe the different line types in a plot.The> legend box is small. > It did not include sufficient length of each line type to helpdistinguish> the differnt line types. > Is there a way to fix this. > Thank you > Hannah > > [[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 guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
On 2010-07-21 22:06, li li wrote:> Hi all, > I am have some difficulty with the legend function. > I need to add a legend to describe the different line types in a plot. The > legend box is small. > It did not include sufficient length of each line type to help distinguish > the differnt line types. > Is there a way to fix this. > Thank you > Hannah >If I understand correctly, you want to have longer line segments in your legend. I agree that this is sometimes desirable but, with the current code, it's not possible - the segment length is hard-coded. If it matters enough to you, you can easily modify the code to achieve your aim: search for the following two lines and in each replace the '2' with a greater value, say '3'. First line: w0 <- w0 + (2 + x.off) * xchar Second line: seg.len <- 2 I don't think that this will have any undesirable side effects, but I haven't given it much thought. I haven't had any problems with this version of legend(). -Peter Ehlers
On 07/22/2010 02:06 PM, li li wrote:> Hi all, > I am have some difficulty with the legend function. > I need to add a legend to describe the different line types in a plot. The > legend box is small. > It did not include sufficient length of each line type to help distinguish > the differnt line types. > Is there a way to fix this.Hi Hannah, I thought that there was some argument to specify the horizontal proportion of the legend box devoted to text, but I can't find it. Perhaps you could roll your own legend using the rect, text and line functions. wide.legend<-function(x,y=NA,legend,lty=1,lwd=1,bg=par("bg"), border=par("fg"),col=par("fg"),cex=1, xjust=0,yjust=1,text.prop=0.5,...) { if(is.na(y) && is.list(x)) { y<-unlist(x[[2]]) x<-unlist(x[[1]]) } boxwidth<-(max(strwidth(legend))+strwidth("o"))/text.prop boxheight<-strheight(paste(legend,collapse="\n"))+strheight("o") boxleft<-x-boxwidth*xjust boxbottom<-y-boxheight*yjust rect(boxleft,boxbottom,boxleft+boxwidth, boxbottom+boxheight,col=bg,border=border) text(boxleft+boxwidth*(1-text.prop), boxbottom+1:length(legend)*strheight("O"), legend,cex=cex,adj=c(0,0.5),...) hgap<-strwidth("o") segments(boxleft+hgap, boxbottom+1:length(legend)*strheight("O"), boxleft+boxwidth*(1-text.prop)-hgap, boxbottom+1:length(legend)*strheight("O"), lty=lty,lwd=lwd,col=col) } Pretty rough, but it's a start Jim
It occurs to me that Hannah may simply be plotting to a plot window that is overall too small. Try setting the plot window to full-screen (or close to that), and then running your plot & legend. Once the picture on the screen is the way you like it, save or copy (clipboard) to your format of choice, and then resize the plot image with some other tool. The line segments in the legend box should scale nicely. Carl ----- From: Peter Ehlers <ehlers_at_ucalgary.ca> Date: Thu, 22 Jul 2010 02:36:32 -0600 On 2010-07-21 22:06, li li wrote: > Hi all, > I am have some difficulty with the legend function. > I need to add a legend to describe the different line types in a plot. The > legend box is small. > It did not include sufficient length of each line type to help distinguish > the differnt line types. > Is there a way to fix this. > Thank you > Hannah > If I understand correctly, you want to have longer line segments in your legend. I agree that this is sometimes desirable but, with the current code, it's not possible - the segment length is hard-coded. If it matters enough to you, you can easily modify the code to achieve your aim: search for the following two lines and in each replace the '2' with a greater value, say '3'. First line: w0 <- w0 + (2 + x.off) * xchar Second line: seg.len <- 2 I don't think that this will have any undesirable side effects, but I haven't given it much thought. I haven't had any problems with this version of legend(). -Peter Ehlers