R - 在散点图上绘制缩略图(位于列表中) [英] R - plotting thumbnails (that are in a list) on a scatterplot

查看:225
本文介绍了R - 在散点图上绘制缩略图(位于列表中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一组缩略图绘制为散点图上的点。我已经开始使用这里的答案代码但是他们在整个情节中重复相同的缩略图,而我有一个图像列表。

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

我已经尝试将thumbnail()包装成循环但是情节是空的

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.

推荐答案

我只需要在缩略图中删除这一行()

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.

这篇关于R - 在散点图上绘制缩略图(位于列表中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆