如何删除 ggsave 添加到光栅图像的不需要的水平线?

How to remove unwanted horizontal lines added to raster image by ggsave?

我无法解决在使用 R 中的 ggsave() 函数从光栅绘制和保存图像时发现的问题。
当我绘制它时,它运行良好。当我使用 ggsave() 导出它时,水平灰线被添加到图中。
我想删除它们,但我不知道该怎么做。
这是一个示例图像,其中包含我使用的选项和代码:

gg.opzioni = list(geom_tile(aes(x, y, fill = values)),
                  scale_fill_gradientn(n.breaks = 3, colours = c("#52647A", "#2C413C", "#646859"), guide = "legend", na.value = "white"),
                  theme(plot.title = element_text(size = 14, face = "bold", hjust = 0.5),
                        axis.title.x = element_text(size = 12), axis.title.y = element_text(size = 12),
                        plot.margin = unit(c(2, 2, 2, 2), "mm"), panel.background = element_blank(),
                        panel.border = element_rect(colour = "black", fill = NA, size = 1),
                        axis.text.x = element_blank(), axis.ticks.x = element_blank(), axis.text.y = element_blank(),
                        axis.ticks.y = element_blank(), panel.grid.minor = element_blank(), panel.grid.major = element_blank(),
                        panel.grid.major.x = element_blank(), panel.grid.major.y = element_blank(),
                        panel.grid.minor.x = element_blank(), panel.grid.minor.y = element_blank(), aspect.ratio = 11/10),
                  scale_x_continuous(limits = c(0, 1), expand = c(0, 0), breaks = seq(0, 1, 0.1), labels = seq(0, 10, 1)),
                  scale_y_continuous(limits = c(0, 1), expand = c(0, 0), breaks = seq(0, 1, 0.1), labels = seq(0, 10, 1)),
                  coord_fixed())

r.sam = ggplot(df) + gg.opzioni + labs(title = "Campione ricostruito", x = "", y = "", fill = "classe:")
ggsave(filename = "lapalma_sam.png", plot = r.sam, device = "png", path = "/Users/Francesco/Downloads/")

我尝试使用 panel.grid 选项删除可能的网格,但没有成功。

最初 df 对象中包含三个变量:两个坐标和一个像素 class。

library(tidyverse)

df <- tibble(
  val = rep(sin(seq(0, 4*pi, length = 100)), 100),
  x = rep(1:100, 100),
  y = rep(1:100, each = 100)
)

以下复制了您的问题,其中每个单元格周围都可以看到水平线:

plot.tiles <- ggplot(data = df, aes(x = x, y = y, fill = val)) +
  geom_tile()
ggsave('plot_tile.png', plot.tiles)  

出现这种情况是因为 geom_tile() 具有边框颜色 属性。一种解决方案是使“颜色”美学与“填充”美学相匹配:

plot.border <- ggplot(data = df, aes(x = x, y = y, fill = val, color = val)) +
  geom_tile()
ggsave('plot_border.png', plot.border)  

或者您可以使用 geom_raster(),它没有单元格边框,但功能类似于 geom_tile():

plot.raster <- ggplot(data = df, aes(x = x, y = y, fill = val)) +
  geom_raster()
ggsave('plot_raster.png', plot.raster)