Re: [R] Legend issue with ggplot2
On 9/3/07, ONKELINX, Thierry [EMAIL PROTECTED] wrote: Dear useRs, I'm struggling with the new version of ggplot2. In the previous version I did something like this. But now this yield an error (object fill not found). library(ggplot2) dummy - data.frame(x = rep(1:10, 4), group = gl(4, 10)) dummy$y - dummy$x * rnorm(4)[dummy$group] + 5 * rnorm(4)[dummy$group] dummy$min - dummy$y - 5 dummy$max - dummy$y + 5 ggplot(data = dummy, aes(x = x, max = max, min = min, fill = group)) + geom_ribbon() + geom_line(aes(y = max, colour = fill)) + geom_line(aes(y = min, colour = fill)) Strange - I'm not sure why that ever worked. When I adjust the code to the line below, it works again. But this time with two legend keys for group. Any idea how to display only one legend key for group? The ggplot-code aboved yielded only on legend key. ggplot(data = dummy, aes(x = x, max = max, min = min, colour = group, fill = group)) + geom_ribbon() + geom_line(aes(y = max)) + geom_line(aes(y = min)) You can manually turn off one of the legends: sc - scale_colour_discrete() sc$legend - FALSE .last_plot + sc It's not very convenient though, so I'll think about how to do this automatically. The legends need to be more intelligent about only displaying the minimum necessary. Hadley __ R-help@stat.math.ethz.ch 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 issue with ggplot2
Thanks Hadley, I've been struggling with this all afternoon. But now it's working again. Since I'm using it in a script, the few extra lines don't bother me that much. Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 [EMAIL PROTECTED] www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -Oorspronkelijk bericht- Van: hadley wickham [mailto:[EMAIL PROTECTED] Verzonden: maandag 3 september 2007 15:15 Aan: ONKELINX, Thierry CC: r-help@stat.math.ethz.ch Onderwerp: Re: [R] Legend issue with ggplot2 On 9/3/07, ONKELINX, Thierry [EMAIL PROTECTED] wrote: Dear useRs, I'm struggling with the new version of ggplot2. In the previous version I did something like this. But now this yield an error (object fill not found). library(ggplot2) dummy - data.frame(x = rep(1:10, 4), group = gl(4, 10)) dummy$y - dummy$x * rnorm(4)[dummy$group] + 5 * rnorm(4)[dummy$group] dummy$min - dummy$y - 5 dummy$max - dummy$y + 5 ggplot(data = dummy, aes(x = x, max = max, min = min, fill = group)) + geom_ribbon() + geom_line(aes(y = max, colour = fill)) + geom_line(aes(y = min, colour = fill)) Strange - I'm not sure why that ever worked. When I adjust the code to the line below, it works again. But this time with two legend keys for group. Any idea how to display only one legend key for group? The ggplot-code aboved yielded only on legend key. ggplot(data = dummy, aes(x = x, max = max, min = min, colour = group, fill = group)) + geom_ribbon() + geom_line(aes(y = max)) + geom_line(aes(y = min)) You can manually turn off one of the legends: sc - scale_colour_discrete() sc$legend - FALSE .last_plot + sc It's not very convenient though, so I'll think about how to do this automatically. The legends need to be more intelligent about only displaying the minimum necessary. Hadley __ R-help@stat.math.ethz.ch 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 issue with ggplot2
Yes - all this stuff is currently rather undocumented. Hopefully that will change in the near future! Hadley On 9/3/07, ONKELINX, Thierry [EMAIL PROTECTED] wrote: Thanks Hadley, I've been struggling with this all afternoon. But now it's working again. Since I'm using it in a script, the few extra lines don't bother me that much. Thierry ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 [EMAIL PROTECTED] www.inbo.be Do not put your faith in what statistics say until you have carefully considered what they do not say. ~William W. Watt A statistical analysis, properly conducted, is a delicate dissection of uncertainties, a surgery of suppositions. ~M.J.Moroney -Oorspronkelijk bericht- Van: hadley wickham [mailto:[EMAIL PROTECTED] Verzonden: maandag 3 september 2007 15:15 Aan: ONKELINX, Thierry CC: r-help@stat.math.ethz.ch Onderwerp: Re: [R] Legend issue with ggplot2 On 9/3/07, ONKELINX, Thierry [EMAIL PROTECTED] wrote: Dear useRs, I'm struggling with the new version of ggplot2. In the previous version I did something like this. But now this yield an error (object fill not found). library(ggplot2) dummy - data.frame(x = rep(1:10, 4), group = gl(4, 10)) dummy$y - dummy$x * rnorm(4)[dummy$group] + 5 * rnorm(4)[dummy$group] dummy$min - dummy$y - 5 dummy$max - dummy$y + 5 ggplot(data = dummy, aes(x = x, max = max, min = min, fill = group)) + geom_ribbon() + geom_line(aes(y = max, colour = fill)) + geom_line(aes(y = min, colour = fill)) Strange - I'm not sure why that ever worked. When I adjust the code to the line below, it works again. But this time with two legend keys for group. Any idea how to display only one legend key for group? The ggplot-code aboved yielded only on legend key. ggplot(data = dummy, aes(x = x, max = max, min = min, colour = group, fill = group)) + geom_ribbon() + geom_line(aes(y = max)) + geom_line(aes(y = min)) You can manually turn off one of the legends: sc - scale_colour_discrete() sc$legend - FALSE .last_plot + sc It's not very convenient though, so I'll think about how to do this automatically. The legends need to be more intelligent about only displaying the minimum necessary. Hadley -- http://had.co.nz/ __ R-help@stat.math.ethz.ch 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 on graph
You can get the legend outside the plot region by 1. First changing the clipping region via par(xpd = TRUE) ; (or xpd=NA). see ?par 2. Specifying x and y coodinates for legend placement outside the limits of the plot region. This allows you to include a legend without adding a bunch of useless whitespace to the plot region; or to add a grid to the plot without interfering with the legend. Bert Gunter Genentech Nonclinical Statistics -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Nguyen Dinh Nguyen Sent: Monday, August 13, 2007 3:42 PM To: [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Subject: [R] Legend on graph Hi Akki, Then you may need to increase y-axis scale by ylim=c(min,max) Cheers Nguyen On 8/12/07, akki [EMAIL PROTECTED] wrote: Hi, I have a problem when I want to put a legend on the graph. I do: legend(topright, names(o), cex=0.9, col=plot_colors,lty=1:5, bty=n) but the legend is writen into the graph (graphs' top but into the graph), because I have values on this position. How can I write the legend on top the graph without the legend writes on graph's values. Thanks. __ R-help@stat.math.ethz.ch 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 on graph
If you are asking to have the values plotted on top of the legend, then you can do the following: plot(x, y, type='n', ...) # create plot, but don't plot legend('topright', ...) lines(x,y) # now plot the data If you want it outside the plot, check the archives for several examples. On 8/12/07, akki [EMAIL PROTECTED] wrote: Hi, I have a problem when I want to put a legend on the graph. I do: legend(topright, names(o), cex=0.9, col=plot_colors,lty=1:5, bty=n) but the legend is writen into the graph (graphs' top but into the graph), because I have values on this position. How can I write the legend on top the graph without the legend writes on graph's values. Thanks. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? __ R-help@stat.math.ethz.ch 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()
On Tue, 2007-07-31 at 10:21 -0700, amna khan wrote: Hi Sir How can I use legend() outside th e plot. Please guid in this regard. Thanks Create a plot, specifying outer margins to make space for the legend. Then move the legend to the open region. # Set 'xpd' to NA so that the legend is not clipped # at the plot region, which it is by default par(xpd = NA) # Make some room at the right hand side par(oma = c(0, 0, 0, 10)) # Do the plot plot(1:5) # Do the legend and use 'inset' to move the legend to # the right hand outer margin legend(topright, legend = 1:5, inset = c(-.4, .0)) You can adjust the outer margin settings and the 'inset' value as you may require to make room for the legend on the side required. See ?par and ?legend for more information. Another option would be to use layout() to create more than one plot region, perhaps adjusting the heights and/or widths of the plot regions, such that the data plot goes into one region and the legend into the other. See ?layout for more information. HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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 and x,y cordinate values
See help(legend) and help(identify). Ajay Singh wrote: Hi, I have two problems in R. 1. I need 10 cdfs on a graph, the graph needs to have legend. Can you let me know how to get legend on the graph? 2. In ecdf plot, I need to know the x and y co-ordinates. I have to get corresponding y coordinate values to x coordinate value so that I could be able to know the particular percentile value to the x-coordinate value. Can you let me know how could I be able the corresponding values of x to the y coordinates? Thanking you, Looking forward to your kind response, Sincerely, Ajay. __ R-help@stat.math.ethz.ch 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 + expression
what about legend(topleft, legend = bquote( R[c]2 == .(format(R2c,nsmall=2)) ) ) HTH, Peter __ R-help@stat.math.ethz.ch 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 + expression
On Fri, 2007-06-08 at 17:27 -0400, Pedro Mardones wrote: Dear all; A simple? question. I'm having a problem with a math expression in the legend of a plot and I haven't found the way to get this to work, so any help will be appreciate. Basically I want to include in the plot is the R-squared and its numerical value, so I tried this: R2c-0.82879 # R-squared of calibration model plot(1:10,1:10) legend(topleft, legend=c(expression(R[c]^2==format(R2c,nsmall=2 Thanks for any hint PM Try this: R2c - 0.82879 plot(1:10,1:10) R2c.2 - sprintf(%.2f, R2c) legend(topleft, legend = bquote(R[c]^2 == .(R2c.2))) See ?bquote and if you search the list archives, there are more complex examples of using 'plotmath' in legends. Note also that 'nsmall' in format() does not fix the number of digits after the decimal: format(0.82879, nsmall = 2) [1] 0.82879 See ?formatC and ?sprintf for better options. HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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 outside plotting area
Judith, Haven't tried it in anger myself, but two things suggest themselves. The first is to use the lattice package, which seems to draw keys (autokey option) outside the plot region by default. Look at the last couple of examples in ?xyplot. May save a lot of hassle... In classical R graphics, have you tried plotting everything explicitly inside a plot region with margins at zero? For example: plot.new() par(mar=c(0,0,0,0)) plot.window(xlim=c(-2,11), ylim=c(-3,13)) points(1:10,1:10, pch=1) points(1:10,10:1, pch=19) par(srt=90) text(x=-2, y=5, y-axis, pos=1, offset=0.5) par(srt=0) text(c(5,5), c(13,-1), labels=c(Title,x-axis), pos=1, offset=0.7, cex=c(1.5,1)) rect(-0.2,-0.2, 11.2,11.2) axis(side=1, at=0:10, pos=-0.2) axis(side=2, at=0:10, pos=-0.2) legend(x=5, y=-2, xjust=0.5, pch=c(1,19), legend=c(Type 1, Type 19), ncol=2) All very tedious, but it works. Also, fiddling around with things like pretty() on the data can automate most of the above positional choices if you're so inclined. And legend(..., plot=F) returns the legend size and coordinates if you want to fine-tune the location. Steve E [EMAIL PROTECTED] 23/05/2007 13:14:54 Quoting Judith Flores [EMAIL PROTECTED]: Hi, I have been trying many of the suggested options to place a legend outside plotting area, including something like this: par(xpd=T, oma=par()$oma+c(4.5,0,1.5,0),mar=par()$mar+c(1,0,1,0) But the aspect of the four plots gets compromised when I change the margin settings. I cannot use mtext because I need to use colors for the text. I tried layout, but wouldn't let me include the legend, only plots. I would appreciate very much some more help. Regards, J *** This email and any attachments are confidential. Any use, co...{{dropped}} __ R-help@stat.math.ethz.ch 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 outside plotting area
Quoting Judith Flores [EMAIL PROTECTED]: Hi, I have been trying many of the suggested options to place a legend outside plotting area, including something like this: par(xpd=T, oma=par()$oma+c(4.5,0,1.5,0),mar=par()$mar+c(1,0,1,0) But the aspect of the four plots gets compromised when I change the margin settings. I cannot use mtext because I need to use colors for the text. I tried layout, but wouldn't let me include the legend, only plots. I would appreciate very much some more help. Regards, J you can use 'mtext' with colors... mtext(whatever, col=blue...) -- Dr. Jose I. de las Heras Email: [EMAIL PROTECTED] The Wellcome Trust Centre for Cell BiologyPhone: +44 (0)131 6513374 Institute for Cell Molecular BiologyFax: +44 (0)131 6507360 Swann Building, Mayfield Road University of Edinburgh Edinburgh EH9 3JR UK __ R-help@stat.math.ethz.ch 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 outside plotting area
RSiteSearch(legend outside plot) will bring you many links to the discussions of this question. layout perfectly allows everything. typical sequence looks like this This divides the device region by two parts one below another: layout(matrix(c(1,2),byrow=TRUE), heights=[blah-blah-blah], [some other arguments]) Then we plot on the first part: plot( ... ) lines ( ... ) points ( ... ) grid( ... ) [ whatever you want on the plotting area] Then we finish plotting on the first part of the layout matrix and come to the next, legend part. The only thing to do is placing the legend in the top left corner. plot.new(); plot.window(c(0,1), c(0,1)); legend(0,1, [ legend text ] ) Judith Flores wrote: Hi, I have been trying many of the suggested options to place a legend outside plotting area, including something like this: par(xpd=T, oma=par()$oma+c(4.5,0,1.5,0),mar=par()$mar+c(1,0,1,0) But the aspect of the four plots gets compromised when I change the margin settings. I cannot use mtext because I need to use colors for the text. I tried layout, but wouldn't let me include the legend, only plots. I would appreciate very much some more help. Regards, J -- View this message in context: http://www.nabble.com/Legend-outside-plotting-area-tf3794564.html#a10735956 Sent from the R help mailing list archive at Nabble.com. __ R-help@stat.math.ethz.ch 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 outside plotting area
Judith, you might try split.screen() and related functions, see ?screen. Example: split.screen(c(1,2)) # 1 row, 2 columns split.screen(c(2,2), screen = 1) # split left column into 2x2 for(i in 3:6) { screen(i); plot(1:10) } screen(2) plot(1, type=n, axes=F, ann=F) # empty plot legend(center, pch=1, legend=Data) Regards, Carsten Hi, I have been trying many of the suggested options to place a legend outside plotting area, including something like this: par(xpd=T, oma=par()$oma+c(4.5,0,1.5,0),mar=par()$mar+c(1,0,1,0) But the aspect of the four plots gets compromised when I change the margin settings. I cannot use mtext because I need to use colors for the text. I tried layout, but wouldn't let me include the legend, only plots. I would appreciate very much some more help. Regards, J __ R-help@stat.math.ethz.ch 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 with mixed boxes and lines (not both)
On Mon, 2007-05-14 at 18:13 -0700, Michael Toews wrote: Hi, I seem to be unable to get a mixed legend that has lines *or* polygons (not both). For example: ppi - seq(0,2*pi,length.out=21)[-21] frame() plot.window(ylim=c(-5,5),xlim=c(-5,5),asp=1) polygon(cos(ppi)*4+rnorm(20,sd=.2),sin(ppi)*4+rnorm(20,sd=.2), col=green,border=FALSE) polygon(cos(ppi)*2+rnorm(20,sd=.1),sin(ppi)*2+rnorm(20,sd=.1), col=blue,border=FALSE) abline(0,2,col=red) legend(topleft,legend=c(out,in,line),bty=n, fill=c(green,blue,NA),col=c(NA,NA,red), lwd=c(NA,NA,1)) I'm really guessing the behaviour in the legend() call, by setting fill to NA for the item, etc. I also tried fill=c(green,blue,FALSE), but that didn't go over too well either. I also tried adding merge=TRUE, but that just puts the line into the box. I also tried using box.lwd=c(1,1,0), but that also did not work Is there either a way to do this or a clean workaround? Thanks in advance. +mt Is this what you want? ppi - seq(0, 2 * pi, length.out = 21)[-21] plot(c(-5, 5), c(-5, 5), xaxs = i, yaxs = i, type = n, axes = FALSE, ann = FALSE, asp=1) polygon(cos(ppi) * 4 + rnorm(20, sd = .2), sin(ppi) * 4 + rnorm(20, sd = .2), col = green, border = FALSE) polygon(cos(ppi) * 2 + rnorm(20, sd = .1), sin(ppi) * 2 + rnorm(20, sd= .1), col = blue, border = FALSE) abline(0, 2, col = red) legend(topleft, legend = c(out, in, line), bty = n, col = c(green, blue, red), lty = c(0, 0, 1), lwd = c(0, 0, 1), pch = c(22, 22, NA), pt.bg = c(green, blue, NA), pt.cex = 2) Instead of using 'fill', set the points explicitly and then define the point backgrounds, line types, etc. to get the desired result. See ?par for line type information. BTW, some strategically placed spaces would help with code readability. HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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 with density and fill
Hi Simon, Try fill=c(white,dark grey,black,black), density=c(NA,NA,25,75), etc Cheers Andrew On Sat, Mar 17, 2007 at 12:36:19PM +, Simon Pickett wrote: Hi, I am trying to make a legend with four symbols as follows 1.white box 2.black box 3.clear box (same as background) 4.clear box with shading lines but the shading lines arent showing... here is my code. par(bg=lightyellow) barplot(c(seq(1,6,1))) legend(8.5,0.3, bty=o, legend=c(young,old,male,female), col=black,cex=1.5, fill=c(white,dark grey,0,0),density=c(NA,NA,0,100),angle=45) any suggestions much appreciated, Thanks, Simon. Simon Pickett PhD student Centre For Ecology and Conservation Tremough Campus University of Exeter in Cornwall TR109EZ Tel 01326371852 __ R-help@stat.math.ethz.ch 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. -- Andrew Robinson Department of Mathematics and StatisticsTel: +61-3-8344-9763 University of Melbourne, VIC 3010 Australia Fax: +61-3-8344-4599 http://www.ms.unimelb.edu.au/~andrewpr http://blogs.mbs.edu/fishing-in-the-bay/ __ R-help@stat.math.ethz.ch 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 question
? par it is the xpd you're looking for. x - seq(-pi, pi, len = 65) par(xpd=TRUE) plot(x, sin(x), type=l, col = 2,xpd=NA) legend(x = 0, y = -1.5, legend text, pch = 1, xjust = 0.5) --- Jenny Barnes [EMAIL PROTECTED] wrote: Hi folks, Do you mind if I ask a related question that I have been having trouble with - how do you put the legend outside of the plot area (to the bottom of the area - below the x-axis title)? Could anybody show me using the example given below: x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) Thank you, I've not been able to do this simple bit of programming and it is very frustrating not to be able to add a simple key. Best Wishes, Jenny Hi Emili, Even though you are calling your horizontal coordinate y, and vertical coordinate z, the first and second arguments to legend(), namely x and y, should be the horizontal and vertical coordinates, respectively; and they are given in user coordinates (e.g., legend()'s x should be between 1960 and 1975 and legend()'s y should be between 1 and 4). If you want to use normalized coordinates (i.e. 0 to 1), you can scale as in this example: legend(x = par(usr)[1] + diff(par(usr)[1:2])*normalizedCoordX, y = par(usr)[3] + diff(par(usr)[3:4])*normalizedCoordY, ...) where normalizedCoordX and Y go from 0 to 1 (see ?par, par(usr) returns vector of c(xmin,xmax,ymin,ymax) of user coordinates on a plot) You can alternatively use legend(x = topleft,...) or bottomright, and so on to place your legend. If you want to add your legend outside of the plot, you should consider increasing the margins using the 'mar' argument in par(), and also setting par(xpd=TRUE) (so stuff can show up outside of the plotting region). Best regards, ST y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) within the data limits of your x and y) --- Emili Tortosa-Ausina [EMAIL PROTECTED] wrote: Hi to all, I'm sorry for posting this question, I am sure I am missing something important but after reading the documentation I cannot find where the problem is. I want to add a legend to a figure. If I use a simple example drawn from the R Reference Manual such as, for instance: x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) then everything works just fine. However, if I use other data such as, for instance: y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) plot(y, z, type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) then the legend is not shown. Any hints? Thanks in advance, Emili __ R-help@stat.math.ethz.ch 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. Food fight? Enjoy some healthy debate __ R-help@stat.math.ethz.ch 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. - End Forwarded Message - ~~ Jennifer Barnes PhD student: long range drought prediction Climate Extremes Group Department of Space and Climate Physics University College London Holmbury St Mary Dorking, Surrey, RH5 6NT Tel: 01483 204149 Mob: 07916 139187 Web: http://climate.mssl.ucl.ac.uk __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 question
On Wed, 28 Feb 2007 17:06:18 +0100 Emili Tortosa-Ausina [EMAIL PROTECTED] wrote: y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) plot(y, z, type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) your x and y are outside the plotting area. try using a different set, or better still use locator() to specify x, y interactively. hth, ranjan __ R-help@stat.math.ethz.ch 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 question
Hi Emili, Even though you are calling your horizontal coordinate y, and vertical coordinate z, the first and second arguments to legend(), namely x and y, should be the horizontal and vertical coordinates, respectively; and they are given in user coordinates (e.g., legend()'s x should be between 1960 and 1975 and legend()'s y should be between 1 and 4). If you want to use normalized coordinates (i.e. 0 to 1), you can scale as in this example: legend(x = par(usr)[1] + diff(par(usr)[1:2])*normalizedCoordX, y = par(usr)[3] + diff(par(usr)[3:4])*normalizedCoordY, ...) where normalizedCoordX and Y go from 0 to 1 (see ?par, par(usr) returns vector of c(xmin,xmax,ymin,ymax) of user coordinates on a plot) You can alternatively use legend(x = topleft,...) or bottomright, and so on to place your legend. If you want to add your legend outside of the plot, you should consider increasing the margins using the 'mar' argument in par(), and also setting par(xpd=TRUE) (so stuff can show up outside of the plotting region). Best regards, ST y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) within the data limits of your x and y) --- Emili Tortosa-Ausina [EMAIL PROTECTED] wrote: Hi to all, I'm sorry for posting this question, I am sure I am missing something important but after reading the documentation I cannot find where the problem is. I want to add a legend to a figure. If I use a simple example drawn from the R Reference Manual such as, for instance: x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) then everything works just fine. However, if I use other data such as, for instance: y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) plot(y, z, type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) then the legend is not shown. Any hints? Thanks in advance, Emili __ R-help@stat.math.ethz.ch 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. Food fight? Enjoy some healthy debate __ R-help@stat.math.ethz.ch 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 question
try: y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) plot(y, z, type=l, col = 2) legend(topleft, legend text, pch = 1, xjust = 0.5) On 2/28/07, Emili Tortosa-Ausina [EMAIL PROTECTED] wrote: Hi to all, I'm sorry for posting this question, I am sure I am missing something important but after reading the documentation I cannot find where the problem is. I want to add a legend to a figure. If I use a simple example drawn from the R Reference Manual such as, for instance: x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) then everything works just fine. However, if I use other data such as, for instance: y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) plot(y, z, type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) then the legend is not shown. Any hints? Thanks in advance, Emili __ R-help@stat.math.ethz.ch 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. -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem you are trying to solve? [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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 question
Hi folks, Do you mind if I ask a related question that I have been having trouble with - how do you put the legend outside of the plot area (to the bottom of the area - below the x-axis title)? Could anybody show me using the example given below: x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) Thank you, I've not been able to do this simple bit of programming and it is very frustrating not to be able to add a simple key. Best Wishes, Jenny Hi Emili, Even though you are calling your horizontal coordinate y, and vertical coordinate z, the first and second arguments to legend(), namely x and y, should be the horizontal and vertical coordinates, respectively; and they are given in user coordinates (e.g., legend()'s x should be between 1960 and 1975 and legend()'s y should be between 1 and 4). If you want to use normalized coordinates (i.e. 0 to 1), you can scale as in this example: legend(x = par(usr)[1] + diff(par(usr)[1:2])*normalizedCoordX, y = par(usr)[3] + diff(par(usr)[3:4])*normalizedCoordY, ...) where normalizedCoordX and Y go from 0 to 1 (see ?par, par(usr) returns vector of c(xmin,xmax,ymin,ymax) of user coordinates on a plot) You can alternatively use legend(x = topleft,...) or bottomright, and so on to place your legend. If you want to add your legend outside of the plot, you should consider increasing the margins using the 'mar' argument in par(), and also setting par(xpd=TRUE) (so stuff can show up outside of the plotting region). Best regards, ST y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) within the data limits of your x and y) --- Emili Tortosa-Ausina [EMAIL PROTECTED] wrote: Hi to all, I'm sorry for posting this question, I am sure I am missing something important but after reading the documentation I cannot find where the problem is. I want to add a legend to a figure. If I use a simple example drawn from the R Reference Manual such as, for instance: x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) then everything works just fine. However, if I use other data such as, for instance: y-c(1960, 1965, 1970, 1975) z-c(1, 2, 3, 4) plot(y, z, type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) then the legend is not shown. Any hints? Thanks in advance, Emili __ R-help@stat.math.ethz.ch 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. Food fight? Enjoy some healthy debate __ R-help@stat.math.ethz.ch 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. - End Forwarded Message - ~~ Jennifer Barnes PhD student: long range drought prediction Climate Extremes Group Department of Space and Climate Physics University College London Holmbury St Mary Dorking, Surrey, RH5 6NT Tel: 01483 204149 Mob: 07916 139187 Web: http://climate.mssl.ucl.ac.uk __ R-help@stat.math.ethz.ch 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 question
On Wed, 28 Feb 2007 16:52:05 + (GMT), Jenny Barnes [EMAIL PROTECTED] wrote: Hi folks, Do you mind if I ask a related question that I have been having trouble with - how do you put the legend outside of the plot area (to the bottom of the area - below the x-axis title)? Could anybody show me using the example given below: x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, col = 2) legend(x = -3, y = .9, legend text, pch = 1, xjust = 0.5) Thank you, I've not been able to do this simple bit of programming and it is very frustrating not to be able to add a simple key. Have a look at ?par and argument 'inset' in ?legend itself. Here's one way: x - seq(-pi, pi, len=65) par(mar=c(par(mar)[1] + 2, par(mar)[-1])) plot(x, sin(x), type=l, col=2) par(xpd=TRUE) legend(bottom, legend text, pch=1, inset=-0.3) -- Seb __ R-help@stat.math.ethz.ch 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 in lattice densityplot [Broadcast]
On 2/14/07, Albert Vilella [EMAIL PROTECTED] wrote: I am defining the legend using trellis.par.set (not sure if correctly), and space does not seem to do the trick. auto-key (here commented) places it to the top... You want 'auto.key = list(space = right)'. Deepayan a = rep(c(alfa,beta,gamma,alfa,beta,gamma),100) b = rnorm(600) input=data.frame(a,b) densityplot(~(input$b), groups=input$a, plot.points=FALSE, # auto.key=TRUE, space = left, trellis.par.set(superpose.line = list( col = rep( c(yellow,green,red,blue,orange,pink,lightblue,black,brown), 3) , lwd=3, lty = rep( c(1,2,3), each = 9) ) ) ) On 2/14/07, Wiener, Matthew [EMAIL PROTECTED] wrote: From the documentation for xyplot (referred to from densityplot): The position of the key can be controlled in either of two possible ways. If a component called space is present, the key is positioned outside the plot region, in one of the four sides, determined by the value of space, which can be one of top, bottom, left and right. Hope this helps, Matt Wiener -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Albert Vilella Sent: Wednesday, February 14, 2007 8:46 AM To: R-help@stat.math.ethz.ch Subject: Re: [R] legend in lattice densityplot [Broadcast] How can I place the legend to the left or right of the densityplot? By default, it goes at the top, and as it is a rather long list, the density plot only uses half the space of the whole graphic... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Me too on Windows XP. Its probably just a bug or unimplemented feature in the SVG driver. Write to the maintainer of that package For a workaround generate fig output and then convert it to svg using whatever fig editor or converter you have. (On my windows system I use the free fig2dev converter although it inserted a DOCTYPE statement into the generated SVG file that IE7 did not recognize but once I manually deleted that it displayed ok in IE7.) # after producing file01.fig run # fig2dev -L svg file01.fig file01.svg # or use some other fig to svg converter or editor xfig(file = /file01.fig, onefile = TRUE) library(lattice) set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 dev.off() On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Should it be a problem to print this dashed line plots as svgs? library(RSvgDevice) devSVG(file = /home/avilella/file01.svg, width = 20, height = 16, bg = white, fg = black, onefile=TRUE, xmlHeader=TRUE) densityplot(...) dev.off() I am getting all the lines as continuous, not dashed... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Yes by using the lty suboption of superpose.line. Here is a modification of the prior example to illustrate: We also use lwd as well in this example. set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f
Re: [R] legend in lattice densityplot
How can I place the legend to the left or right of the densityplot? By default, it goes at the top, and as it is a rather long list, the density plot only uses half the space of the whole graphic... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Me too on Windows XP. Its probably just a bug or unimplemented feature in the SVG driver. Write to the maintainer of that package For a workaround generate fig output and then convert it to svg using whatever fig editor or converter you have. (On my windows system I use the free fig2dev converter although it inserted a DOCTYPE statement into the generated SVG file that IE7 did not recognize but once I manually deleted that it displayed ok in IE7.) # after producing file01.fig run # fig2dev -L svg file01.fig file01.svg # or use some other fig to svg converter or editor xfig(file = /file01.fig, onefile = TRUE) library(lattice) set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 dev.off() On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Should it be a problem to print this dashed line plots as svgs? library(RSvgDevice) devSVG(file = /home/avilella/file01.svg, width = 20, height = 16, bg = white, fg = black, onefile=TRUE, xmlHeader=TRUE) densityplot(...) dev.off() I am getting all the lines as continuous, not dashed... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Yes by using the lty suboption of superpose.line. Here is a modification of the prior example to illustrate: We also use lwd as well in this example. set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis settings and to get lines or points in the key, you probably need at least to use key = simpleKey() rather than the auto.key argument, and you may need to look into draw.key(). Other people on the list might know simpler approaches for using your own colors in this situation. If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot .
Re: [R] legend in lattice densityplot [Broadcast]
From the documentation for xyplot (referred to from densityplot): The position of the key can be controlled in either of two possible ways. If a component called space is present, the key is positioned outside the plot region, in one of the four sides, determined by the value of space, which can be one of top, bottom, left and right. Hope this helps, Matt Wiener -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Albert Vilella Sent: Wednesday, February 14, 2007 8:46 AM To: R-help@stat.math.ethz.ch Subject: Re: [R] legend in lattice densityplot [Broadcast] How can I place the legend to the left or right of the densityplot? By default, it goes at the top, and as it is a rather long list, the density plot only uses half the space of the whole graphic... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Me too on Windows XP. Its probably just a bug or unimplemented feature in the SVG driver. Write to the maintainer of that package For a workaround generate fig output and then convert it to svg using whatever fig editor or converter you have. (On my windows system I use the free fig2dev converter although it inserted a DOCTYPE statement into the generated SVG file that IE7 did not recognize but once I manually deleted that it displayed ok in IE7.) # after producing file01.fig run # fig2dev -L svg file01.fig file01.svg # or use some other fig to svg converter or editor xfig(file = /file01.fig, onefile = TRUE) library(lattice) set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 dev.off() On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Should it be a problem to print this dashed line plots as svgs? library(RSvgDevice) devSVG(file = /home/avilella/file01.svg, width = 20, height = 16, bg = white, fg = black, onefile=TRUE, xmlHeader=TRUE) densityplot(...) dev.off() I am getting all the lines as continuous, not dashed... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Yes by using the lty suboption of superpose.line. Here is a modification of the prior example to illustrate: We also use lwd as well in this example. set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis settings and to get lines or points in the key, you probably need at least to use key = simpleKey() rather than the auto.key argument, and you may need to look into draw.key(). Other people on the list might know simpler approaches for using your own colors in this situation. If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6
Re: [R] legend in lattice densityplot [Broadcast]
I am defining the legend using trellis.par.set (not sure if correctly), and space does not seem to do the trick. auto-key (here commented) places it to the top... a = rep(c(alfa,beta,gamma,alfa,beta,gamma),100) b = rnorm(600) input=data.frame(a,b) densityplot(~(input$b), groups=input$a, plot.points=FALSE, # auto.key=TRUE, space = left, trellis.par.set(superpose.line = list( col = rep( c(yellow,green,red,blue,orange,pink,lightblue,black,brown), 3) , lwd=3, lty = rep( c(1,2,3), each = 9) ) ) ) On 2/14/07, Wiener, Matthew [EMAIL PROTECTED] wrote: From the documentation for xyplot (referred to from densityplot): The position of the key can be controlled in either of two possible ways. If a component called space is present, the key is positioned outside the plot region, in one of the four sides, determined by the value of space, which can be one of top, bottom, left and right. Hope this helps, Matt Wiener -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Albert Vilella Sent: Wednesday, February 14, 2007 8:46 AM To: R-help@stat.math.ethz.ch Subject: Re: [R] legend in lattice densityplot [Broadcast] How can I place the legend to the left or right of the densityplot? By default, it goes at the top, and as it is a rather long list, the density plot only uses half the space of the whole graphic... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Me too on Windows XP. Its probably just a bug or unimplemented feature in the SVG driver. Write to the maintainer of that package For a workaround generate fig output and then convert it to svg using whatever fig editor or converter you have. (On my windows system I use the free fig2dev converter although it inserted a DOCTYPE statement into the generated SVG file that IE7 did not recognize but once I manually deleted that it displayed ok in IE7.) # after producing file01.fig run # fig2dev -L svg file01.fig file01.svg # or use some other fig to svg converter or editor xfig(file = /file01.fig, onefile = TRUE) library(lattice) set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 dev.off() On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Should it be a problem to print this dashed line plots as svgs? library(RSvgDevice) devSVG(file = /home/avilella/file01.svg, width = 20, height = 16, bg = white, fg = black, onefile=TRUE, xmlHeader=TRUE) densityplot(...) dev.off() I am getting all the lines as continuous, not dashed... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Yes by using the lty suboption of superpose.line. Here is a modification of the prior example to illustrate: We also use lwd as well in this example. set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis
Re: [R] legend in lattice densityplot [Broadcast]
I use key= instead. Much more flexible. I set the parameters in trellis.par.set for the plot and then take these settings in key to get them in the legend. space= is part of the key= settings. As in this (to stick with your example): library(lattice) lg - c(alfa,beta,gamma) a - rep(lg, 200) b - rnorm(600) input - data.frame(a,b) densityplot(~(input$b), groups = input$a, plot.points = FALSE, trellis.par.set(superpose.line = list( col = rep( c(yellow,green,red,blue,orange,pink,lightblue,black,brown) ,3), lwd = rep( 3, 27), lty = rep( c(1,2,3), each = 9) ) ), key = list(space=left, lines=list( col = trellis.par.get()$superpose.line$col[1:3], lwd = trellis.par.get()$superpose.line$lwd[1:3], lty = trellis.par.get()$superpose.line$lty[1:3] ), text=list(lg)) ) Hope this helps. Rene -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Albert Vilella Sent: Wednesday, February 14, 2007 6:50 AM To: Wiener, Matthew Cc: R-help@stat.math.ethz.ch Subject: Re: [R] legend in lattice densityplot [Broadcast] I am defining the legend using trellis.par.set (not sure if correctly), and space does not seem to do the trick. auto-key (here commented) places it to the top... a = rep(c(alfa,beta,gamma,alfa,beta,gamma),100) b = rnorm(600) input=data.frame(a,b) densityplot(~(input$b), groups=input$a, plot.points=FALSE, # auto.key=TRUE, space = left, trellis.par.set(superpose.line = list( col = rep( c(yellow,green,red,blue,orange,pink,lightblue,black,brown) , 3) , lwd=3, lty = rep( c(1,2,3), each = 9) ) ) ) On 2/14/07, Wiener, Matthew [EMAIL PROTECTED] wrote: From the documentation for xyplot (referred to from densityplot): The position of the key can be controlled in either of two possible ways. If a component called space is present, the key is positioned outside the plot region, in one of the four sides, determined by the value of space, which can be one of top, bottom, left and right. Hope this helps, Matt Wiener -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Albert Vilella Sent: Wednesday, February 14, 2007 8:46 AM To: R-help@stat.math.ethz.ch Subject: Re: [R] legend in lattice densityplot [Broadcast] How can I place the legend to the left or right of the densityplot? By default, it goes at the top, and as it is a rather long list, the density plot only uses half the space of the whole graphic... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Me too on Windows XP. Its probably just a bug or unimplemented feature in the SVG driver. Write to the maintainer of that package For a workaround generate fig output and then convert it to svg using whatever fig editor or converter you have. (On my windows system I use the free fig2dev converter although it inserted a DOCTYPE statement into the generated SVG file that IE7 did not recognize but once I manually deleted that it displayed ok in IE7.) # after producing file01.fig run # fig2dev -L svg file01.fig file01.svg # or use some other fig to svg converter or editor xfig(file = /file01.fig, onefile = TRUE) library(lattice) set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 dev.off() On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Should it be a problem to print this dashed line plots as svgs? library(RSvgDevice) devSVG(file = /home/avilella/file01.svg, width = 20, height = 16, bg = white, fg = black, onefile=TRUE, xmlHeader=TRUE) densityplot(...) dev.off() I am getting all the lines as continuous, not dashed... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Yes by using the lty suboption of superpose.line. Here is a modification of the prior example to illustrate: We also use lwd as well in this example. set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely
Re: [R] legend font
On 2/14/2007 1:32 PM, Tyler Smith wrote: Hi, I'd like to make the text in my legends italic, but I can't figure out how to do so. font=3 doesn't work. Googling brings up the possibility of expression(italic()), which produces italics, but I can't get this to work with my label data, which is a vector of strings: legend(locator(1), legend = levels(factor(label.vector)), col = plotting.colours, pch =plotsym.bw, cex = 0.7 ) How can I do this? This should work: plot(1,1) savefont - par(font=3) legend(topright, legend=c('Label 1', 'Label 2'), pch=1:2) par(savefont) Duncan Murdoch __ R-help@stat.math.ethz.ch 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 font
On Wed, Feb 14, 2007 at 02:40:47PM -0500, Duncan Murdoch wrote: On 2/14/2007 1:32 PM, Tyler Smith wrote: Hi, I'd like to make the text in my legends italic, ... How can I do this? This should work: plot(1,1) savefont - par(font=3) legend(topright, legend=c('Label 1', 'Label 2'), pch=1:2) par(savefont) Thanks! I don't understand it yet, but it does indeed work. -- Regards, Tyler Smith __ R-help@stat.math.ethz.ch 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 font
On 2/14/2007 3:12 PM, Tyler Smith wrote: On Wed, Feb 14, 2007 at 02:40:47PM -0500, Duncan Murdoch wrote: On 2/14/2007 1:32 PM, Tyler Smith wrote: Hi, I'd like to make the text in my legends italic, ... How can I do this? This should work: plot(1,1) savefont - par(font=3) legend(topright, legend=c('Label 1', 'Label 2'), pch=1:2) par(savefont) Thanks! I don't understand it yet, but it does indeed work. The idea is that the first par() command changes the default font for everything. (The legend() function doesn't pass any font request down to the graphics system, it just uses the default font.) It also returns the old font setting and I saved it in savefont. The second par() call restores the old font setting. Duncan Murdoch __ R-help@stat.math.ethz.ch 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/plotmath/substitute problem
On 12/14/2006 5:05 PM, Philipp Pagel wrote: Dear R Experts, I am trying to produce a legend for a series of plots which are generated in a loop. The legend is supposed to look like this: 2000: gamma=1.8 where gamma is replaced by the greek letter and both the year and the value of gamma are stored in variables. Everything works fine as long as I have only one data series: year = 2001 g = 1.9 plot(1) legend('top', legend=substitute(paste(year, ': ', gamma, '=', g), list(year=year, g=g)) ) My problem starts, when I want to put more than one series of data in the plot and accordingly need one legend row per data series: year1 = 2001 year2 = 2005 g1 = 1.9 g2 = 1.7 plot(1) legend('top', legend=c( substitute(paste(year, ': ', gamma, '=', g), list(year=year1, g=g1)), substitute(paste(year, ': ', gamma, '=', g), list(year=year2, g=g2)) ) ) This obviously does not produce the desired result. Apparently, I am not generating a list of expressions, as intended. So I thought, maybe R uses a variety of the recycling rule here and tried: The problem is that legend wants an expression, but substitute() isn't returning one, it's returning a call, and c(call1,call2) produces a list of two calls, not an expression holding two calls. So the following would work, but there might be something more elegant: year1 = 2001 year2 = 2005 g1 = 1.9 g2 = 1.7 plot(1) legend('top', legend=c( as.expression(substitute(paste(year, ': ', gamma, '=', g), list(year=year1, g=g1))), as.expression(substitute(paste(year, ': ', gamma, '=', g), list(year=year2, g=g2))) ) ) Duncan Murdoch year = c(2001, 2005) g = c(1.9, 1.7) plot(1) legend('top', legend=list( substitute(paste(year, ': ', gamma, '=', g), list(year=year, g=g)), ) ) No succes, either... I have read and re-read the documentation for legend, expression, substitute and plotmath but can't figure it out. Even drinking a cup of tea prepared from fine-cut man page printouts didn't lead to satori. I'm probably missing something simple. Any hints are highly appreciated. Thanks Philipp __ R-help@stat.math.ethz.ch 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/plotmath/substitute problem
On Thu, Dec 14, 2006 at 06:25:49PM -0500, Duncan Murdoch wrote: On 12/14/2006 5:05 PM, Philipp Pagel wrote: My problem starts, when I want to put more than one series of data in the plot and accordingly need one legend row per data series: year1 = 2001 year2 = 2005 g1 = 1.9 g2 = 1.7 plot(1) legend('top', legend=c( substitute(paste(year, ': ', gamma, '=', g), list(year=year1, g=g1)), substitute(paste(year, ': ', gamma, '=', g), list(year=year2, g=g2)) ) ) This obviously does not produce the desired result. Apparently, I am not generating a list of expressions, as intended. So I thought, maybe R uses a variety of the recycling rule here and tried: The problem is that legend wants an expression, but substitute() isn't returning one, it's returning a call, and c(call1,call2) produces a list of two calls, not an expression holding two calls. So the following would work, but there might be something more elegant: Thanks a lot! Learned something, again. cu Philipp -- Dr. Philipp PagelTel. +49-8161-71 2131 Dept. of Genome Oriented Bioinformatics Fax. +49-8161-71 2186 Technical University of Munich 85350 Freising, Germany http://mips.gsf.de/staff/pagel __ R-help@stat.math.ethz.ch 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 in lattice densityplot
Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis settings and to get lines or points in the key, you probably need at least to use key = simpleKey() rather than the auto.key argument, and you may need to look into draw.key(). Other people on the list might know simpler approaches for using your own colors in this situation. If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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 in lattice densityplot
Albert Vilella wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? Yes. Using Gabor's suggestion of changing the trellis settings within the call to densityplot(), try something like this: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE, par.settings = list(superpose.line = list(col = c(Green, Red, Blue), lty=c(2,1,2 On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis settings and to get lines or points in the key, you probably need at least to use key = simpleKey() rather than the auto.key argument, and you may need to look into draw.key(). Other people on the list might know simpler approaches for using your own colors in this situation. If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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 in lattice densityplot
Yes by using the lty suboption of superpose.line. Here is a modification of the prior example to illustrate: We also use lwd as well in this example. set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis settings and to get lines or points in the key, you probably need at least to use key = simpleKey() rather than the auto.key argument, and you may need to look into draw.key(). Other people on the list might know simpler approaches for using your own colors in this situation. If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 in lattice densityplot
Should it be a problem to print this dashed line plots as svgs? library(RSvgDevice) devSVG(file = /home/avilella/file01.svg, width = 20, height = 16, bg = white, fg = black, onefile=TRUE, xmlHeader=TRUE) densityplot(...) dev.off() I am getting all the lines as continuous, not dashed... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Yes by using the lty suboption of superpose.line. Here is a modification of the prior example to illustrate: We also use lwd as well in this example. set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis settings and to get lines or points in the key, you probably need at least to use key = simpleKey() rather than the auto.key argument, and you may need to look into draw.key(). Other people on the list might know simpler approaches for using your own colors in this situation. If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 in lattice densityplot
Me too on Windows XP. Its probably just a bug or unimplemented feature in the SVG driver. Write to the maintainer of that package For a workaround generate fig output and then convert it to svg using whatever fig editor or converter you have. (On my windows system I use the free fig2dev converter although it inserted a DOCTYPE statement into the generated SVG file that IE7 did not recognize but once I manually deleted that it displayed ok in IE7.) # after producing file01.fig run # fig2dev -L svg file01.fig file01.svg # or use some other fig to svg converter or editor xfig(file = /file01.fig, onefile = TRUE) library(lattice) set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 dev.off() On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Should it be a problem to print this dashed line plots as svgs? library(RSvgDevice) devSVG(file = /home/avilella/file01.svg, width = 20, height = 16, bg = white, fg = black, onefile=TRUE, xmlHeader=TRUE) densityplot(...) dev.off() I am getting all the lines as continuous, not dashed... On 11/30/06, Gabor Grothendieck [EMAIL PROTECTED] wrote: Yes by using the lty suboption of superpose.line. Here is a modification of the prior example to illustrate: We also use lwd as well in this example. set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = c(1,1,2,2), lty = 1:2, lwd = c(1,1,1,1,2 On 11/30/06, Albert Vilella [EMAIL PROTECTED] wrote: Can I combine colors and line types? For example, would it be possible to have 5 colors per 2 types of lines (continuous and dashed)? On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis settings and to get lines or points in the key, you probably need at least to use key = simpleKey() rather than the auto.key argument, and you may need to look into draw.key(). Other people on the list might know simpler approaches for using your own colors in this situation. If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010
Re: [R] legend in lattice densityplot
Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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 in lattice densityplot
Are this legend colors correlated to the plot? If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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 in lattice densityplot
Try specifying it at the par.settings= level since that is where both the plot and the legend get it from: set.seed(1) DF - data.frame(x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)), f = sample(c(A,B,C,D,E),300,replace=TRUE)) library(lattice) densityplot(~ x, DF, groups = f, auto.key = TRUE, plot.points = FALSE, par.settings = list(superpose.line = list(col = heat.colors(5 On 11/29/06, Albert Vilella [EMAIL PROTECTED] wrote: Are this legend colors correlated to the plot? If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 in lattice densityplot
Albert Vilella wrote: Are this legend colors correlated to the plot? They are if you rely on the colors in trellis.par.get(superpose.line)$col If you want different colors you might use trellis.par.set() to temporarily change the colors: x - c(rnorm(100,-2,1),rnorm(100,0,1),rnorm(100,2,1)) f - rep(c(A,B,C), each=100) df - data.frame(x,f) library(lattice) oldpar - trellis.par.get(superpose.line)$col trellis.par.set(superpose.line = list(col = heat.colors(3))) densityplot(~ x, groups = f, data = df, plot.points=FALSE, auto.key=TRUE) trellis.par.set(superpose.line = list(col = oldpar)) If you don't require points or lines in the key, you also could do something like this: densityplot(~ x, groups = f, data = df, plot.points=FALSE, key = simpleKey(levels(df$f), lines=FALSE, points=FALSE, col=heat.colors(3)), col=heat.colors(3)) To use your own colors without changing the trellis settings and to get lines or points in the key, you probably need at least to use key = simpleKey() rather than the auto.key argument, and you may need to look into draw.key(). Other people on the list might know simpler approaches for using your own colors in this situation. If I do a: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE,col=heat.colors(5)) I get different colors in the legend than the plot... On 11/29/06, Chuck Cleland [EMAIL PROTECTED] wrote: Albert Vilella wrote: Hi, I have a densityplot like this: x = c(rnorm(100,1,2),rnorm(100,2,4),rnorm(100,3,6)) f = sample(c(A,B,C,D,E),300,replace=TRUE) df=data.frame(x,f) library(lattice) attach(df) densityplot(~x, groups=f) And I want to add a legend with the colours for the factors. How can I do that? How can I not have the dots of the distribution at the bottom, or at least, make them occupy less vertical space? Change the last line to the following: densityplot(~x, groups=f, plot.points=FALSE, auto.key=TRUE) See ?panel.densityplot . __ R-help@stat.math.ethz.ch 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. -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 -- Chuck Cleland, Ph.D. NDRI, Inc. 71 West 23rd Street, 8th floor New York, NY 10010 tel: (212) 845-4495 (Tu, Th) tel: (732) 512-0171 (M, W, F) fax: (917) 438-0894 __ R-help@stat.math.ethz.ch 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 problems in lattice
Ernst O Ahlberg Helgee wrote: Hi! Im sorry to bother you but I cant fix this. I use the lattice function levelplot and I want the colorkey at the bottom, how do I get it there? I have tried changing colorkey.space and changing in legend but I cant get it right, plz help btw I'd like to speceify strings to appear at the tick marks and also there I fail any thoughts? cheers Ernst __ R-help@stat.math.ethz.ch 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. Hi, Ernst, Please read ?levelplot. Under the argument for colorkey you will see: colorkey: logical specifying whether a color key is to be drawn alongside the plot, or a list describing the color key. The list may contain the following components: 'space': location of the colorkey, can be one of 'left', 'right', 'top' and 'bottom'. Defaults to 'right'. So the answer to your first question is: levelplot(..., colorkey = list(space = bottom)) For your second question, use the scale argument. See ?xyplot for details. For example, levelplot(..., scale = list(x = list(at = 1:4, labels = letters[1:4]))) HTH, --sundar __ R-help@stat.math.ethz.ch 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 box line thickness
On Mon, 2006-08-28 at 17:55 -0700, Phil Turk wrote: I am merely trying to increase the line thickness, or line width, of the box drawn around the legend in a plot I am constructing. The help page on 'legend' was of no use. Does anyone have an idea on how to do this? Please respond to [EMAIL PROTECTED] Thanks! There are two options. The easier one is not immediately evident from the help page and requires reviewing the R code for the legend function, which reveals that there is an internal function called rect2(), which is built on top of rect(). It is this function that draws the outer box. The help page for rect() shows that the line width argument 'lwd' in the function defaults to par(lwd). See ?par for more information. Thus using: par(lwd = SomethingGreaterThan1) before the call to legend will set the box to a wider line thickness. Be sure to set par(lwd = 1) before any other plot calls to return to the default setting. Second, the Value section of ?legend clearly indicates: Value A list with list components rect a list with components w, h positive numbers giving width and height of the legend's box. left, top x and y coordinates of upper left corner of the box. text a list with components x, y numeric vectors of length length(legend), giving the x and y coordinates of the legend's text(s). returned invisibly. Thus, expanding on the third example in ?legend: ## right-justifying a set of labels: thanks to Uwe Ligges x - 1:5; y1 - 1/x; y2 - 2/x plot(rep(x, 2), c(y1, y2), type=n, xlab=x, ylab=y) lines(x, y1); lines(x, y2, lty=2) # Key call here temp - legend(topright, legend = c( , ), text.width = strwidth(1,000,000), lty = 1:2, xjust = 1, yjust = 1, title = Line Types) text(temp$rect$left + temp$rect$w, temp$text$y, c(1,000, 1,000,000), pos=2) # Now do the legend box using a wide line: rect(temp$rect$left, temp$rect$top - temp$rect$h, temp$rect$left + temp$rect$w, temp$rect$top + temp$rect$h, lwd = 2) It would not seem unreasonable to add new arguments to legend(), perhaps calling them box.lwd and box.lty, which can then be passed to the rect2() internal function call for the box by modifying the existing call to: rect2(left, top, dx = w, dy = h, col = bg, density = NULL, lwd = box.lwd, lty = box.lty) HTH, Marc Schwartz __ R-help@stat.math.ethz.ch 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 on trellis plot
1. Use the x, y and corner components to the key= list to specify the legend position, and 2. pass the panel.number in the panel function and test that as shown in the panel function below. Alternately you can place the horizontal line on afterwards using trellis.focus/trellis.unfocus as shown below. Read the material under key= and panel= in ?xyplot for more information on the key and panel arguments and read ?trellis.focus for more information on trellis.focus/trellis.unfocus. xyplot(DV~TIME | DOSE, data=data, groups=ID, layout=c(2,1), key=list(x=.1,y=.8,corner=c(0,0),border=TRUE,colums=2,text=list(c(ID1,ID2),col=c(1,4)), lines=list(type=o,pch=c(1,16),lty=c(1,2), col=c(1,4)), layout.heights=list(key.axis.padding=15)), panel = function(x,y,groups,...,panel.number) { panel.superpose.2(x,y,groups,...,type=o,pch=c(1,16), lty=c(1,2), col=c(1,4), cex=0.8) if (panel.number == 1) panel.abline(h=0.301,col=5,lty=1,lwd=2) } ) # add a red horizontal line only to panel 2, 1 trellis.focus(panel, 2, 1, highlight = FALSE) panel.abline(h=0.301,col=2,lty=1,lwd=2) trellis.unfocus() On 8/9/06, HKAG (Henrik Agersø) [EMAIL PROTECTED] wrote: Dear all I have two questions regarding trellis plots - which I hope you may be able to help me with. Is it possible to place the key in a trellis plot on the panel (instead of beside the panel)? This will cause the same key to be reproduced on each panel. Please see the plot below - here I placed the legend below the plot. I tried moving the key to the function statement, but it did not really work out the way I expected. One last thing, in the plot below I placed a horizontal line on the plot, is it possible to only have the horizontal line on the left panel (I remember that in S it was possible to state something like if(get(cell,fr=9)==2) in the function statement to include the line on only one of the panels)? All suggestions will highly appreciated. Br Henrik ### data - as.data.frame(cbind(rep(1:4,each=25), rep(1:2,each=50) ,rep(1:25,4), rnorm(100,0,1) )) names(data) - c(ID,DOSE,TIME,DV) xyplot(DV~TIME | DOSE, data=data, groups=ID, layout=c(2,1), key=list(space=bottom,border=TRUE,colums=2,text=list(c(ID1,ID2),col=c(1,4)), lines=list(type=o,pch=c(1,16),lty=c(1,2), col=c(1,4)), layout.heights=list(key.axis.padding=15)), panel = function(x,y,groups,...) { panel.superpose.2(x,y,groups,...,type=o,pch=c(1,16), lty=c(1,2), col=c(1,4), cex=0.8) panel.abline(h=0.301,col=5,lty=1,lwd=2) } ) ### [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch 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@stat.math.ethz.ch 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 outside plotting area
see http://finzi.psych.upenn.edu/R/Rhelp02a/archive/68585.html Georg Otto wrote: Hi, I would like to place a legend outside a plotting area. Could anybody give me a hint how this is done? Cheers, Georg __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html confidentiality notice: The information contained in this e-mail is confidential and...{{dropped}} __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend with filled boxes AND lines
Did you try legend(.., lty=..., fill=..., merge = TRUE) ? In an example I just tried, this allowed to give filled boxes *and* lines. Please give a reproducible example of what you did -- maybe by modifying one of the many example(legend) examples. Martin Maechler, ETH Zurich florian == florian koller [EMAIL PROTECTED] on Mon, 3 Jul 2006 13:40:33 +0200 writes: florian Dear all, florian Is there a straightforward way to create a legend florian box that has both filled boxes and lines? So far I florian have built around this problem by creating two florian legends (with bty = n) and manually drawing a box florian around both (but this is cumbersome, because I have florian to check upon the y coordinates of the legends florian every time). florian If I do something like legend( ...,c(X1,X2, florian mean), fill = c(red, blue, 0), lty = (0,0,2)) florian , I cannot get rid of the unfilled box or change florian the color of the fill box border (from its default florian color black), and I end up with two filled boxes florian and an empty, black-lined box plus the line as a florian legend for the third argument mean. This trick florian therefore only works if I define black as the bg florian color for the complete legend box (because it masks florian the empty box from the fill argument). So, if there florian is a command to modify the color of the fill box florian border line (not the legend box border line), this florian would help me, too (still not ideal, though...). florian Thanks, florian Florian florian __ florian Florian Koller florian GfK Fernsehforschung GmbH florian Research Consulting Development florian Nordwestring 101 florian D-90319 Nürnberg florian Fon +49 (0)911 395-3554 florian Fax +49 (0)911 395-4130 florian www.gfk.de / www.gfk.com florian _ florian Diese E-Mail (ggf. nebst Anhang) enthält vertrauliche und/oder rechtlich florian geschützte Informationen. Wenn Sie nicht der richtige Adressat sind, oder florian diese E-Mail irrtümlich erhalten haben, informieren Sie bitte sofort den florian Absender und vernichten Sie diese Mail. Das unerlaubte Kopieren sowie die florian unbefugte Weitergabe dieser Mail ist nicht gestattet. florian This e-mail (and any attachment/s) contains confidential and/or privileged florian information. If you are not the intended recipient (or have received this florian e-mail in error) please notify the sender immediately and destroy this florian e-mail. Any unauthorised copying, disclosure or distribution of the florian material in this e-mail is strictly forbidden. florian __ florian R-help@stat.math.ethz.ch mailing list florian https://stat.ethz.ch/mailman/listinfo/r-help florian PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend titles in log plots broken? (ver. 2.2.1)
I forgot to mention: if you want to use the patch without installing a new version of R, it's available by sourcing the file https://svn.r-project.org/R/branches/R-2-3-patches/src/library/graphics/R/legend.R It was just a one character change: Index: legend.R === --- legend.R(revision 38022) +++ legend.R(revision 38033) @@ -226,7 +226,7 @@ xt - xt + x.intersp * xchar if(plot) { - if (!is.null(title)) text(left + w/2, top - ymax, labels = title, + if (!is.null(title)) text2(left + w/2, top - ymax, labels = title, adj = c(0.5, 0), cex = cex, col = text.col) text2(xt, yt, labels = legend, adj = adj, cex = cex, col = text.col) Duncan Murdoch Rob Steele wrote: Legend titles work in linear plots: curve(1/x, xlim = c(0, 1)) legend(x = 'topright', inset = 0.04, legend = '1/x', lty = 1, title = 'Legend Title') But when you change to a log plot on either dimension things get screwy: curve(1/x, xlim = c(0, 1), log = 'y') legend(x = 'topright', inset = 0.04, legend = '1/x', lty = 1, title = 'Legend Title') If you save the value legend() returns you can look at it and see that it's messed up: l - legend(x = 'topright', inset = 0.04, legend = '1/x', lty = 1, title = 'Legend Title') l $rect $rect$w [1] 0.2349272 $rect$h [1] 0.2727899 $rect$left [1] 0.7618728 $rect$top [1] 1.9936 $text $text$x [1] 0.9188374 $text$y [1] 1.81174 R.Version() $platform [1] i686-redhat-linux-gnu $arch [1] i686 $os [1] linux-gnu $system [1] i686, linux-gnu $status [1] $major [1] 2 $minor [1] 2.1 $year [1] 2005 $month [1] 12 $day [1] 20 $svn rev [1] 36812 $language [1] R __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend titles in log plots broken? (ver. 2.2.1)
Rob Steele wrote: Legend titles work in linear plots: curve(1/x, xlim = c(0, 1)) legend(x = 'topright', inset = 0.04, legend = '1/x', lty = 1, title = 'Legend Title') But when you change to a log plot on either dimension things get screwy: curve(1/x, xlim = c(0, 1), log = 'y') legend(x = 'topright', inset = 0.04, legend = '1/x', lty = 1, title = 'Legend Title') If you save the value legend() returns you can look at it and see that it's messed up: l - legend(x = 'topright', inset = 0.04, legend = '1/x', lty = 1, title = 'Legend Title') I don't think this is the problem. It's simply a little bug in legend() that puts the title in the wrong place when a log scale is used. I'll fix it. Duncan Murdoch l $rect $rect$w [1] 0.2349272 $rect$h [1] 0.2727899 $rect$left [1] 0.7618728 $rect$top [1] 1.9936 $text $text$x [1] 0.9188374 $text$y [1] 1.81174 R.Version() $platform [1] i686-redhat-linux-gnu $arch [1] i686 $os [1] linux-gnu $system [1] i686, linux-gnu $status [1] $major [1] 2 $minor [1] 2.1 $year [1] 2005 $month [1] 12 $day [1] 20 $svn rev [1] 36812 $language [1] R __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend in the outer margin
Prasanna wrote: Dear Rs I have a 3x3 multiple plot. I would like to have a overall legend in the outer right margin. From the help archive, I found that it can be done by setting par(xpd=NA). However, I couldn't find the correct values for x and y co-ordinates for the legend. Please find the code snippet below: par(mfrow=c(3,3), mar=c(4,4,0.9,0.5), oma=c(1,2,2,4),cex.main=1.1) *postscript(*file=epsfile,onefile=FALSE,horizontal=TRUE*)* /* some plotting */ par(xpd=NA) You get the user coordinates of the plotting region by par(usr) Now simply make the legend right of that plotting region, e.g. with x corrdinates at par(usr)[2] + epsilon and y coordinates at mean(par(usr)[3:4]) Uwe Ligges legend(legend=c(2h-opt Exact,1-shift Exact,2p-opt Exact), lty=c(solid,dashed,dotdash),lwd=c(2,2,2),col=c(red,green,black), bty=n,cex=0.8) Thanks in advance Prasanna [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend in the outer margin
Uwe Ligges ligges at statistik.uni-dortmund.de writes: You get the user coordinates of the plotting region by par(usr) Now simply make the legend right of that plotting region, e.g. with x corrdinates at par(usr)[2] + epsilon and y coordinates at mean(par(usr)[3:4]) I always found it ugly that this depends on the last plotted figure in an array, but I wanted to position my legend independent of it at an absolute position in device space. What's the best way to achieve this? par(mfrow=c(3,3), mar=c(4,4,0.9,0.5), oma=c(5,2,5,10),cex.main=1.1) n=7 # Legend should be positioned independent of n for (i in 1:n){ plot(rnorm(20),ylim=c(-3,3)) } # ... just the idea reset_to_01_coordinates() par(xpd=NA) leg = legend(0.9,0.5, # Should be seen as absolute in 0/1 coords c(2h-opt Exact,1-shift Exact,2p-opt Exact), lty=c(solid,dashed,dotdash),lwd=c(2,2,2), col=c(red,green,black), bty=n,cex=0.8) Dieter __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend in the outer margin
The cnvrt.coords function in the TeachingDemos package may be of help. Here is an example of possible use (just change the .9 and .7 to where ever on the page you want the legend): par(mfrow=c(3,3), mar=c(4,4,0.9,0.5), oma=c(1,2,2,4),cex.main=1.1) for (i in 1:9){ x - runif(25,1,10) y - 3+ i*x + rnorm(25) plot(x,y) } par(xpd=NA) tmp - cnvrt.coords(.9,.7, 'tdev')$usr legend(tmp,legend=c(2h-opt Exact,1-shift Exact,2p-opt Exact), lty=c(solid,dashed,dotdash),lwd=c(2,2,2),col=c(red,green,blac k), bty=n,cex=0.8) -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare [EMAIL PROTECTED] (801) 408-8111 -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Dieter Menne Sent: Monday, April 10, 2006 7:40 AM To: r-help@stat.math.ethz.ch Subject: Re: [R] Legend in the outer margin Uwe Ligges ligges at statistik.uni-dortmund.de writes: You get the user coordinates of the plotting region by par(usr) Now simply make the legend right of that plotting region, e.g. with x corrdinates at par(usr)[2] + epsilon and y coordinates at mean(par(usr)[3:4]) I always found it ugly that this depends on the last plotted figure in an array, but I wanted to position my legend independent of it at an absolute position in device space. What's the best way to achieve this? par(mfrow=c(3,3), mar=c(4,4,0.9,0.5), oma=c(5,2,5,10),cex.main=1.1) n=7 # Legend should be positioned independent of n for (i in 1:n){ plot(rnorm(20),ylim=c(-3,3)) } # ... just the idea reset_to_01_coordinates() par(xpd=NA) leg = legend(0.9,0.5, # Should be seen as absolute in 0/1 coords c(2h-opt Exact,1-shift Exact,2p-opt Exact), lty=c(solid,dashed,dotdash),lwd=c(2,2,2), col=c(red,green,black), bty=n,cex=0.8) Dieter __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend in bubble plots made with symbols()
Denis Chabot wrote: Hi, I have read about the use of symbols() to draw circles of different sizes, but I have not been able to find out how to add a legend to such a graph, legend that would display some specific sizes and their meaning. Before finding the symbols function in Paul Murrell's book, I had rolled by own function where the variable I want to use to control circle size was actually used to control cex. I was able to draw a legend afterward. Symbols seems a bit simpler and I wanted to see if it would be better than my own function. But without legend it is less useful. However I'm sure there is a way which I'm not aware of to draw a legend for a plot drawn with symbols()... Thanks in advance, Denis Chabot library(Hmisc) ?xYplot See the size argument and the use of the skey function that is generated by xYplot. Frank __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend in bubble plots made with symbols()
On Sat, 18 Mar 2006, Denis Chabot wrote: Hi, I have read about the use of symbols() to draw circles of different sizes, but I have not been able to find out how to add a legend to such a graph, legend that would display some specific sizes and their meaning. Before finding the symbols function in Paul Murrell's book, I had rolled by own function where the variable I want to use to control circle size was actually used to control cex. I was able to draw a legend afterward. Symbols seems a bit simpler and I wanted to see if it would be better than my own function. But without legend it is less useful. However I'm sure there is a way which I'm not aware of to draw a legend for a plot drawn with symbols()... There is a recent paper in JSS by Susumu Tanimura, Chusi Kuroiwa, and Tsutomu Mizota, including some legend code: http://www.jstatsoft.org/ Volume 15, 2006, Issue 5 Thanks in advance, Denis Chabot __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Roger Bivand Economic Geography Section, Department of Economics, Norwegian School of Economics and Business Administration, Helleveien 30, N-5045 Bergen, Norway. voice: +47 55 95 93 55; fax +47 55 95 95 43 e-mail: [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend Outside Plot Dimension
Hi I think you need to use par(xpd=TRUE). Try to search archives as similar question was answered few days ago. HTH Petr On 19 Jan 2006 at 12:19, Abd Rahman Kassim wrote: From: Abd Rahman Kassim [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Date sent: Thu, 19 Jan 2006 12:19:30 -0800 Subject:[R] Legend Outside Plot Dimension Dear All, I'm trying to attach a legend outside the plot (Inside plot OK), but failed. Any help is very much appreciated. Thanks. Abd. Rahman Kassim, PhD Forest Management Ecology Program Forestry Conservation Division Forest Research Institute Malaysia Kepong 52109 Selangor MALAYSIA * Checked by TrendMicro Interscan Messaging Security. For any enquiries, please contact FRIM IT Department. * [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Petr Pikal [EMAIL PROTECTED] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend Outside Plot Dimension
Dear Peter, Thanks for your promt response. Abd. Rahman - Original Message - From: Petr Pikal [EMAIL PROTECTED] To: Abd Rahman Kassim [EMAIL PROTECTED]; r-help@stat.math.ethz.ch Sent: Thursday, January 19, 2006 12:11 AM Subject: Re: [R] Legend Outside Plot Dimension Hi I think you need to use par(xpd=TRUE). Try to search archives as similar question was answered few days ago. HTH Petr On 19 Jan 2006 at 12:19, Abd Rahman Kassim wrote: From: Abd Rahman Kassim [EMAIL PROTECTED] To: r-help@stat.math.ethz.ch Date sent: Thu, 19 Jan 2006 12:19:30 -0800 Subject:[R] Legend Outside Plot Dimension Dear All, I'm trying to attach a legend outside the plot (Inside plot OK), but failed. Any help is very much appreciated. Thanks. Abd. Rahman Kassim, PhD Forest Management Ecology Program Forestry Conservation Division Forest Research Institute Malaysia Kepong 52109 Selangor MALAYSIA * Checked by TrendMicro Interscan Messaging Security. For any enquiries, please contact FRIM IT Department. * [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Petr Pikal [EMAIL PROTECTED] * Outgoing mail is certified Virus Free. Checked by TrendMicro Interscan Messaging Security. For any enquiries, please contact FRIM IT Department. * __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend Outside Plot Dimension
use xpd argument in par(), as follows: ?par par(xpd=T, mar=par()$mar+c(0,0,0,4)) plot(1,1) legend(1.5,1,point,pch=1) Abd Rahman Kassim a écrit : Dear All, I'm trying to attach a legend outside the plot (Inside plot OK), but failed. Any help is very much appreciated. Thanks. Abd. Rahman Kassim, PhD Forest Management Ecology Program Forestry Conservation Division Forest Research Institute Malaysia Kepong 52109 Selangor MALAYSIA * Checked by TrendMicro Interscan Messaging Security. For any enquiries, please contact FRIM IT Department. * [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend Outside Plot Dimension
Dear Jacques, Thanks for the promt response. Abd. Rahman - Original Message - From: Jacques VESLOT [EMAIL PROTECTED] To: Abd Rahman Kassim [EMAIL PROTECTED] Cc: r-help@stat.math.ethz.ch Sent: Wednesday, January 18, 2006 9:15 PM Subject: Re: [R] Legend Outside Plot Dimension use xpd argument in par(), as follows: ?par par(xpd=T, mar=par()$mar+c(0,0,0,4)) plot(1,1) legend(1.5,1,point,pch=1) Abd Rahman Kassim a écrit : Dear All, I'm trying to attach a legend outside the plot (Inside plot OK), but failed. Any help is very much appreciated. Thanks. Abd. Rahman Kassim, PhD Forest Management Ecology Program Forestry Conservation Division Forest Research Institute Malaysia Kepong 52109 Selangor MALAYSIA * Checked by TrendMicro Interscan Messaging Security. For any enquiries, please contact FRIM IT Department. * [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html * Checked by TrendMicro Interscan Messaging Security. For any enquiries, please contact FRIM IT Department. * * Checked by TrendMicro Interscan Messaging Security. For any enquiries, please contact FRIM IT Department. __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend
Mark Miller wrote: I use the following to plot two graphs over each other and then insert a legend, but the two items in the legend both come up the same colour x = seq(0,30,0.01) plot(ecdf(complete), do.point=FALSE, main = 'Cummlative Plot of Monday IATs for Data and\n Fitted PDF over Entire 15 Weeks') lines(x, pexp(x,0.415694806),col=red) legend(x=5,y=0.2 , legend=c(Data Set,Fitted PDF),col=c(black,red)) Many thanks Mark Miller Hi, Mark, You want to use text.col in legend instead of col: set.seed(1) z - rexp(30, 0.415694806) x - seq(0, 30, 0.1) plot(ecdf(z), do.point = FALSE) lines(x, pexp(x, 0.415694806), col=red) legend(x = 5, y = 0.2, legend = c(Data Set, Fitted PDF), text.col = c(black, red)) --sundar __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend
And you want to have different colored lines but black texts, try legend(x = 5, y = 0.2, legend = c(Data Set, Fitted PDF), col = c(black, red), lty=1) The advantage of this is that you can use dotted (lty option) or lines with different weights (lwd option). Regards, Adai On Sun, 2005-11-13 at 06:46 -0600, Sundar Dorai-Raj wrote: Mark Miller wrote: I use the following to plot two graphs over each other and then insert a legend, but the two items in the legend both come up the same colour x = seq(0,30,0.01) plot(ecdf(complete), do.point=FALSE, main = 'Cummlative Plot of Monday IATs for Data and\n Fitted PDF over Entire 15 Weeks') lines(x, pexp(x,0.415694806),col=red) legend(x=5,y=0.2 , legend=c(Data Set,Fitted PDF),col=c(black,red)) Many thanks Mark Miller Hi, Mark, You want to use text.col in legend instead of col: set.seed(1) z - rexp(30, 0.415694806) x - seq(0, 30, 0.1) plot(ecdf(z), do.point = FALSE) lines(x, pexp(x, 0.415694806), col=red) legend(x = 5, y = 0.2, legend = c(Data Set, Fitted PDF), text.col = c(black, red)) --sundar __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend out of Plot Region
Le 24.09.2005 20:22, Michel Friesenhahn a écrit : Hi, Could someone tell me how to place a legend outside the plot region? Thanks, Mike Hi Mike, Take a look at : R par(xpd=NA) -- visit the R Graph Gallery : http://addictedtor.free.fr/graphiques ~ ~~ Romain FRANCOIS - http://addictedtor.free.fr ~~ Etudiant ISUP - CS3 - Industrie et Services ~~http://www.isup.cicrp.jussieu.fr/ ~~ Stagiaire INRIA Futurs - Equipe SELECT ~~ http://www.inria.fr/recherche/equipes/select.fr.html~~ ~ __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend
Thomas Steiner wrote: I color some area grey with polygon() (with a red border) and then I want to have the dashed red border in the legend as well. How do I manage it? And I want to mix (latex) expressions with text in my legend. Both points are not that easy to solve, hence I'd like to suggest to write your own little function that generates the legend. Starting at the upper left, calculating the stringheight, painting the (party very special) symbols, and adding the text line by line seems to be the most easiest solution here (which is not that nice, though. Uwe Ligges Just execute my lines below and you know want I mean. Or pass by at http://de.wikipedia.org/wiki/Bild:GBM.png to see the picture online. Thomas bm - function(n=500, from=0, to=1) { x=seq(from=from,to=to,length=n) BM-c(0,cumsum(rnorm(n-1,mean=0,sd=sqrt(to/n cbind(x,BM) } gbm - function(bm,S0=1,sigma=0.1,mu=1) { gbm=S0 for (t in 2:length(bm[,1])) { gbm[t]=S0*exp((mu-sigma^2/2)*bm[t,1]+sigma*bm[t,2]) } cbind(bm[,1],gbm) } set.seed(9826064) cs=c(dark green, steelblue, red, yellow) #png(filename = GBM.png, width=1600, height=1200, pointsize = 12) par(bg=lightgrey) x=seq(from=0,to=1,length=500) plot(x=x, y=exp(0.7*x), type=n, xlab=Zeit, ylab=, ylim=c(1,3.5)) polygon(x=c(x,rev(x)), y=c(exp(0.7*x)+0.4*sqrt(x),rev(exp(0.7*x)-0.4*sqrt(x))), col=grey, border=cs[3], lty=dashed) lines(x=x,y=exp(0.7*x), type=l, lwd=3, col=cs[1]) lines(gbm(bm(),S0=1,mu=0.7,sigma=0.4), lwd=3, col=cs[2]) lines(gbm(bm(),S0=1,mu=0.7,sigma=0.2), lwd=3, col=cs[3]) lines(gbm(bm(),S0=1,mu=0.7,sigma=0.1), lwd=3, col=cs[4]) title(main=Geometrische Brownsche Bewegung,cex.main=2.5) legend(x=0,y=3.5,legend=c(exp(0.7x),mu=0.7, sigma=0.4,mu=0.7, sigma=0.2,mu=0.7, sigma=0.1,Standardabweichung für sigma=0.2),lwd=c(4,4,4,4,12),col=c(cs,grey),bg=transparent,cex=1.15) #dev.off() __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend
Hi Uwe Ligges wrote: Thomas Steiner wrote: I color some area grey with polygon() (with a red border) and then I want to have the dashed red border in the legend as well. How do I manage it? And I want to mix (latex) expressions with text in my legend. Both points are not that easy to solve, hence I'd like to suggest to write your own little function that generates the legend. Starting at the upper left, calculating the stringheight, painting the (party very special) symbols, and adding the text line by line seems to be the most easiest solution here (which is not that nice, though. I don't think it's too bad. For example, try replacing the original ... legend(x=0,y=3.5,legend=c(exp(0.7x),mu=0.7, sigma=0.4,mu=0.7, sigma=0.2,mu=0.7, sigma=0.1,Standardabweichung für sigma=0.2),lwd=c(4,4,4,4,12),col=c(cs,grey),bg=transparent,cex=1.15) ... with ... # Use grid and gridBase so you've got some sensible # coordinate systems to work within library(grid) library(gridBase) # Align a grid viewport with the plotting region vps - baseViewports() pushViewport(vps$inner, vps$figure, vps$plot) # Define labels and colours # Labels are mathematical expressions labels - expression(exp(0.7x), list(mu == 0.7,sigma == 0.4), list(mu == 0.7,sigma == 0.2), list(mu == 0.7, sigma == 0.1), paste(Standardabweichung für ,sigma == 0.2)) cols - cs # Draw each legend item on its own line # Top line 1cm in from top-left corner for (i in 1:5) { x - unit(1, cm) y - unit(1, npc) - unit(1, cm) - unit(i, lines) if (i 5) { grid.lines(unit.c(x, unit(2, cm)), y + unit(0.5, lines), gp=gpar(col=cols[i], lwd=3)) } else { grid.rect(x, y, width=unit(1, cm), height=unit(1, lines), gp=gpar(fill=grey, col=cs[3], lty=dashed), just=c(left, bottom)) } grid.text(labels[i], x + unit(1.5, cm), y, just=c(left, bottom)) } # clean up popViewport(3) ... that's a bit of typing, but if you need to do more than one, it would go inside a function with labels and cols as arguments (and '5' replaced by 'length(labels)') without too much trouble. (In this case, you could also pretty easily just do the main plot using grid and avoid having to use gridBase.) Paul Just execute my lines below and you know want I mean. Or pass by at http://de.wikipedia.org/wiki/Bild:GBM.png to see the picture online. Thomas bm - function(n=500, from=0, to=1) { x=seq(from=from,to=to,length=n) BM-c(0,cumsum(rnorm(n-1,mean=0,sd=sqrt(to/n cbind(x,BM) } gbm - function(bm,S0=1,sigma=0.1,mu=1) { gbm=S0 for (t in 2:length(bm[,1])) { gbm[t]=S0*exp((mu-sigma^2/2)*bm[t,1]+sigma*bm[t,2]) } cbind(bm[,1],gbm) } set.seed(9826064) cs=c(dark green, steelblue, red, yellow) #png(filename = GBM.png, width=1600, height=1200, pointsize = 12) par(bg=lightgrey) x=seq(from=0,to=1,length=500) plot(x=x, y=exp(0.7*x), type=n, xlab=Zeit, ylab=, ylim=c(1,3.5)) polygon(x=c(x,rev(x)), y=c(exp(0.7*x)+0.4*sqrt(x),rev(exp(0.7*x)-0.4*sqrt(x))), col=grey, border=cs[3], lty=dashed) lines(x=x,y=exp(0.7*x), type=l, lwd=3, col=cs[1]) lines(gbm(bm(),S0=1,mu=0.7,sigma=0.4), lwd=3, col=cs[2]) lines(gbm(bm(),S0=1,mu=0.7,sigma=0.2), lwd=3, col=cs[3]) lines(gbm(bm(),S0=1,mu=0.7,sigma=0.1), lwd=3, col=cs[4]) title(main=Geometrische Brownsche Bewegung,cex.main=2.5) legend(x=0,y=3.5,legend=c(exp(0.7x),mu=0.7, sigma=0.4,mu=0.7, sigma=0.2,mu=0.7, sigma=0.1,Standardabweichung für sigma=0.2),lwd=c(4,4,4,4,12),col=c(cs,grey),bg=transparent,cex=1.15) #dev.off() __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 [EMAIL PROTECTED] http://www.stat.auckland.ac.nz/~paul/ __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend as a subtitle
[EMAIL PROTECTED] wrote: Very little space is available in one of my plots for the legend. I would like to lift it out of the main plot area and present it in the subtitle area. Would appreciate any help that I can get. Look at the following code and read the corresponding help pages: plot(1:10, xlab=) ## clipping to device region rather than plot region: par(xpd=NA) legend(mean(par(usr)[1:2]), 0, legend=nonsense, xjust=0.5) Uwe Ligges Ravi Vishnu This message is meant for the addressee only and may contain confidential and legally privileged information. Any unauthorised review, use, copying, storage, disclosure or distribution of this e- mail and any attachments is strictly prohibited. If you are not the named recipient or have otherwise received this communication in error, please destroy this message from your system and kindly notify the sender by e-mail. Thank you for your co-operation. [[alternative HTML version deleted]] __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend(): how to put variable in subscript?
Aleksey Naumov [EMAIL PROTECTED] writes: Dear List, I would like to plot a simple legend with two math expressions, e.g. plot(0) legend(1, 0.5, expression(sigma[i], sigma[j])) The difficulty is that i and j should be variables rather than strings i and j. In other words I'd like to do something like: i = A j = B legend(1, 0.5, expression(sigma[i], sigma[j])) and have A and B as the actual subscripts. I can substitute the variable in the expression e.g.: legend(1, 0.5, substitute(sigma[i], list(i='A', j='B'))) legend(1, 0.5, bquote(sigma[.(i)])) however, this gives me just one of the two entries in the legend. I cannot figure out how to include both sigmas in the legend. What would be the best way to do something like this? Thank you for your ideas or suggestions. Ick. One of those cases that suggests that our current substitute mechanisms don't quite cut it... However, try i - A; j - B e - bquote(expression(sigma[.(i)],sigma[.(j)])) plot(0) legend(1,.5,eval(e)) legend(1,-.5,e) # for comparison Thing is, substitute(expression(),...) returns a call to the expression constructor, rather than the expression itself, so you need the eval(). -- O__ Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~ - ([EMAIL PROTECTED]) FAX: (+45) 35327907 __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend in xyplot two columns
On Thursday 14 April 2005 05:30, Gesmann, Markus wrote: Dear R-Help I have some trouble to set the legend in a xyplot into two rows. The code below gives me the legend in the layout I am looking for, I just rather have it in two rows. library(lattice) schluessel - list( points=list( col=red, pch=19, cex=0.5 ), text=list(lab=John), lines=list(col=blue), text=list(lab=Paul), lines=list(col=green), text=list(lab=George), lines=list(col=orange), text=list(lab=Ringo), rectangles = list(col= #CC, border=FALSE), text=list(lab=The Beatles), ) xyplot(1~1, key=schluessel) The next code gives me two rows, but repeates all the points,lines, and rectangles. schluessel2 - list( points=list( col=red, pch=19, cex=0.5 ), lines=list(col=c(blue, green, orange)), rectangles = list(col= #CC, border=FALSE), text=list(lab=c(John,Paul,George,Ringo, The Beatles)), columns=3, ) xyplot(1~1, key=schluessel2) So I think each list has to have 6 items, but some with no content. How do I do this? You could try using col=transparent to suppress things, but that's not a very satisfactory solution. The function to create the key is simply not designed to create unstructured legends like this. However, you can create an use an arbitrary ``grob'' (grid graphics object) for a legend, e.g.: ##- library(grid) library(lattice) fl - grid.layout(nrow = 2, ncol = 6, heights = unit(rep(1, 2), lines), widths = unit(c(2, 1, 2, 1, 2, 1), c(cm, strwidth, cm, strwidth, cm, strwidth), data = list(NULL, John, NULL, George, NULL, The Beatles))) foo - frameGrob(layout = fl) foo - placeGrob(foo, pointsGrob(.5, .5, pch=19, gp = gpar(col=red, cex=0.5)), row = 1, col = 1) foo - placeGrob(foo, linesGrob(c(0.2, 0.8), c(.5, .5), gp = gpar(col=blue)), row = 2, col = 1) foo - placeGrob(foo, linesGrob(c(0.2, 0.8), c(.5, .5), gp = gpar(col=green)), row = 1, col = 3) foo - placeGrob(foo, linesGrob(c(0.2, 0.8), c(.5, .5), gp = gpar(col=orange)), row = 2, col = 3) foo - placeGrob(foo, rectGrob(width = 0.6, gp = gpar(col=#CC, fill = #CC)), row = 1, col = 5) foo - placeGrob(foo, textGrob(lab = John), row = 1, col = 2) foo - placeGrob(foo, textGrob(lab = Paul), row = 2, col = 2) foo - placeGrob(foo, textGrob(lab = George), row = 1, col = 4) foo - placeGrob(foo, textGrob(lab = Ringo), row = 2, col = 4) foo - placeGrob(foo, textGrob(lab = The Beatles), row = 1, col = 6) xyplot(1 ~ 1, legend = list(top = list(fun = foo))) ##- HTH, Deepayan __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend in xyplot two columns
Thanks Deepayan! Your solution does excatly what I want. Further experiments and thoughts on my side brought me also to a solution. If I use the option rep=FALSE, and plot the bullit with lines and split the lines argument into two groups it gives me the same result, as every item in the key list starts a new column. library(lattice) key - list( rep=FALSE, lines=list(col=c(red, blue), type=c(p,l), pch=19), text=list(lab=c(John,Paul)), lines=list(col=c(green, red), type=c(l, l)), text=list(lab=c(George,Ringo)), rectangles = list(col= #CC, border=FALSE), text=list(lab=The Beatles), ) xyplot(1~1, key=key) But your solution is much more felxible! Kind Regards Markus -Original Message- LNSCNTMCS01*** The information in this E-Mail and in any attachments is CONFIDENTIAL and may be privileged. If you are NOT the intended recipient, please destroy this message and notify the sender immediately. You should NOT retain, copy or use this E-mail for any purpose, nor disclose all or any part of its contents to any other person or persons. Any views expressed in this message are those of the individual sender, EXCEPT where the sender specifically states them to be the views of Lloyd's. Lloyd's may monitor the content of E-mails sent and received via its network for viruses or unauthorised use and for other lawful business purposes. Lloyd's is authorised under the Financial Services and Markets Act 2000 From: Deepayan Sarkar [mailto:[EMAIL PROTECTED] Sent: 14 April 2005 16:01 To: r-help@stat.math.ethz.ch Cc: Gesmann, Markus Subject: Re: [R] Legend in xyplot two columns On Thursday 14 April 2005 05:30, Gesmann, Markus wrote: Dear R-Help I have some trouble to set the legend in a xyplot into two rows. The code below gives me the legend in the layout I am looking for, I just rather have it in two rows. library(lattice) schluessel - list( points=list( col=red, pch=19, cex=0.5 ), text=list(lab=John), lines=list(col=blue), text=list(lab=Paul), lines=list(col=green), text=list(lab=George), lines=list(col=orange), text=list(lab=Ringo), rectangles = list(col= #CC, border=FALSE), text=list(lab=The Beatles), ) xyplot(1~1, key=schluessel) The next code gives me two rows, but repeates all the points,lines, and rectangles. schluessel2 - list( points=list( col=red, pch=19, cex=0.5 ), lines=list(col=c(blue, green, orange)), rectangles = list(col= #CC, border=FALSE), text=list(lab=c(John,Paul,George,Ringo, The Beatles)), columns=3, ) xyplot(1~1, key=schluessel2) So I think each list has to have 6 items, but some with no content. How do I do this? You could try using col=transparent to suppress things, but that's not a very satisfactory solution. The function to create the key is simply not designed to create unstructured legends like this. However, you can create an use an arbitrary ``grob'' (grid graphics object) for a legend, e.g.: ##- library(grid) library(lattice) fl - grid.layout(nrow = 2, ncol = 6, heights = unit(rep(1, 2), lines), widths = unit(c(2, 1, 2, 1, 2, 1), c(cm, strwidth, cm, strwidth, cm, strwidth), data = list(NULL, John, NULL, George, NULL, The Beatles))) foo - frameGrob(layout = fl) foo - placeGrob(foo, pointsGrob(.5, .5, pch=19, gp = gpar(col=red, cex=0.5)), row = 1, col = 1) foo - placeGrob(foo, linesGrob(c(0.2, 0.8), c(.5, .5), gp = gpar(col=blue)), row = 2, col = 1) foo - placeGrob(foo, linesGrob(c(0.2, 0.8), c(.5, .5), gp = gpar(col=green)), row = 1, col = 3) foo - placeGrob(foo, linesGrob(c(0.2, 0.8), c(.5, .5), gp = gpar(col=orange)), row = 2, col = 3) foo - placeGrob(foo, rectGrob(width = 0.6, gp = gpar(col=#CC, fill = #CC)), row = 1, col = 5) foo - placeGrob(foo, textGrob(lab = John), row = 1, col = 2) foo - placeGrob(foo, textGrob(lab = Paul), row = 2, col = 2) foo - placeGrob(foo, textGrob(lab = George), row = 1, col = 4) foo - placeGrob(foo
Re: [R] Legend in xyplot two columns
On Thursday 14 April 2005 10:29, Gesmann, Markus wrote: Thanks Deepayan! Your solution does excatly what I want. Further experiments and thoughts on my side brought me also to a solution. If I use the option rep=FALSE, and plot the bullit with lines and split the lines argument into two groups it gives me the same result, as every item in the key list starts a new column. Of course. I'd forgotten that 'lines' can also be points. Deepayan __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend positioning in scaled survival plot
Rachel Pearce wrote: I am sorry that this is another novice question. I am having trouble using legend with the survival curve plot from the survival package, and I wonder if it is because I have rescaled my plot. Here is the relevant segment of code: plot(survfit(Surv(OS,Status)~shortishcr1),main='Overall Survival by factor', + xlab='Years',ylab='% surviving',lty=c(1,2),xscale=365.25,yscale=100) legend(5,80,c('Factor=1','Factor=2'),lty=c(1,2)) Here the variable OS is in days, but I want to plot it in years, so I scale it; likewise y is scaled to a percentage. I am trying to position the legend in the rescaled x and y values. Legend returns no error, but no legend appears on the plot. Ask for the real coordinatre system using par(usr) and place the legend somewhere appropriate within these user coordinates. Uwe Ligges If I exclude the scaling altogether: plot(survfit(Surv(OS,Status)~shortishcr1),main='Overall Survival by factor', + xlab='Years',ylab='% surviving',lty=c(1,2),) legend(1825,.8,c('Factor=1','Factor=2'),lty=c(1,2)) then the legend appears exactly as expected. Using the unscaled version of the legend call with the scaled plot, however, again no legend appears but no error is returned. I suspect I am making some elementary mistake, but I just can't see it. It is so elementary that I can't find a similar question in the archives. Can someone help? Here is my version information: _ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major2 minor0.1 year 2004 month11 day 15 language R Rachel __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend Line Size
On Mon, 2005-03-14 at 15:55 -0500, McGehee, Robert wrote: Hello all, When I view or print the below plot on my Linux machine under R 2.0.1 I see a nice thick solid and dashed line with a legend. However, while the lines are distinguishable, the legend is not. That is, the short (solid) line next to line1 and the short (dashed) line next to line2 seem to have the exact same length. What I would like to do is to expand the legend line a bit farther so that the user can clearly see a solid vs. a dashed line and not too small lines that look the same. A glance at the legend source code shows that the line segment length (seg.len) seems to be hard-coded as 2. If I change this to a larger number within the code, I get the effect that I want (although the box around the legend needs to be resized). Am I overlooking a more obvious way to distinguish the legend lines, or would it make more sense to patch the legend function to fit my needs? x - 1:10 plot(x, x, type = l, lty = 1, lwd = 4) lines(x, 2*x, type = l, lty = 5, lwd = 4) legend(7, 5, legend = c(line1, line2), lty = c(1, 5), lwd = 4) Robert, I think that this is exhibiting an interaction between the line type and the line width. I have not looked at the low level segments code to see what is going on, but if you try 'lwd = 2' in the call to legend, the dashed line shows up fine. If I try a line width of 3, it seems that this is the point where there is the loss of the dashed line type. There is a difference in the appearance of the line even with a lwd setting of 1 versus 2. I temporarily put up a PDF file at: http://www.MedAnalytics.com/Rplots.pdf The lwd setting in each plot is: 1, 2 3, 4 You can see the progression of the loss of the dashed line type in the lower two plots. It appears as if the length of the first dash increases as the line width increases, rather than just the line width increasing independently. So there is a progressive loss of the second dash. resulting in a single solid line. Not sure if that helps, but if you can stay with 'lwd = 1' for your plot, that should solve the problem. HTH, Marc Schwartz __ R-help@stat.math.ethz.ch mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend help needed
You have not called legend() in your codes below, so we do not know what your problem is. See other comments below. On Mon, 2004-11-15 at 01:08, Sean David Richards wrote: R : Version 1.9.1 Hi, Am having trouble adding a legend to scatterplot. R code is shown below. I have tried various incantations to add a legend (using the legend() function) to the resulting plot but without any success. Looks like it should be simple but I must be missing something. Any pointers would be welcome. Have looked at help(legend) etc. help(legend) provides many nice examples. Here is a simplified one : x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, lty=1, col=1) lines(x, cos(x), type=l, lty=2, col=2) legend(-pi, 1, legend=c(sin, cosine), lty=1:2, col=1:2) Or you can replace the last line with legend(locator(1), legend=c(sin, cosine), lty=1:2, col=1:2) where the legend will be placed on mouse left click. --8-- --- sfiles - c(72_12_12_V.csv , 150_25_15_V.csv, 150_25_20_V.csv, 150_25_25_V.csv, 150_25_40_V.csv, 150_25_60_V.csv, 150_25_90_V.csv, 240_40_40_V.csv) ## process each file in list for (i in 1:length(sfiles)) { data - read.csv(paste(../data/,sfiles[i],sep=)) ## assign columns to some nice names K - data[,8] AN - data[,3] * (data[,2] - data[,4]) ## plot K against AN Please give a simplified example. You do not need to show us all the preprocessing steps. It can be distracting. if ( i == 1) { plot(AN, K, ylim=c(1000,9000), xlim=c(0,1500), xlab=Area above Notch (mm), main=Size Effect Specimens) par(new=TRUE) } else{ plot(AN,K, pch=(i),ylim=c(1000,9000), xlim=c(0,1500), axes=FALSE,xlab=) par(new=TRUE) } } Have you considered points() or lines() here ? You could simplify to plot(0,1000, type=n, xlim=c(0,1500), ylim=c(1000,9000), xlab=Area above Notch (mm), main=Size Effect Speciments) n - length(sfiles) for (i in 1:n) { data - read.csv(paste(../data/,sfiles[i],sep=)) K- data[,8] AN - data[,3] * (data[,2] - data[,4]) points( AN, K, pch=i, col=i ) } legend( 1500, 9000, legend=paste(Data from, sfiles), pch=1:n, col=i ) --8-- --- __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend help needed
Sorry typo. The last line should read legend(1500, 9000, legend=paste(Data from, sfiles), pch=1:n, col=1:n ) ^^^ On Mon, 2004-11-15 at 11:39, Adaikalavan Ramasamy wrote: You have not called legend() in your codes below, so we do not know what your problem is. See other comments below. On Mon, 2004-11-15 at 01:08, Sean David Richards wrote: R : Version 1.9.1 Hi, Am having trouble adding a legend to scatterplot. R code is shown below. I have tried various incantations to add a legend (using the legend() function) to the resulting plot but without any success. Looks like it should be simple but I must be missing something. Any pointers would be welcome. Have looked at help(legend) etc. help(legend) provides many nice examples. Here is a simplified one : x - seq(-pi, pi, len = 65) plot(x, sin(x), type=l, lty=1, col=1) lines(x, cos(x), type=l, lty=2, col=2) legend(-pi, 1, legend=c(sin, cosine), lty=1:2, col=1:2) Or you can replace the last line with legend(locator(1), legend=c(sin, cosine), lty=1:2, col=1:2) where the legend will be placed on mouse left click. --8-- --- sfiles - c(72_12_12_V.csv , 150_25_15_V.csv, 150_25_20_V.csv, 150_25_25_V.csv, 150_25_40_V.csv, 150_25_60_V.csv, 150_25_90_V.csv, 240_40_40_V.csv) ## process each file in list for (i in 1:length(sfiles)) { data - read.csv(paste(../data/,sfiles[i],sep=)) ## assign columns to some nice names K - data[,8] AN - data[,3] * (data[,2] - data[,4]) ## plot K against AN Please give a simplified example. You do not need to show us all the preprocessing steps. It can be distracting. if ( i == 1) { plot(AN, K, ylim=c(1000,9000), xlim=c(0,1500), xlab=Area above Notch (mm), main=Size Effect Specimens) par(new=TRUE) } else{ plot(AN,K, pch=(i),ylim=c(1000,9000), xlim=c(0,1500), axes=FALSE,xlab=) par(new=TRUE) } } Have you considered points() or lines() here ? You could simplify to plot(0,1000, type=n, xlim=c(0,1500), ylim=c(1000,9000), xlab=Area above Notch (mm), main=Size Effect Speciments) n - length(sfiles) for (i in 1:n) { data - read.csv(paste(../data/,sfiles[i],sep=)) K- data[,8] AN - data[,3] * (data[,2] - data[,4]) points( AN, K, pch=i, col=i ) } legend( 1500, 9000, legend=paste(Data from, sfiles), pch=1:n, col=i ) --8-- --- __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Adaikalavan Ramasamy[EMAIL PROTECTED] Centre for Statistics in Medicine http://www.ihs.ox.ac.uk/csm/ Cancer Research UK Tel : 01865 226 677 Old Road Campus, Headington, Oxford Fax : 01865 226 962 __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend help needed
On 15 Nov 2004 at 12:11, Adaikalavan Ramasamy wrote: Have you considered points() or lines() here ? You could simplify to plot(0,1000, type=n, xlim=c(0,1500), ylim=c(1000,9000), xlab=Area above Notch (mm), main=Size Effect Speciments) n - length(sfiles) for (i in 1:n) { data - read.csv(paste(../data/,sfiles[i],sep=)) K- data[,8] AN - data[,3] * (data[,2] - data[,4]) points( AN, K, pch=i, col=i ) } legend( 1500, 9000, legend=paste(Data from, sfiles), pch=1:n, col=i:n) Thanks this got me going on the right track. The code is a lot more concise as well :) Using locator() instead of x,y coord was suggested by Tom and that showed me where my problem was. The legend was being created just not where it would be visible. I found this bit of code in the R-help archives and it makes thing a lot more straightforward when positioning a legend ## set the range of the usr coordinates to x = (0,1), y = (0,1) opar - par(no.readonly=TRUE) par(usr=c(0,1,0,1)) ## add the legend legend(0.75,0.9,sub(.csv,,nfiles), pch=1:length(nfiles), cex=0.7) Cheers -- Sean Richards C-fACS P.O. Box 84, Lincoln University, Canterbury, New Zealand Phone:(64)(3) 325-2811 ext 8636 Email: [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend placement in barplot?
On Wed, 3 Nov 2004, Marc Schwartz wrote: On Wed, 2004-11-03 at 09:55, Dan Bolser wrote: This has been asked before, but all the answers are hidiously complex. The legend.text=TRUE option of barplot is almost exactly what I need, except I need a legend.placement='tl' (top left) option. This option would be in contrast to the default placement which we could call 'tr' (top right). Anyone know how to edit the barplot code to make this change? Could someone like me work out how to do this? Cheers, Dan. Dan, Do not edit the barplot() code. Use the legend() function instead, which enables you to specify the x,y coordinates of the upper left hand corner of the legend box. See ?legend Thing is I need to pass legend the correct groups and correct plotting colors and correct XY position relative to my data. All these things are already known by the barplot function, and used to draw a beautiful legend. The fact that this legend can only appear in the upper right hand corner is surly a bug worthy of changing the code for? A fair number of the questions that you have had regarding graphics are covered in Chapter 12 Graphical Procedures in An Introduction to R: http://cran.r-project.org/doc/manuals/R-intro.pdf which is included with the R installation. Another online resource for some graphics assistance would be R News Volume 3 Number 2 for October 2003, which has an article on R's base graphics in the R Help Desk section: http://cran.r-project.org/doc/Rnews/Rnews_2003-2.pdf Thanks for the links Notwithstanding all of that, searching the r-help archives is yet another terrific online (and free) resource that you _should_ avail yourself of. Quoting me... This has been asked before, but all the answers are hidiously complex. HTH, Marc Schwartz __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend placement in barplot?
On Wed, 3 Nov 2004, Gabor Grothendieck wrote: Dan Bolser dmb at mrc-dunn.cam.ac.uk writes: : : This has been asked before, but all the answers are hidiously complex. : : The : : legend.text=TRUE : : option of barplot is almost exactly what I need, except I need a : : legend.placement='tl' : : (top left) option. This option would be in contrast to the default : placement which we could call 'tr' (top right). : : Anyone know how to edit the barplot code to make this change? Could : someone like me work out how to do this? : : Cheers, : Dan. Check out gplots::smartlegend (in the R 2.0.0 gregmisc bundle). This works great, but like the (smart)legend function, fill=true appears to be giving me only black boxes. Here is what I add.. smartlegend(x=left,y=top, c(PDB,MSD), fill=TRUE, col=c(red,blue) ) The result is two black boxes! I tried swapping the order of the color and fill options, but to the same effect. I got round the problem by using... smartlegend(x=left,y=top, c(PDB,MSD), col=c(red,blue), lwd=5 ) Not quite the same, but good enough. One other thing (while I am generally complaining), the legend dosn't scale correctly as I change the image size with the mouse. All the other aspects of the barplot scale correctly. If I redraw the legend after changing the size it is scaled correctly, suggesting that this problem isn't fundamental, but is a bug in the implementation of legend. __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend placement in barplot?
You can use locator() nested within legend() i.e. plot(YourVariable) legend(locator(1),legend=Your Legend) Once you call this command it will display Your Legend in the place where you left clicked your mouse. Beware that, as described in the documentation ...'locator' is only supported on screen devices such as 'X11','windows' and 'quartz'. On other devices the call will do nothing Altrenativelly you can pass the exact coordinates of the position where you want the legend, instead of using locator i.e. plot(YourVariable) x-list(x=-91.76781, y=46.87375) legend(x,legend=Your Legend) I hope that this helps Francisco From: Gabor Grothendieck [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: [R] Legend placement in barplot? Date: Wed, 3 Nov 2004 18:48:48 + (UTC) Dan Bolser dmb at mrc-dunn.cam.ac.uk writes: : : This has been asked before, but all the answers are hidiously complex. : : The : : legend.text=TRUE : : option of barplot is almost exactly what I need, except I need a : : legend.placement='tl' : : (top left) option. This option would be in contrast to the default : placement which we could call 'tr' (top right). : : Anyone know how to edit the barplot code to make this change? Could : someone like me work out how to do this? : : Cheers, : Dan. Check out gplots::smartlegend (in the R 2.0.0 gregmisc bundle). __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend placement in barplot?
On Fri, 5 Nov 2004, F Z wrote: You can use locator() nested within legend() i.e. plot(YourVariable) legend(locator(1),legend=Your Legend) Once you call this command it will display Your Legend in the place where you left clicked your mouse. Beware that, as described in the documentation ...'locator' is only supported on screen devices such as 'X11','windows' and 'quartz'. On other devices the call will do nothing Altrenativelly you can pass the exact coordinates of the position where you want the legend, instead of using locator i.e. plot(YourVariable) x-list(x=-91.76781, y=46.87375) legend(x,legend=Your Legend) I hope that this helps Francisco Thanks very much for the tips. Basically I want a very flexible solution that lets me punch the numbers in - take a look at the result and then immediatly dump a .ps / .eps / .png format of what I saw. The result should be very quickly 'publication quality' (whatever that is). The problem with locator is that I don't know how to make it work with postscript, and I don't want to find out. I don't want to have to probe my figure for the coordinates every time I change the data in my figure. I am happy saying something like 'oh, top left is bad, lets use top right' - done. smartlegend is almost there, I just think barplot should support exactly the same functionality as smartlegend. This would save me the hassle of creating a new legend every time my data changes, matching up colors and names. I am sure their is a way to code this, but I don't want to write code - at least not code that I have to look at when what I want to see is my data. From: Gabor Grothendieck [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: [R] Legend placement in barplot? Date: Wed, 3 Nov 2004 18:48:48 + (UTC) Dan Bolser dmb at mrc-dunn.cam.ac.uk writes: : : This has been asked before, but all the answers are hidiously complex. : : The : : legend.text=TRUE : : option of barplot is almost exactly what I need, except I need a : : legend.placement='tl' : : (top left) option. This option would be in contrast to the default : placement which we could call 'tr' (top right). : : Anyone know how to edit the barplot code to make this change? Could : someone like me work out how to do this? : : Cheers, : Dan. Check out gplots::smartlegend (in the R 2.0.0 gregmisc bundle). __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend placement in barplot?
On Wed, 2004-11-03 at 09:55, Dan Bolser wrote: This has been asked before, but all the answers are hidiously complex. The legend.text=TRUE option of barplot is almost exactly what I need, except I need a legend.placement='tl' (top left) option. This option would be in contrast to the default placement which we could call 'tr' (top right). Anyone know how to edit the barplot code to make this change? Could someone like me work out how to do this? Cheers, Dan. Dan, Do not edit the barplot() code. Use the legend() function instead, which enables you to specify the x,y coordinates of the upper left hand corner of the legend box. See ?legend A fair number of the questions that you have had regarding graphics are covered in Chapter 12 Graphical Procedures in An Introduction to R: http://cran.r-project.org/doc/manuals/R-intro.pdf which is included with the R installation. Another online resource for some graphics assistance would be R News Volume 3 Number 2 for October 2003, which has an article on R's base graphics in the R Help Desk section: http://cran.r-project.org/doc/Rnews/Rnews_2003-2.pdf Notwithstanding all of that, searching the r-help archives is yet another terrific online (and free) resource that you _should_ avail yourself of. HTH, Marc Schwartz __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend placement in barplot?
Dan Bolser dmb at mrc-dunn.cam.ac.uk writes: : : This has been asked before, but all the answers are hidiously complex. : : The : : legend.text=TRUE : : option of barplot is almost exactly what I need, except I need a : : legend.placement='tl' : : (top left) option. This option would be in contrast to the default : placement which we could call 'tr' (top right). : : Anyone know how to edit the barplot code to make this change? Could : someone like me work out how to do this? : In package gplots (in bundle gregmisc in R 2.0.0) there is smartlegend. __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend placement in barplot?
Dan Bolser dmb at mrc-dunn.cam.ac.uk writes: : : This has been asked before, but all the answers are hidiously complex. : : The : : legend.text=TRUE : : option of barplot is almost exactly what I need, except I need a : : legend.placement='tl' : : (top left) option. This option would be in contrast to the default : placement which we could call 'tr' (top right). : : Anyone know how to edit the barplot code to make this change? Could : someone like me work out how to do this? : : Cheers, : Dan. Check out gplots::smartlegend (in the R 2.0.0 gregmisc bundle). __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend/Substitute/Plotmath problem
Johannes == Johannes Graumann [EMAIL PROTECTED] on Sat, 23 Oct 2004 11:04:25 -0700 writes: Johannes == Johannes Graumann [EMAIL PROTECTED] on Sat, 23 Oct 2004 11:04:25 -0700 writes: Johannes Hello, Johannes I seem unable to construct a legend which contains Johannes a substitution as well as math symbols. I'm trying Johannes to do the following: strain2 - YJG48 legend.txt - c( substitute( strain * %==% * YJG45, rpn10 * %Delta%, list(strain=strain2) ), Verhulst/Logistic, Malthus ) Johannes . Do try to break down a problem into simple things -- particularly when you have problems! This substitute() call is simply invalid: ss - substitute( strain * %==% * YJG45, rpn10 * %Delta%, list(strain=strain2) ) Error: syntax error and the 'syntax error' should give you a clue: The first argument of substitute must be a syntactically correct R expression. Now you try more and more simple things till you 'see it' : Why should I expect 'A * %==% B' to be valid syntax? Both '*' and '%==%' are (diadic) operators: You can't juxtapose them, as well as you can't write 'A * = B'. Then, '%Delta%' (like any other '%foo%' !!) is a diadic operator too and hence can't be juxtaposed to '*'. But I'm pretty sure you rather mean (greek) 'Delta'. Hence: ss - substitute( strain %==% YJG45, rpn10 * Delta, list(strain=strain2) ) --- Once you have the expression you can go further; still step by step : c(ss, Verhulst) [[1]] YJG48 %==% YJG45, rpn10 * Delta [[2]] [1] Verhulst Hmm, a list; that won't work. You do need to pass either a character vector or an expression, i.e., an expression of length 3 in our case. We must build the expression somewhat manually: e - expression(1, Verhulst, Malthus)# '1' is a place holder expression(1, Verhulst, Malthus) e[[1]] - ss ## that's the trick! str(e) expression(YJG48 %==% YJG45, rpn10 * Delta, Verhulst, Malthus) plot(1); legend(1,1, leg = e) --- Maybe something to be added as an example to help(legend) or rather to help(expression) ? HTH, Martin Maechler, ETH Zurich __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend/Substitute/Plotmath problem
Martin Maechler wrote: Johannes == Johannes Graumann [EMAIL PROTECTED] on Sat, 23 Oct 2004 11:04:25 -0700 writes: Johannes == Johannes Graumann [EMAIL PROTECTED] on Sat, 23 Oct 2004 11:04:25 -0700 writes: Johannes Hello, Johannes I seem unable to construct a legend which contains Johannes a substitution as well as math symbols. I'm trying Johannes to do the following: strain2 - YJG48 legend.txt - c( substitute( strain * %==% * YJG45, rpn10 * %Delta%, list(strain=strain2) ), Verhulst/Logistic, Malthus ) Johannes . Do try to break down a problem into simple things -- particularly when you have problems! This substitute() call is simply invalid: ss - substitute( strain * %==% * YJG45, rpn10 * %Delta%, list(strain=strain2) ) Error: syntax error and the 'syntax error' should give you a clue: The first argument of substitute must be a syntactically correct R expression. Now you try more and more simple things till you 'see it' : Why should I expect 'A * %==% B' to be valid syntax? Both '*' and '%==%' are (diadic) operators: You can't juxtapose them, as well as you can't write 'A * = B'. Then, '%Delta%' (like any other '%foo%' !!) is a diadic operator too and hence can't be juxtaposed to '*'. But I'm pretty sure you rather mean (greek) 'Delta'. Hence: ss - substitute( strain %==% YJG45, rpn10 * Delta, list(strain=strain2) ) --- Once you have the expression you can go further; still step by step : c(ss, Verhulst) [[1]] YJG48 %==% YJG45, rpn10 * Delta [[2]] [1] Verhulst Hmm, a list; that won't work. You do need to pass either a character vector or an expression, i.e., an expression of length 3 in our case. We must build the expression somewhat manually: e - expression(1, Verhulst, Malthus)# '1' is a place holder expression(1, Verhulst, Malthus) e[[1]] - ss ## that's the trick! str(e) expression(YJG48 %==% YJG45, rpn10 * Delta, Verhulst, Malthus) plot(1); legend(1,1, leg = e) --- Maybe something to be added as an example to help(legend) or rather to help(expression) ? Martin, a small example is given in the Help Desk in R News 2 (3). Maybe you want to include it ... Uwe HTH, Martin Maechler, ETH Zurich __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend/Substitute/Plotmath problem
Thank you so much ... works now ... sooo much to learn ... Joh On Mon, 25 Oct 2004 09:56:35 +0200 Martin Maechler [EMAIL PROTECTED] wrote: Johannes == Johannes Graumann [EMAIL PROTECTED] on Sat, 23 Oct 2004 11:04:25 -0700 writes: Johannes == Johannes Graumann [EMAIL PROTECTED] on Sat, 23 Oct 2004 11:04:25 -0700 writes: Johannes Hello, Johannes I seem unable to construct a legend which contains Johannes a substitution as well as math symbols. I'm trying Johannes to do the following: strain2 - YJG48 legend.txt - c( substitute( strain * %==% * YJG45, rpn10 * %Delta%, list(strain=strain2) ), Verhulst/Logistic, Malthus ) Johannes . Do try to break down a problem into simple things -- particularly when you have problems! This substitute() call is simply invalid: ss - substitute( strain * %==% * YJG45, rpn10 * %Delta%, list(strain=strain2) ) Error: syntax error and the 'syntax error' should give you a clue: The first argument of substitute must be a syntactically correct R expression. Now you try more and more simple things till you 'see it' : Why should I expect 'A * %==% B' to be valid syntax? Both '*' and '%==%' are (diadic) operators: You can't juxtapose them, as well as you can't write 'A * = B'. Then, '%Delta%' (like any other '%foo%' !!) is a diadic operator too and hence can't be juxtaposed to '*'. But I'm pretty sure you rather mean (greek) 'Delta'. Hence: ss - substitute( strain %==% YJG45, rpn10 * Delta, list(strain=strain2) ) --- Once you have the expression you can go further; still step by step : c(ss, Verhulst) [[1]] YJG48 %==% YJG45, rpn10 * Delta [[2]] [1] Verhulst Hmm, a list; that won't work. You do need to pass either a character vector or an expression, i.e., an expression of length 3 in our case. We must build the expression somewhat manually: e - expression(1, Verhulst, Malthus)# '1' is a place holder expression(1, Verhulst, Malthus) e[[1]] - ss ## that's the trick! str(e) expression(YJG48 %==% YJG45, rpn10 * Delta, Verhulst, Malthus) plot(1); legend(1,1, leg = e) --- Maybe something to be added as an example to help(legend) or rather to help(expression) ? HTH, Martin Maechler, ETH Zurich __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend with different sized symbols
J. Pedro Granadeiro wrote: Dear list, I wonder if it is possible to produce a legend with symbols of different sizes using a single legend command. I managed to do so more or less like in this crude example, but there is probably a smarter and more practical way: set.seed(0) plot(rnorm(100), rnorm(100), cex=rep(1:5,each=20)) x-legend(-2,2.8, legend=1:5, pch= ,y.intersp=2, bty=n) points(x$text$x-.2,x$text$y, cex=1:5) I tried with playing with cex inside the legend command, but this gave me funny results, since it manipulates the overall size of symbols plus text. I was looking for something like pt.cex ... and exactly that one exists in recent versions of R (e.g. R-1.9.1)! Uwe Ligges , but this probably it does not exist... Thanks Jose Pedro Granadeiro __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend under plot region?
Have a look at ?par You need to have par(xpd = TRUE) and then use par(usr) to get the coordinates for the edges of the plot. Also make sure you have enough space around the plot to put the legend. For that, have a look at par(oma) or par(mar) etc. HTH. Partha Jean Eid [EMAIL PROTECTED] Sent by: [EMAIL PROTECTED] 07/30/2004 02:21 PM To: [EMAIL PROTECTED] cc: Subject:[R] legend under plot region? I am trying to put legends underneath the plot (in the outer margins). Is there an easy way to do this. I have been tinkering with split..screen but I could not make it work. Thank in advance Jean __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend
Hi, - Original Message - From: Perez Martin, Agustin [EMAIL PROTECTED] To: lista R help (E-mail) [EMAIL PROTECTED] Sent: Wednesday, June 23, 2004 9:19 PM Subject: [R] legend DeaR UseRs: I want to put a legend in my plot. In the first line of the legend I want to put a box filled but in the second one I would like to put a lty=2 Have you looked at ?lengend It's got some good examples. Kevin __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] legend
Perez Martin, Agustin wrote: DeaR UseRs: I want to put a legend in my plot. In the first line of the legend I want to put a box filled but in the second one I would like to put a lty=2 Of course it must appear with different colors. I think I wrote this function about a year ago for someone, but I couldn't find it anywhere. Here it is again. Jim add.legend.bars-function(legend.info,whichbars,col,border=black) { nelements-length(legend.info$text$y) left-rep(legend.info$rect$left+ 0.1*(legend.info$text$x[1]-legend.info$rect$left),nelements) right-rep(legend.info$rect$left+ 0.8*(legend.info$text$x[1]-legend.info$rect$left),nelements) top-legend.info$text$y+(legend.info$text$y[1]-legend.info$text$y[2])/3 bottom-top-(legend.info$text$y[1]-legend.info$text$y[2])/1.5 rect(left[whichbars],bottom[whichbars], right[whichbars],top[whichbars], col=col,border=border) } __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
RE: [R] Legend outside the plotting region
Dear Miha, Try setting par(xpd=TRUE) or par(xpd=NA). I hope this helps, John -Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Miha STAUT Sent: Monday, May 03, 2004 4:15 PM To: [EMAIL PROTECTED] Subject: [R] Legend outside the plotting region Hello, How would I add a legend to the plot outside the plotting region? I tried different graphical parameters (fig, plt, usr and fig in combination with plt) in par() without success. Thanks in advance, Miha Staut __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend text -- discrepancy between X11 and postscript
Hi! On Wed, Jan 14, 2004 at 11:08:44PM -0800, Itay Furman wrote: When I place a legend on a plot it looks exactly as I intended on the screen. However, almost always, when I export this to postscript file, the legend's text protrudes through the legend's frame (the latter being placed correctly). This routinely happens to me when using dev.copy2eps. If I use a postscript device to begin with everything is fine. cu Philipp -- Dr. Philipp PagelTel. +49-89-3187-3675 Institute for Bioinformatics / MIPS Fax. +49-89-3187-3585 GSF - National Research Center for Environment and Health Ingolstaedter Landstrasse 1 85764 Neuherberg, Germany __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend text -- discrepancy between X11 and postscript
The short answer is not to copy the device, but to replot on the new device. That is the advice given in MASS, for example. When you copy a device, you replay the device list and hence the lines and text are placed at the positions calculated using the font metrics of the first device and not the second. dev.copy2eps does not try to adjust the pointsize of the postscript device, and provided the fonts match you should just be able to adjust the pointsize in this case. You do need to be suspicious of on-screen viewers and indeed of ghostscript, for they are often not pixel-perfect and ghostscript does font substitution (it does not have Helvetica). I would always test by printing on a postscript printer. On Wed, 14 Jan 2004, Itay Furman wrote: Hi, When I place a legend on a plot it looks exactly as I intended on the screen. However, almost always, when I export this to postscript file, the legend's text protrudes through the legend's frame (the latter being placed correctly). See the appended example code. I can send the EPS file as well for those that are interested (4 kb; 200 lines). I found nothing in the FAQS, or in R-intro to enlighten me. I tried few things --- changing font size, setting legend's text width, etc. --- but eventually gave up. How can I get a consistent X11 and PS rendering? (R 1.8.1 on Linux RedHat 9; GhostView 3.5.8) TIA Itay Example for X11-EPS discrepancy in legend rendering ## A useless data to plot x- 0:10; XY - list(x=x, y=2*x) ## Set lims explicitly; use later in placing the legend. xlim - range(XY$x); ylim - range(XY$y) plot(XY, xlim=xlim, ylim=ylim, type=l, lty=1, col=2, axes=FALSE) axis(1); axis(2) ## Legend and plot share the bottom-right corner. legend(xlim[2], ylim[1], A set of random numbers, lty=1, col=2, xjust=1, yjust=0) ## On the screen: OK. Now produce EPS file. dev.copy2eps(file=test.eps, paper=letter) # End example ## -- [EMAIL PROTECTED] __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html -- Brian D. Ripley, [EMAIL PROTECTED] Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UKFax: +44 1865 272595 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
Re: [R] Legend position in barplots
On Sun, 2003-11-16 at 12:42, Andreas Lackner wrote: Hello! Is there a way to change the position of the legend created in a barplot from the right side of the plot to the left side or somewhere else? Thanks Yes...instead of specifying 'legend.text' in barplot, use the legend() function, which will enable you to specify the coordinates and other details for the legend. See ?legend for more information. HTH, Marc Schwartz __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] legend over-prints barplot bar
On Wed, 2003-10-29 at 22:28, Paul Sorenson wrote: When I create a bar plot, the legend is obscuring the rightmost bar. I haven't found a setting that appears to affect the positioning of the legend - any tips re moving the legend would be most appreciated. paul sorenson Conceptually, barplot() sets the default axes and legend position based upon the data that you are using for 'height'. These defaults may not be appropriate in all cases, as you are seeing. There are a couple of approaches that you can take: 1. Explicitly locate the legend by using legend() instead of the default used by barplot(). Don't set 'legend.text' in barplot() in this case. See ?legend for more details. 2. You can adjust the range of the y axis (if you have vertical bars) or the x axis (if you have horizontal bars) by using 'ylim' or 'xlim' in barplot(), respectively. Thus you could do something like: barplot(height, ..., ylim = c(0, max(height) * 1.3)) which will increase the maximum value of the y axis by 30%, leaving room for the legend in the upper portion of the plot area. HTH, Marc Schwartz __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Fwd: Re: [R] legend() with option adj=1
Is there a simpler way then the solution to the one that was posted here? I'm not very proficient with legend, and I don't understand this solution. All I have is two or more lines on one plot that I want to put a legend on and I can't figure out how to do it from the examples. Can you give a very simple example? It does not have to be fancy!! I have never worked with a package where the legend was not automatic. -- Forwarded Message -- Subject: Re: [R] legend() with option adj=1 Date: Wed, 21 May 2003 09:19:11 +0200 From: Uwe Ligges [EMAIL PROTECTED] To: Jerome Asselin [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Jerome Asselin wrote: Hi there, I want to justify to right the text of my legend. Consider this short reproducable example. x - 1:5 y1 - 1/x y2 - 2/x plot(rep(x,2),c(y1,y2),type=n,xlab=x,ylab=y) lines(x,y1) lines(x,y2,lty=2) legend(5,2,c(1,000,1,000,000),lty=1:2,xjust=1,yjust=1) legend(5,1.5,c(1,000,1,000,000),lty=1:2,xjust=1,yjust=1,adj=1) Now, I would like to right-justify the text of the legend. As you can see, the option adj=1 does not give satisfactory results. Is this a bug or is there an easy way that I'm missing? Thanks, Jerome Works, e.g., with the following little trick: x - 1:5 y1 - 1/x y2 - 2/x plot(rep(x,2),c(y1,y2),type=n,xlab=x,ylab=y) lines(x,y1) lines(x,y2,lty=2) temp - legend(5, 2, legend = c( , ), text.width = strwidth(1,000,000), lty = 1:2, xjust = 1, yjust = 1) text(temp$rect$left + temp$rect$w, temp$text$y, c(1,000, 1,000,000), pos=2) See ?legend for details, in particular the returned value. Uwe Ligges __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: Fwd: Re: [R] legend() with option adj=1
Anna H. Pryor wrote: Is there a simpler way then the solution to the one that was posted here? I'm not very proficient with legend, and I don't understand this solution. All I have is two or more lines on one plot that I want to put a legend on and I can't figure out how to do it from the examples. Can you give a very simple example? It does not have to be fancy!! I have never worked with a package where the legend was not automatic. Hmm. The simple solution is to use legend() as is, see ?legend for details. The solution given below was intended for the specific question on right justified legend text. Uwe Ligges -- Forwarded Message -- Subject: Re: [R] legend() with option adj=1 Date: Wed, 21 May 2003 09:19:11 +0200 From: Uwe Ligges [EMAIL PROTECTED] To: Jerome Asselin [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Jerome Asselin wrote: Hi there, I want to justify to right the text of my legend. Consider this short reproducable example. x - 1:5 y1 - 1/x y2 - 2/x plot(rep(x,2),c(y1,y2),type=n,xlab=x,ylab=y) lines(x,y1) lines(x,y2,lty=2) legend(5,2,c(1,000,1,000,000),lty=1:2,xjust=1,yjust=1) legend(5,1.5,c(1,000,1,000,000),lty=1:2,xjust=1,yjust=1,adj=1) Now, I would like to right-justify the text of the legend. As you can see, the option adj=1 does not give satisfactory results. Is this a bug or is there an easy way that I'm missing? Thanks, Jerome Works, e.g., with the following little trick: x - 1:5 y1 - 1/x y2 - 2/x plot(rep(x,2),c(y1,y2),type=n,xlab=x,ylab=y) lines(x,y1) lines(x,y2,lty=2) temp - legend(5, 2, legend = c( , ), text.width = strwidth(1,000,000), lty = 1:2, xjust = 1, yjust = 1) text(temp$rect$left + temp$rect$w, temp$text$y, c(1,000, 1,000,000), pos=2) See ?legend for details, in particular the returned value. Uwe Ligges __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: Fwd: Re: [R] legend() with option adj=1
On Thu, 5 Jun 2003 07:20:11 -0700 Anna H. Pryor [EMAIL PROTECTED] wrote: Is there a simpler way then the solution to the one that was posted here? I'm not very proficient with legend, and I don't understand this solution. All I have is two or more lines on one plot that I want to put a legend on and I can't figure out how to do it from the examples. Can you give a very simple example? It does not have to be fancy!! I have never worked with a package where the legend was not automatic. -- Forwarded Message -- Subject: Re: [R] legend() with option adj=1 Date: Wed, 21 May 2003 09:19:11 +0200 From: Uwe Ligges [EMAIL PROTECTED] To: Jerome Asselin [EMAIL PROTECTED] Cc: [EMAIL PROTECTED] Jerome Asselin wrote: Hi there, I want to justify to right the text of my legend. Consider this short reproducable example. x - 1:5 y1 - 1/x y2 - 2/x plot(rep(x,2),c(y1,y2),type=n,xlab=x,ylab=y) lines(x,y1) lines(x,y2,lty=2) legend(5,2,c(1,000,1,000,000),lty=1:2,xjust=1,yjust=1) legend(5,1.5,c(1,000,1,000,000),lty=1:2,xjust=1,yjust=1,adj=1) . . . Uwe Ligges Here is an alternative to consider. In plots such as this I like to label the curves where they are most separated and avoid legends altogether (as well as usually avoiding the need for different line types, unless curves intertwine): x - 1:5 y - 1/x y2 - 2/x w - list('1/x'=list(x=x,y=y),'2/x'=list(x=x,y=y2)) library(Hmisc) # see http://hesweb1.med.virginia.edu/biostat/s/Hmisc.html labcurve(w, pl=TRUE, offset=.1) Or put a legend in the most empty region of the graph: labcurve(w, pl=TRUE, lty=c(2,1), lwd=c(1,3), col=gray(c(0,.7)), keys='lines', xlab=expression(chi)) # lty only for demonstration - omit that for this example. Thick gray scale # lines are excellent for step functions Or use same line types but put symbols every so often (point.inc= to override default spacing; this works well for overlapping step functions also): labcurve(w, pl=TRUE, keys=1:2) # uses pch=1:2 --- Frank E Harrell Jr Prof. of Biostatistics Statistics Div. of Biostatistics Epidem. Dept. of Health Evaluation Sciences U. Virginia School of Medicine http://hesweb1.med.virginia.edu/biostat __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Legend in plot: symbol for mean and standard deviation
Dear list, I attached to this mail an eps file containing an example that illustrate the problem: the plot display dot+vertical lines while with the legend I am able only to display dot+horizontal line. Any help is appreciate, cheers, marco On Mon, 2003-02-24 at 17:51, Uwe Ligges wrote: Marco Kienzle wrote: Dear list, I am facing the following problem with the legend of a plot that display the mean and variance of a measurement y as a function of x, the mean being represented by a dot and the variance by a vertical line. At least for me the latter does not appear to be that common ... My problem is that I am unable to display the symbol (dot + vertical line) in the legend. any help is welcome, thanks marco Does the following do what you are looking for? legend(..., c(y., y|)) or legend(..., expression(y[.], y[|])) Uwe Ligges -- Marco Kienzle Fisheries Research Services Marine Laboratory PO Box 101 Victoria Road Aberdeen AB119DB United Kingdom direct: +44 (0) 1224 295421 tel:+44 (0) 1224 876544 fax:+44 (0) 1224 295511 http://www.marlab.ac.uk graph.eps Description: PostScript document
Re: [R] Legend in plot: symbol for mean and standard deviation
You can specify that no plot character is drawn using pch = -1. Maybe this is what you want: plot(1:10,1:10) legend(locator(1),c(blah,blahblah,blahblahblah,blah...), pch = c(-1,-1,22,22),lty=c(1,1,0,0),col=c(green,red,blue,blue), pt.bg=c(white,white,yellow,orange)) ~~~ Anne E. York National Marine Mammal Laboratory Seattle WA 98115-0070 USA e-mail: [EMAIL PROTECTED] Voice: +1 206-526-4039 Fax: +1 206-526-6615 ~~~ Date: Mon, 24 Feb 2003 18:51:35 +0100 From: Uwe Ligges [EMAIL PROTECTED] To: Marco Kienzle [EMAIL PROTECTED] CC: [EMAIL PROTECTED] Subject: Re: [R] Legend in plot: symbol for mean and standard deviation Marco Kienzle wrote: Dear list, I am facing the following problem with the legend of a plot that display the mean and variance of a measurement y as a function of x, the mean being represented by a dot and the variance by a vertical line. At least for me the latter does not appear to be that common ... My problem is that I am unable to display the symbol (dot + vertical line) in the legend. any help is welcome, thanks marco Does the following do what you are looking for? legend(..., c(y., y|)) or legend(..., expression(y[.], y[|])) Uwe Ligges __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] Legend in plot: symbol for mean and standard deviation
Marco Kienzle wrote: Dear list, I am facing the following problem with the legend of a plot that display the mean and variance of a measurement y as a function of x, the mean being represented by a dot and the variance by a vertical line. At least for me the latter does not appear to be that common ... My problem is that I am unable to display the symbol (dot + vertical line) in the legend. any help is welcome, thanks marco Does the following do what you are looking for? legend(..., c(y., y|)) or legend(..., expression(y[.], y[|])) Uwe Ligges __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] legend
legend(locator(1), month.abb[1:5], fill=T, col=1:5) gives me 5 black boxes. Try: legend(locator(1), month.abb[1:5], fill=1:5) What am I doing wrong? The T is interpreted as (equal to?) a 1, so you're requesting the boxes to be filled with color 1, which is black. - Hedderik. __ [EMAIL PROTECTED] mailing list http://www.stat.math.ethz.ch/mailman/listinfo/r-help