如何用png作为背景?

Ale*_*huk 39 r data-visualization large-data

我制作了一个300万分的情节并将其保存为PNG.花了几个小时,我想避免重新绘制所有要点.

在此输入图像描述

如何生成以PNG为背景的新图?

bil*_*080 80

试试这个:

library(png)

#Replace the directory and file information with your info
ima <- readPNG("C:\\Documents and Settings\\Bill\\Data\\R\\Data\\Images\\sun.png")

#Set up the plot area
plot(1:2, type='n', main="Plotting Over an Image", xlab="x", ylab="y")

#Get the plot information so the image will fill the plot box, and draw it
lim <- par()
rasterImage(ima, lim$usr[1], lim$usr[3], lim$usr[2], lim$usr[4])
grid()
lines(c(1, 1.2, 1.4, 1.6, 1.8, 2.0), c(1, 1.3, 1.7, 1.6, 1.7, 1.0), type="b", lwd=5, col="white")
Run Code Online (Sandbox Code Playgroud)

下面是情节.

在此输入图像描述


cbe*_*ica 17

虽然@ bill_080的答案直接回答了你的问题,这真的是你想要的吗?如果要绘制到此图上,则必须仔细对齐坐标系.参见例如休斯顿犯罪地图如何使用ggplot2完成此操作.

对于你的问题,在我看来,可能有一个更容易的解决方案:binning,即停止2d直方图.

> df <- data.frame (x = rnorm (1e6), y = rnorm (1e6))
> system.time (plot (df))
       User      System verstrichen 
     54.468       0.044      54.658 
> library (hexbin)
> system.time (binned <- hexbin (df, xbins=200))
       User      System verstrichen 
      0.252       0.012       0.266 
> system.time (plot (binned))
       User      System verstrichen 
      0.704       0.040       0.784
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

hexbin直接使用lattice和ggplot2,但是bin的中心坐标是binned@xcmbinned@ycm,所以你也可以在基本图形中绘制结果.拥有大量垃圾箱,您可以获得原始情节的快速版本:

> system.time (plot (binned@xcm, binned@ycm, pch = 20, cex=0.4))
       User      System verstrichen 
      0.780       0.004       0.786 
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但你可以很容易地得到编码密度的颜色:

> plot (binned@xcm, binned@ycm, pch = 20, cex=0.4, col = as.character (col))

> col <- cut (binned@count, 20)
> levels (col) <- grey.colors (20, start=0.9, end = 0)
> plot (binned@xcm, binned@ycm, pch = 20, cex=0.4, col = as.character (col))
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述