我们如何在R中的给定坐标处绘制图像? [英] How do we plot images at given coordinates in R?

查看:320
本文介绍了我们如何在R中的给定坐标处绘制图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出png / jpeg格式的'n'图像和2维(x,y)的'n'对应坐标:我想在单个图上的给定坐标处绘制这些图像。如果我发现图像太大,我最好将它们绘制为给定坐标处的较小/缩放版本。我怎样才能在R中实现这样的情节?

Given 'n' images in a png/jpeg format and 'n' corresponding coordinates in 2 dimensions (x,y): I would like to plot these images at the given coordinates on a single plot. If I find the images to be too big, I would ideally like to plot them as a smaller/scaled version at the given coordinates. How can i achieve such a plot in R?

下面给出了这样一个情节的示例:

An example of how such a plot would look is given below:

推荐答案

xy <- data.frame(x=runif(10, 0, 100), y=runif(10, 0, 100))

require(png)
img <- readPNG(system.file("img", "Rlogo.png", package="png"))

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)

这篇关于我们如何在R中的给定坐标处绘制图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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