0

I've made the attached plot using a for loop and the regular plot function in R. enter image description here

I used code similar to the code below in order to create it:

imagecoords <- data.frame(name=c("apple", "orange", "pear"), 
                          x=c(1, 2, 3), y=c(1,2,3))

for(i in 1:nrow(imagecoords)) {
  thisimagename <- imagecoords$name[i]
  path <- paste('~mypath/', 
                thisimagename, "_pic.png", sep="")
  thispic <- readPNG(path)  
  rasterImage(thispic, 
              xleft=imagecoords$x[i]-1, 
              xright=imagecoords$x[i]+1,
              ytop=imagecoords$y[i]+1, 
              ybottom=imagecoords$y[i]-1
  )
}

I'm wondering if it's possible to use ggplot to easily plot a series of images in specific locations in a similar manner. (After several years of dragging my feet, I'm finally learning to use ggplot, and am, of course, finding it MUCH more powerful for plotting and comparing data within subgroups, and am hoping to do additional plots using ggplot, and would like them to match aesthetically.)

I can plot ellipses with mean central points with the following code:enter image description here

apples <- data.frame(fruit=rep("apple", times=10), x=rnorm(10, mean=1), y=rnorm(10, mean=1))
oranges <- data.frame(fruit=rep("orange", times=10), x=rnorm(10, mean=2), y=rnorm(10, mean=2))
pears <- data.frame(fruit=rep("pear", times=10), x=rnorm(10, mean=3), y=rnorm(10, mean=3))

fruitdata <- rbind(apples, oranges, pears)
fruitmeans <- ddply(fruitdata, .(fruit), summarize, 
                    xmean=mean(x), ymean=mean(y))
ggplot() + 
  stat_ellipse(data=fruitdata, aes(x=x, y=y, color=factor(fruit)), level=0.95) +
  geom_point(data=fruitmeans,aes(x=xmean, y=ymean, color=factor(fruit)))

I read this extremely helpful post, and infer from it that I could do half a dozen calls to annotation_custom, but that seems a little ugly.

I know enough about ggplot to know that my previous approach is somewhat anachronistic for ggplot's approach to data - I'd be open to strategizing about different ways of approaching the problem, as well.

Community
  • 1
  • 1
fredtal
  • 571
  • 4
  • 16
  • Oh, thank you! Although that example seems SO complicated for my ggplot-newbie self, and it seems that key parts of the subsequent one are gone. :( I will go try to wrap my brain around it... – fredtal Nov 27 '13 at 22:16
  • the short answer is, unfortunately, that ggplot2 doesn't really lend itself to custom geoms – baptiste Nov 27 '13 at 22:53
  • I'm sorry this is closed, because I know what you mean about the multiple calls being inelegant. Look at http://stackoverflow.com/questions/19625328/make-the-value-of-the-fill-the-actual-fill-in-ggplot2/20196002#20196002 : the code is in the last 10 lines of the answer - create annotation_customs using apply(rasterGrob()) and then add them to the plot. The important thing I found by trial and error was to use rasterGrob to create a grob from the image and make sure it's 1x1 height by width - then annotation_custom will scale and size it as you would expect. – Troy Nov 28 '13 at 01:50
  • require(grid) require(png) #NEWLINE# imlayers<-apply( imagecoords,1,FUN=function(x)return( annotation_custom( rasterGrob( image=readPNG(paste(x["name"], "_pic.png", sep="")), x=0,y=0,height=1,width=1,just=c("left","bottom")), xmin=as.numeric(x["x"])-1, xmax=as.numeric(x["x"])+1, ymin=as.numeric(x["y"])-1, ymax=as.numeric(x["y"])+1))) #NEWLINE# qplot(0:5,0:5)+imlayers – Troy Nov 28 '13 at 02:22
  • The above works if the images are in your working directory – Troy Nov 28 '13 at 02:22

0 Answers0