【问题标题】:How to plot with a png as background? [duplicate]如何以png为背景进行绘图? [复制]
【发布时间】:2012-10-06 18:28:00
【问题描述】:

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

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

【问题讨论】:

标签: r data-visualization large-data


【解决方案1】:

虽然@bill_080 的回答直接回答了您的问题,但这真的是您想要的吗?如果你想在上面绘制,你必须仔细对齐你的坐标系。参见例如Houston Crime Map ggplot2 如何做到这一点。

对于您的问题,在我看来,可能有一个更简单的解决方案:分箱,即 ceating 2d histograms。

> 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

hexbin 直接使用 lattice 和 ggplot2,但 bin 的中心坐标位于 binned@xcm 和 binned@ycm,因此您也可以在基本图形中绘制结果。通过大量的 bin,您可以获得原始绘图的快速版本:

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

但是你可以很容易地用颜色来编码密度:

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

【讨论】:

    【解决方案2】:

    试试这个:

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

    下面是剧情。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-04-21
      • 2012-03-04
      • 2013-04-10
      • 2019-02-28
      • 2011-03-04
      相关资源
      最近更新 更多