【问题标题】:ggplot writes to pdf and png with very different sizesggplot 以非常不同的大小写入 pdf 和 png
【发布时间】:2021-09-21 12:45:40
【问题描述】:

我创建了一个包含 4 个图的网格,然后将其写入 pdf 和 png。由于我不明白的原因,图表和字体的绘制方式不同(png 比 pdf 小得多)。我使用的命令是:

pdf("Cusum Page 1.pdf")
  grid.arrange(log_return_plot, tracking_error_plot, 
               ir_graph, excess_vol, 
               layout_matrix = rbind(c(1, 2), c(3, 4)))
dev.off()

png(filename = "Cusum Page 1.png", width = 800, height = 800, units = "px",
    pointsize = 12, bg = "white", res = NA, family = "", restoreConsole = TRUE,
    type = c("windows", "cairo", "cairo-png"), antialias = "d")   

  grid.arrange(log_return_plot, tracking_error_plot, 
               ir_graph, excess_vol, 
               layout_matrix = rbind(c(1, 2), c(3, 4)))

dev.off()

我尝试在 png 调用中更改和删除各种选项,但没有成功 - 我无法让我的 png 和 pdf 看起来相同。是否知道此问题的根本原因?我非常感谢有关设置的建议,我应该尝试更改以使两个导出相同。

在此致以诚挚的谢意

托马斯·飞利浦

【问题讨论】:

  • 天哪,真是一团糟。这真的很玄学。我不认为我可以自己解决这个问题。

标签: r pdf ggplot2 png


【解决方案1】:

我怀疑这与设置每个选项的 widthheight 参数以及使 png::res(标称分辨率)和 pdf::pointsize(默认为 12)一致有关。

pdf 尺寸默认为英寸,所以我让 png 效仿。

这是解决方案的初步尝试。

library(gridExtra)
library(ggplot2)


pdf("Cusum Page 1.pdf", width = 6, height = 6)
grid.arrange(p1, p2, p3, p4, 
             layout_matrix = rbind(c(1, 2), c(3, 4)))
dev.off()



png(filename = "Cusum Page 2.png", width = 6, height = 6, units = "in",
    pointsize = 12, bg = "white", res = 144, family = "", restoreConsole = TRUE,
    type = c("windows", "cairo", "cairo-png"), antialias = "d")   

grid.arrange(p1, p2, p3, p4, 
             layout_matrix = rbind(c(1, 2), c(3, 4)))

dev.off()

图表


p1 <- 
  ggplot(iris) + 
  geom_histogram(aes(Sepal.Length))

p2 <-  
  ggplot(iris) + 
  geom_histogram(aes(Sepal.Width))

p3 <- 
  ggplot(iris) + 
  geom_histogram(aes(Petal.Length))

p4 <-  
  ggplot(iris) + 
  geom_histogram(aes(Petal.Width))

reprex package (v2.0.0) 于 2021-07-12 创建

png

pdf - 转换为 jpg 以启用上传

【讨论】:

  • 像魅力一样工作。我将 pdf 和 png 都设置为 8 英寸 x 8 英寸,并将 res 设置为 144,这两个输出基本相同。感谢磨坊。
猜你喜欢
  • 2018-07-05
  • 2020-08-18
  • 1970-01-01
  • 2019-02-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-22
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多