问题
I'd like to plot a set of thumbnail images as points on a scatterplot. I've started with the answer code located here but they repeat the same thumbnail throughout the plot, whereas I have a list of images.
xy <- data.frame(x=runif(337, 0, 100), y=runif(337, 0, 100))
imgfiles <- list.files(getwd(),pattern="*-scaled.png")
img <- lapply( imgfiles,function(x) readPNG(x) )
thumbnails <- function(x, y, images, width = 0.1*diff(range(x)),
height = 0.1*diff(range(y))){
images <- replicate(length(x), images, simplify=FALSE)
stopifnot(length(x) == length(y))
for (ii in seq_along(x)){
rasterImage(images[[ii]], xleft=x[ii] - 0.5*width,
ybottom= y[ii] - 0.5*height,
xright=x[ii] + 0.5*width,
ytop= y[ii] + 0.5*height, interpolate=FALSE)
}
}
plot(xy, t="n")
thumbnails(xy[,1], xy[,2], img[[11]]) # this works but the same image is repeated
I've tried wrapping thumbnail() into a loop but the plot comes out empty
for (n in length(img)){
thumbnails(xy[n,1], xy[n,2], img[[n]])
}
How do I do this? I'm not understanding how to reference the images within the list, or whether i should change them to a different data structure.
回答1:
I just had to get rid of this line in thumbnails()
images <- replicate(length(x), images, simplify=FALSE)
It was replicating the sample image given in the original answer. There's no need for a loop after.
来源:https://stackoverflow.com/questions/37150889/r-plotting-thumbnails-that-are-in-a-list-on-a-scatterplot