在R中创建栅格图像的散点图 [英] Create a Scatterplot of Raster Images in R

查看:659
本文介绍了在R中创建栅格图像的散点图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定是否可以在SO中允许使用此类问题,因为目前我没有可重复的数据.

I am not entirely sure if these kind of questions are allowed at SO, as I have no reproducible data at the moment.

我的问题是关于如何在R中创建栅格图像的散点图.我不熟悉允许您执行此操作的任何程序包. 这是唯一的示例到目前为止,我在搜索中遇到过.本质上,这就是我要执行的操作,但是,我想知道R是否有可能简单地获取输入数据并绘制图像,而不是在绘制区域中输入坐标.

My question is in regards to how one might go about creating a scatterplot of raster images in R. I am not familiar with any packages that allow you to do this. This is the only example I have come across so far in my search. Essentially, this is what I would like to do, however, I am wondering if it's possible for R to simply take the input data and plot the image rather than be fed coordinates in my plot area.

我的最终目标是使用徽标而不是标签创建运动队的光栅散点图.我的第一个想法是创建一个包含团队名称,X变量,Y变量和.png图像URL位置的数据框.

My end goal is to create raster image scatterplots of sports teams using their logos instead of labels. My first thought is to create a data frame including team name, X variable, Y variable, and .png image URL location.

这是我最终希望做的一个例子.我不确定OP使用什么程序,但是显然我想在R中做类似的事情.

Here is an example of what I am ultimately hoping to do. I'm not sure what program the OP uses, but obviously I would like to do something like this in R.

更新

在格雷格·斯诺(Greg Snow)的建议的帮助下,我能够用自己的徽标重现他的例子.

With the help of Greg Snow's suggestion, I was able to reproduce his example with my own logos.

推荐答案

TeachingDemos软件包中的my.symbolsms.image函数是一个可能的起点.帮助页面上有一个ms.image的示例,该示例显示了如何使用R徽标作为绘图符号.目前,它一次只处理一幅图像,因此您可以从空白图开始并遍历图像集,也可以编写包装函数以获取图像列表和要绘制的指示符.这是包装器功能的第一个刺孔:

The the my.symbols and ms.image functions in the TeachingDemos package are one possible starting place. There is an example on the help page for ms.image that shows how to use the R logo as the plotting symbol. Currently it only does one image at a time, so you could either start with a blank plot and loop through the set of images, or a wrapper function can be written that takes a list of images and an indicator of which to plot. Here is a first stab at a wrapper function:

ms.image2 <- function(imgs, transpose=TRUE, 
                      which=1, ...) {
  ms.image(imgs[[which]], transpose=transpose, ...)
}

然后我们可以使用以下代码创建图像列表:

Then we can create a list of images with code like:

require(png)
img1 <- readPNG(system.file("img", "Rlogo.png", package="png"))
logos <- list( img1, img1[76:1,,], img1[,100:1,],
               img1[76:1,100:1,], img1[,,c(3:1,4)])

这些都是徽标上的变体,但对于您的示例,您可以将.png文件的文件名向量传递给lapply以生成类似的列表.

These are all variations on the logo, but for your example you could pass a vector of file names of the .png files to lapply to produce a similar list.

现在我们可以像这样运行my.symbols(尽管显然您将使用真实数据而不是随机数作为位置):

Now we can run my.symbols like this (though obviously you will use real data rather than random numbers for the locations):

my.symbols( runif(10), runif(10), ms.image2, 
            MoreArgs=list(imgs=logos), which=rep(1:5,2),
            inches=0.3, symb.plots=TRUE, add=FALSE)

这将按照您的示例生成图:

And that produces a plot along the lines of your example:

修改

为了加快速度,您可以使用rasterImage,下面是一些新代码,运行时间大约是上述代码的一半(与microbenchmark相比):

For a speed up you can use rasterImage, here is some new code that ran in about half the time as the above (compared using microbenchmark):

ms.rasterImage <- function(imgs, which=1, ...) {
  rasterImage(imgs[[which]], -1, -1, 1, 1)
}

logos2 <- list(as.raster(img1), as.raster(img1[76:1,,]),
               as.raster(img1[,100:1,]), 
               as.raster(img1[76:1,100:1,]),
               as.raster(img1[,,c(3:1,4)])
    )

my.symbols( runif(10), runif(10), ms.rasterImage, 
            MoreArgs=list(imgs=logos2), which=rep(1:5,2),
            inches=0.3, symb.plots=TRUE, add=FALSE)

这是基于上面的注释中的链接使用ggplot2的一些代码,但使用的是徽标列表:

And here is some code using ggplot2 based on the link in the comment above, but using the list of logos:

ggplot(mtcars, aes(mpg, wt)) + 
  mapply(function(xx, yy, i) 
    annotation_raster(logos[[i]], xmin=xx-1, xmax=xx+1, ymin=yy-0.2, ymax=yy+0.2),
    mtcars$mpg, mtcars$wt, mtcars$gear-2) 

主要是出于我的好奇心,下面是一些时间安排:

And mainly for my curiosity, here are the timings:

> microbenchmark(
+   my.symbols( mtcars$mpg, mtcars$wt, ms.image2, 
+               MoreArgs=list(imgs=logos), which=mtcars$gear-2,
+               inches=0.3, symb.plots=TRUE, add=FALSE),
+   my.symbols( mtcars$mpg, mtcars$wt, ms.rasterImage, 
+               MoreArgs=list(imgs=logos2), which=mtcars$gear-2,
+               inches=0.3, symb.plots=TRUE, add=FALSE),
+   plot(ggplot(mtcars, aes(mpg, wt)) + 
+          mapply(function(xx, yy, i) 
+            annotation_raster(logos[[i]], xmin=xx-1, xmax=xx+1, ymin=yy-0.2, ymax=yy+0.2),
+            mtcars$mpg, mtcars$wt, mtcars$gear-2) )
+ )
Unit: milliseconds

                      min       lq     mean   median        uq       max neval cld
       ms.image  518.9137 530.5549 661.9333 545.3890  751.7116 1737.7430   100  b 
 ms.rasterImage  158.7097 162.4493 244.6673 171.6103  381.6499  544.1656   100 a  
        ggplot2  478.3005 606.3831 896.8793 772.7210 1359.8888 1714.5647   100   c

这篇关于在R中创建栅格图像的散点图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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