r - 如何提高ggplot2对象的保存效率?

标签 r ggplot2 png

我正在尝试使用ggplot2软件包使用R生成加权密度图,并将其另存为.png文件。在我的代码中,我正在制作100-1000个具有不同地理坐标的地块。

问题是,如果我的数据集甚至是1500点,那么ggsave函数将变得非常慢。然后,大约需要100秒钟来保存这些图中的一个。据我了解,计算效率低下是因为我正在绘制的ggplot2对象是网格,而ggsave必须在保存之前打印它们。

所以,我在问有什么方法可以使这些ggplot2对象的保存更加有效吗?我的意思是除了降低kde2d密度估计的分辨率外,还可以采用其他任何方法,这实际上会使要绘制的数据帧更小。

我提供了一个最小的工作示例,其中生成了一个.png文件。当在ggsave函数周围使用system.time()时,您会发现执行该过程大约需要100秒钟。

library(MASS)
library(ggplot2)
library(grid)


x <- runif(1550, 0, 100)
y <- runif(1550, 0, 100)
wg <- runif(1550, 0, 1)

data <- data.frame(x, y, wg)


source("C:/Users/cpt2avo/Documents/R/kde2dweighted.r")
dens <- kde2d.weighted(data$x, data$y, data$wg)
dfdens <- data.frame(expand.grid(x=dens$x, y=dens$y), z=as.vector(dens$z))

p <- ggplot(data, aes(x = x, y = y)) + stat_contour(data = dfdens, geom = "polygon", bins = 20, alpha = 0.2, aes(x = x, y = y, z = z, fill = ..level..)) + scale_fill_continuous(low = "green", high = "red") + scale_alpha(range = c(0,1), limits = c(0.5, 1), na.value = 0) + labs(x = NULL, y = NULL) + theme(axis.title = element_blank(), axis.text = element_blank(), axis.ticks = element_blank(), axis.line = element_blank(), plot.margin = unit(c(0,0,-0.5,-0.5), "line"), panel.border = element_blank(), panel.grid = element_blank(), panel.margin = unit(c(0,0,0,0), "mm"), legend.position = "none", plot.background = element_rect(fill = "transparent", colour = NA), panel.background = element_blank())

system.time(ggsave(p, file = "C:/Users/cpt2avo/Documents/R/example.png", width = 2, height = 2, units = "in", dpi = 128))

kde2d.weighted是用于进行2d加权内核密度估计的函数。
kde2d.weighted <- function (x, y, w, h, n = 25, lims = c(range(x), range(y))) {
      nx <- length(x)
      if (length(y) != nx) 
        stop("data vectors must be the same length")
      if (length(w) != nx & length(w) != 1)
        stop("weight vectors must be 1 or length of data")
      gx <- seq(lims[1], lims[2], length = n) # gridpoints x
      gy <- seq(lims[3], lims[4], length = n) # gridpoints y
      if (missing(h)) 
        h <- c(bandwidth.nrd(x), bandwidth.nrd(y));
      if (missing(w)) 
        w <- numeric(nx)+1;
      h <- h/4
      ax <- outer(gx, x, "-")/h[1] # distance of each point to each grid point in x-direction
      ay <- outer(gy, y, "-")/h[2] # distance of each point to each grid point in y-direction
      z <- (matrix(rep(w,n), nrow=n, ncol=nx, byrow=TRUE)*matrix(dnorm(ax), n, nx)) %*% t(matrix(dnorm(ay), n, nx))/(sum(w) * h[1] * h[2]) # z is the density
      return(list(x = gx, y = gy, z = z))
    }

最佳答案

@AntonvSchantz我遇到了与您相同的问题,但经历非常相似。实际上,它通过ggsave()导出为高分辨率png,这会使此过程变慢。我的决议是通过执行以下操作来导出为pdf:

在剧情创作之上pdf(paste("plots/my_filename", rn , ".pdf", sep = ""), width = 11, height = 8)
在您的情节创建下方:dev.off()

关于r - 如何提高ggplot2对象的保存效率?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24626323/

相关文章:

r - 在 R 中使用线性(或非线性?)约束优化值

用于将新列返回到数据集的 R 函数

r - 如何使 'head'自动应用于输出?

r - R 和 ggplot 中有没有办法创建带有条件几何图形的facet_wrap 图?

Rmarkdown 不会将 Montserrat 字体编织成 PDF

css - 操作 html 图像的颜色

directx - 在 SharpDx 中保存为 png

r - 基本 R 图的颜色已更改 - 我可以恢复到旧调色板吗?

r - ggplot2:每组的四分位距

shell - 为现有png图片添加标签,使用convert