[R] PLotting R graphics/symbols without user x-y scaling

Greg Snow Greg.Snow at intermountainmail.org
Tue Feb 27 21:37:30 CET 2007


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.
>



More information about the R-help mailing list