保存为 PDF 时,geom_raster 出现“污点”

2024-03-26

当我保存使用的 ggplot 时geom_raster,瓷砖“被涂抹”了。如果我使用的话结果是一样的ggsave() or pdf()。我没有这个问题geom_tile or image。我使用 RStudio、X11 或 PNG 图形设备时没有出现此问题。

这是什么原因造成的?我该如何修复它?

例子:

library(ggplot2)

## doesn't work: tiles are smeared together

ggsave("smeared1.pdf",
  ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
    geom_raster(aes(x = x, y = y, fill = fill)))

pdf("smeared2.pdf")
ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
    geom_raster(aes(x = x, y = y, fill = fill))
dev.off()

## works fine

ggsave("not-smeared0.png",
  ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
    geom_raster(aes(x = x, y = y, fill = fill)))

ggsave("not-smeared1.pdf",
  ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
    geom_tile(aes(x = x, y = y, fill = fill)))

pdf("not-smeared2.pdf")
ggplot(cbind(expand.grid(x = 1:3, y = 1:3), fill = rnorm(9))) +
  geom_tile(aes(x = x, y = y, fill = fill)))
dev.off()

pdf("not-smeared3.pdf")
image(matrix(rnorm(9), 3))
dev.off()

这可能是由于您的 PDF 查看器执行的操作栅格插值 https://groups.google.com/forum/#!topic/ggplot2/8VLuo1cw6SE。我在 Mac 上重新创建了您的“smeared2.pdf”(见下文),它在 Adob​​e Reader 中看起来不错(右),但在预览中看起来很模糊(左)。根据您的 PDF 查看器,您也许可以通过更改设置来消除模糊效果。例如,在预览中,在首选项下的 PDF 选项卡中,您可以取消选中“平滑文本和线条艺术”,PDF 将正确显示。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

保存为 PDF 时,geom_raster 出现“污点” 的相关文章

随机推荐