I've made the attached plot using a for loop and the regular plot function in R.
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:
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.