11

Possible Duplicate:
How to use an image as a point in ggplot?

Is it possible to have user defined pch (clip art or icon or other type of file) used as point in R base or ggplot or other graphical device.

For example:

enter image description here

set.seed(123)
mydt <- data.frame (x = rnorm(5, 5,2), y = rnorm (5,10,3), z = rnorm (5, 1,0.5))

enter image description here

Here size is proportional to z.

Community
  • 1
  • 1
jon
  • 11,186
  • 19
  • 80
  • 132
  • grImport seems interesting, I am interested to see how this can be used in this situation ! – shNIL Dec 31 '12 at 14:59

2 Answers2

16

Using grid.raster

library(png)
flower <- readPNG("flower.png")
pushViewport(plotViewport(margins=c(5,5,5,5)))
grid.rect(gp = gpar(fill=NA))
pushViewport(plotViewport(margins=c(5,5,5,5),
                          xscale=extendrange(mydt$x),
                          yscale=extendrange(mydt$y)))


grid.raster(image=flower,x=mydt$x,y=mydt$y,width=mydt$z, 
                interpolate=FALSE,default.units = 'native')
grid.polyline(mydt$x,mydt$y,default.units='native')
upViewport(2)

enter image description here

agstudy
  • 119,832
  • 17
  • 199
  • 261
  • thanks agstudy for the solution, it seems a easier alternative to grImport (i am just struggling to understand) – jon Dec 31 '12 at 16:18
  • actually I would like to display axis label, the example I provided was hand drawn, I forgot to mention it – jon Dec 31 '12 at 17:15
  • 1
    Type grid.axis() before grid.raster line. i am far from a pc to do it. – agstudy Dec 31 '12 at 17:51
3

For Base graphics look at the my.symbols and ms.image functions in the TeachingDemos package.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110