【发布时间】:2026-02-07 12:55:02
【问题描述】:
当我保存一个使用 geom_raster 的 ggplot 时,瓷砖会出现“涂抹”。如果我使用ggsave() 或pdf(),结果是一样的。我对geom_tile 或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()
【问题讨论】:
-
也许贴一两个屏幕截图,以便我们可以轻松了解您的意思?
-
谢谢。将
geom_raster转换为geom_tile对我有用。