如何以 png 作为背景进行绘图?

发布于 2024-10-21 15:40:54 字数 150 浏览 4 评论 0原文

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

在此处输入图像描述

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

I made a plot with a 3 million points and saved it as PNG. It took a few hours and I would like to avoid re-drawing all the points.

enter image description here

How can I generate a new plot that has this PNG as a background?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夏末 2024-10-28 15:40:55

试试这个:

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

下面是情节。

在此处输入图像描述

Try this:

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

Below is the plot.

enter image description here

渡你暖光 2024-10-28 15:40:55

虽然@bill_080的答案直接回答了你的问题,但这真的是你想要的吗?如果您想在其上绘图,则必须仔细对齐坐标系。请参阅例如 休斯顿犯罪地图 如何使用 ggplot2 完成此任务。

对于你的问题,在我看来,可能有一个更简单的解决方案:分箱,即ceating 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

在此处输入图像描述

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

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

在此处输入图像描述

While @bill_080's answer directly answers your question, is this really what you want? If you want to plot onto this, you'll have to carefully align your coordinate systems. See e.g. Houston Crime Map how this can be done with ggplot2.

For your problem, it seems to me that there may be an easier solution: binning, i.e. 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

enter image description here

hexbin works directly with lattice and ggplot2, but the center coordinates of the bins are in binned@xcm and binned@ycm, so you could also plot the result in base graphics. With high number of bins, you get a fast version of your original plot:

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

enter image description here

but you can easily have the colours coding the density:

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

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文