Jonathan Lees
2007-Feb-26 14:25 UTC
[R] PLotting R graphics/symbols without user x-y scaling
Is it possible to add lines or other user defined graphics to a plot in R that does not depend on the user scale for the plot? For example I have a plot plot(x,y) and I want to add some graphic that is scaled in inches or cm but I do not want the graphic to change when the x-y scales are changed - like a thermometer, scale bar or other symbol - How does one do this? I want to build my own library of glyphs to add to plots but I do not know how to plot them when their size is independent of the device/user coordinates. Is it possible to add to the list of symbols in the function symbols() other than: _circles_, _squares_, _rectangles_, _stars_, _thermometers_, and _boxplots_ can I make my own symbols and have symbols call these? Thanks- -- Jonathan M. Lees Professor THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL Department of Geological Sciences Campus Box #3315 Chapel Hill, NC 27599-3315 TEL: (919) 962-0695 FAX: (919) 966-4519 jonathan.lees at unc.edu http://www.unc.edu/~leesj
Paul Murrell
2007-Feb-26 23:14 UTC
[R] PLotting R graphics/symbols without user x-y scaling
Hi Jonathan Lees wrote:> Is it possible to add lines or other > user defined graphics > to a plot in R that does not depend on > the user scale for the plot? > > For example I have a plot > plot(x,y) > and I want to add some graphic that is > scaled in inches or cm but I do not want the > graphic to change when the x-y scales are > changed - like a thermometer, scale bar or > other symbol - > How does one do this? > > I want to build my own library of glyphs to add to plots > but I do not know how to plot them when their > size is independent of the device/user coordinates. > > Is it possible to add to the list > of symbols in the function symbols() > other than: > _circles_, _squares_, _rectangles_, _stars_, _thermometers_, and > _boxplots_ > > can I make my own symbols and have symbols call these?There is currently no mechanism for defining your own additions to symbols(), but this sort of thing is easily doable using the grid graphics system, and the resulting symbols would be easy to add to lattice plots. See ... http://www.stat.auckland.ac.nz/~paul/RGraphics/chapter4.pdf http://www.stat.auckland.ac.nz/~paul/RGraphics/chapter5.pdf There is also an example of how to do this sort of thing using the grImport package (and grid and lattice) in http://www.stat.auckland.ac.nz/~paul/Talks/import.pdf The complete code for the example is ... library(grImport) hourglass <- new("Picture", paths list(new("PictureFill", x=c(0, 1, 0, 1), y=c(0, 0, 1, 1), rgb="black"), new("PictureStroke", x=c(0, 1, 0, 1, 0), y=c(0, 0, 1, 1, 0), rgb="grey")), summary new("PictureSummary", numPaths=1, xscale=c(0, 1), yscale=c(0, 1))) dotplot(variety ~ yield | year, data=barley, panel=function(x, y, type, ...) { panel.dotplot(x, y, type="n", ...) grid.symbols(hourglass, x=unit(as.numeric(x), "native"), y=unit(as.numeric(y), "native"), size=unit(5, "mm")) }) Paul> Thanks- > >-- Dr Paul Murrell Department of Statistics The University of Auckland Private Bag 92019 Auckland New Zealand 64 9 3737599 x85392 paul at stat.auckland.ac.nz http://www.stat.auckland.ac.nz/~paul/
Gabor Grothendieck
2007-Feb-27 01:00 UTC
[R] PLotting R graphics/symbols without user x-y scaling
You can use par("usr") to get the min/max coords of the plot and then use that. For example, this will plot a red dot in the middle of the plot area regardless of the coordinates: plot(1:10) # sample plot usr <- par("usr") points(mean(usr[1:2]), mean(usr[3:4]), pch = 20, col = "red") # red dot See ?par On 2/26/07, Jonathan Lees <jonathan.lees at unc.edu> wrote:> > Is it possible to add lines or other > user defined graphics > to a plot in R that does not depend on > the user scale for the plot? > > For example I have a plot > plot(x,y) > and I want to add some graphic that is > scaled in inches or cm but I do not want the > graphic to change when the x-y scales are > changed - like a thermometer, scale bar or > other symbol - > How does one do this? > > I want to build my own library of glyphs to add to plots > but I do not know how to plot them when their > size is independent of the device/user coordinates. > > Is it possible to add to the list > of symbols in the function symbols() > other than: > _circles_, _squares_, _rectangles_, _stars_, _thermometers_, and > _boxplots_ > > can I make my own symbols and have symbols call these? > > > Thanks- > > > -- > Jonathan M. Lees > Professor > THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL > Department of Geological Sciences > Campus Box #3315 > Chapel Hill, NC 27599-3315 > TEL: (919) 962-0695 > FAX: (919) 966-4519 > jonathan.lees at unc.edu > http://www.unc.edu/~leesj > > ______________________________________________ > R-help at 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. >
Beyond the other replies that you have received, here are a couple more ideas. The cnvrt.coords function in the TeachingDemos package can aid in switching between the coordinate systems so you could use this to convert your x,y coordinates from the user system to the plot coordinates (0 to 1), add values to these to represent the graphic you want to plot, then convert these coordinates back to user coordinates and use the lines function to do the plotting. You could also do the conversion, then change the user plotting coordinates rather than converting back to user coordinates. You can also use the subplot function in the TeachingDemos package to add small plots to an existing plot (see the examples). Here is a quick stab at a function that uses subplot internally to do similar to symbols, you give it the x and y coordinates where the center of your symbol should be, then the 'symb' argument can either be a matrix with points that define the shape of the symbol, or a function that creates a plot of the symbol. my.symbols <- function(x, y=NULL, symb, inches=1, add=FALSE, xlab=deparse(substitute(x)), ylab=deparse(substitute(y)), main=NULL, xlim=NULL, ylim=NULL, vadj=0.5, hadj=0.5, pars=NULL, ...){ if(!require(TeachingDemos)) stop('The TeachingDemos package is required') if(!add){ plot(x,y, type='n', xlab=xlab,ylab=ylab,xlim=xlim,ylim=ylim) } if(is.function(symb)){ symb2 <- symb } else { symb2 <- function(...){ plot( rbind(symb, symb[1,]), xlab='',ylab='', xaxs='i',yaxs='i', ann=FALSE, axes=FALSE, type='l' ) } } for (i in seq(along=x)){ subplot(symb2(i,...),x[i],y[i], size=c(inches,inches), vadj=vadj, hadj=hadj, pars=pars) } } Here are a couple of quick examples that use the above function: tmp.hex <- rbind( c(0,0), c(0,1), c(1,2), c(2,2), c(2,1), c(1,0) ) my.symbols( runif(10), runif(10), tmp.hex, inches=.25 ) tmp.fun <- function(which, r, ...){ r <- r[which] plot( 0.5, 0.5, xlim=c(0,1), ylim=c(0,1), xaxs='i', yaxs='i', ann=FALSE, axes=FALSE, cex=r) } my.symbols( runif(10), runif(10), tmp.fun, r=sample(1:10) ) I will polish and document this function and add it to the next release of the TeachingDemos package (when I get enough time to put out a new release, hopefully soon, but don't hold your breath). Hope this helps, -- Gregory (Greg) L. Snow Ph.D. Statistical Data Center Intermountain Healthcare greg.snow at intermountainmail.org (801) 408-8111> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Jonathan Lees > Sent: Monday, February 26, 2007 7:26 AM > To: r-help at stat.math.ethz.ch > Subject: [R] PLotting R graphics/symbols without user x-y scaling > > > Is it possible to add lines or other > user defined graphics > to a plot in R that does not depend on > the user scale for the plot? > > For example I have a plot > plot(x,y) > and I want to add some graphic that is > scaled in inches or cm but I do not want the graphic to > change when the x-y scales are changed - like a thermometer, > scale bar or other symbol - How does one do this? > > I want to build my own library of glyphs to add to plots but > I do not know how to plot them when their size is independent > of the device/user coordinates. > > Is it possible to add to the list > of symbols in the function symbols() > other than: > _circles_, _squares_, _rectangles_, _stars_, _thermometers_, and > _boxplots_ > > can I make my own symbols and have symbols call these? > > > Thanks- > > > -- > Jonathan M. Lees > Professor > THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL Department of > Geological Sciences Campus Box #3315 Chapel Hill, NC 27599-3315 > TEL: (919) 962-0695 > FAX: (919) 966-4519 > jonathan.lees at unc.edu > http://www.unc.edu/~leesj > > ______________________________________________ > R-help at 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. >