将数据叠加到背景图片

问题描述:

我最近发现,使用Tableau Public在背景图片和地图上使用背景图片和地图数据是多么容易。这是从他们的website的过程。正如您所看到的,它非常简单,您只需告诉软件您要使用的图像以及如何定义坐标。将数据叠加到背景图片

R中的过程是否简单?最好的方法是什么?

JPEG

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

如:

anImage <- read.jpeg("anImage.jpeg") 
plot(anImage) 
points(my.x,my.y,col="red") 
... 

通过接下来的情节命令之前设置面值(新= T),你可以在背景图片构建完整的情节。 (见?par进一步回落)

PNG

PNG图像,你可以使用readPNGpng上传软件包。使用readPNG,您需要使用rasterImage命令进行绘图(另请参阅帮助文件)。在Windows上,人们必须摆脱alpha通道,因为到目前为止,Windows无法应对每个像素的alpha通道。西蒙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) 

GIF

GIF文件,你可以使用read.gifcaTools。问题是,这个旋转矩阵,所以你必须调整它:

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) 

要绘制了这张图片,你必须正确地设置面值,如:

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) 
+1

发现我尝试在2.10和2.12.1,并安装锐美包两次都得到了相同的错误。我使用的是Windows XP:警告:无法为库http://www.stats.ox.ac.uk/pub/RWin/bin/windows/contrib/2.10 警告信息访问指数: 在getDependencies(PKGS,依赖,available,lib): 包'rimage'不可用 – Btibert3

+0

@ Btibert3:我可以从www.freestatistics.org/cran安装这两个程序都没有问题。无论如何,尝试获得另一个存储库,在http://cran.r-project.org/mirrors.html上提到的一个镜像之一将会做到。 –

+0

我的工作笔记本电脑有问题,出于某种原因。我只是试过我的个人笔记本电脑,并没有问题安装。最后,我应该说这是一个GIF文件。 – Btibert3

我不是确定你想要做的一部分就是所谓的“地理参照” - 这是一种拍摄没有坐标信息的图像,并精确地定义了它如何映射到现实世界。

为此,我将使用Quantum GIS,一种免费和开源的GIS软件包。作为栅格图层加载到图像中,然后启动地理配准插件。点击图像上的一些已知点并输入这些点的纬度长实际坐标。一旦你有足够的这些,地理参考者将解决如何拉伸和移动你的图像到它在地球上的真实位置,并写出一个'世界档案'。

然后,R应该能够使用rgdal包中的readGDAL以及可能的光栅包来读取它。

+1

感谢您的帮助。该图片实际上没有任何地理属性;这是一个曲棍球场,我想要在它上面绘制X/Y坐标。我的图片是一个GIF文件。 – Btibert3

对于JPEG图像,您可以使用jpeg libraryggplot2 library

通常情况下,我发现轴的像素渐变和垂直轴在向下方向变为正值并且图片保持其原始高宽比时非常有用。所以我可以用计算机视觉算法产生的输出直接提供R,例如该算法可以检测弹孔并从拍摄目标图片中提取孔坐标,然后R可以使用目标图像作为背景绘制二维直方图。

我的代码是由baptiste基于代码的https://*.com/a/16418186/15485

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") 
    ) 

enter image description here

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") 
) 

enter image description here