将数据叠加到背景图像上

Bti*_*rt3 15 r background-image

我最近发现使用Tableau Public使用背景图像并在其上映射数据是多么容易.这是他们网站上的流程.如您所见,它非常简单,您只需告诉软件您要使用的图像以及如何定义坐标.

这个过程在R中是否直截了当?什么是最好的方法?

Jor*_*eys 22

JPEG

对于jpeg图像,您可以read.jpeg()rimage包中使用.

例如:

anImage <- read.jpeg("anImage.jpeg")
plot(anImage)
points(my.x,my.y,col="red")
...
Run Code Online (Sandbox Code Playgroud)

通过在下一个绘图命令之前设置par(new = T),可以在背景图片上构建完整的绘图.(见下文?par)

PNG

您可以readPNGpng包中上传的PNG图像.使用readPNG,您需要rasterImage绘制命令(另请参阅帮助文件).在Windows上,必须摆脱alpha通道,因为到目前为止Windows无法应对每像素alpha.Simon Urbanek非常友好地指出了这个解决方案:

img <- readPNG(system.file("img", "Rlogo.png", package="png"))
r = as.raster(img[,,1:3])
r[img[,,4] == 0] = "white"

plot(1:2,type="n")
rasterImage(r,1,1,2,2)
Run Code Online (Sandbox Code Playgroud)

GIF

对于gif文件,您可以使用read.giffrom caTools.问题是这是旋转矩阵,所以你必须调整它:

Gif <- read.gif("http://www.openbsd.org/art/puffy/ppuf600X544.gif")

n <- dim(Gif$image)
image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)
Run Code Online (Sandbox Code Playgroud)

要绘制此图像,您必须正确设置par,例如:

image(t(Gif$image)[n[2]:1,n[1]:1],col=Gif$col,axes=F)
op <- par(new=T)
plot(1:100,new=T)
par(op)
Run Code Online (Sandbox Code Playgroud)


Spa*_*man 5

我不确定你想要做的部分是什么叫做"地理参考" - 拍摄没有坐标信息的图像并精确定义它如何映射到现实世界的行为.

为此,我使用Quantum GIS,一个免费的开源GIS软件包.将图像作为栅格图层加载,然后启动地理配准插件.单击图像上的某些已知点,然后输入这些点的纬度实际坐标.一旦你获得了足够的数据,那么地理参考者将会研究如何将你的图像拉伸并移动到它在地球上的真实位置,并编写一个"世界文件".

那么R应该能够使用rgdal包中的readGDAL以及可能的栅格包来读取它.


Ale*_*son 5

对于 JPEG 图像,您可以使用jpeg 库ggplot2 库

通常我发现让轴以像素为单位分级并且垂直轴在向下方向为正并且图片保持其原始纵横比很有用。所以我可以直接用计算机视觉算法产生的输出馈送R,例如该算法可以检测弹孔并从射击目标图片中提取孔坐标,然后R可以使用目标图像作为背景绘制二维直方图。

我的代码基于baptiste/sf/answers/1149273051/上找到的代码

library(ggplot2)
library(jpeg)

img <- readJPEG("bersaglio.jpg") # http://www.tiropratico.com/bersagli/forme/avancarica.jpg

h<-dim(img)[1] # image height
w<-dim(img)[2] # image width

df<-data.frame(x=rnorm(100000,w/1.99,w/100),y=rnorm(100000,h/2.01,h/97))
plot(ggplot(df, aes(x,y)) + 
       annotation_custom(grid::rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed
       scale_x_continuous(expand=c(0,0),limits=c(0,w)) +
       scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward
                                                      # Also note the limits where h>0 is the first parameter.
       coord_equal() + # To keep the aspect ratio of the image.

       stat_bin2d(binwidth=2,aes(fill = ..density..)) +
       scale_fill_gradient(low = "dark red", high = "red")
     )
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

df<-data.frame(x=rnorm(100000,100,w/70),y=rnorm(100000,400,h/100))
plot(ggplot(df, aes(x,y)) + 
       annotation_custom(grid::rasterGrob(img, width=unit(1,"npc"), height=unit(1,"npc")), 0, w, 0, -h) + # The minus is needed to get the y scale reversed
       scale_x_continuous(expand=c(0,0),limits=c(0,w)) +
       scale_y_reverse(expand=c(0,0),limits=c(h,0)) + # The y scale is reversed because in image the vertical positive direction is typically downward
       # Also note the limits where h>0 is the first parameter.
       coord_equal() + # To keep the aspect ratio of the image.

       stat_bin2d(binwidth=2,aes(fill = ..density..)) +
       scale_fill_gradient(low = "dark red", high = "red")
)
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明