使用 geom_raster() 或 geom_tile 或 geom_rect() 绘制多层

Plotting multiple layers with geom_raster() or geom_tile or geom_rect()

我有一个 data.frame 的 b/w 栅格图像数据用作 background/base 图层。它具有三列:x、y、z。其中 x 和 y 是各自的坐标,z 是定义 512 x 512 图像的连续值。我将此背景填充设置为黑白渐变并使用 ggplot2 中的 geom_raster() 绘图,然后将图像保存为 PDF 文件。

### MWE of base layer
# original 512x512 data is read in from a file in matrix format, so here's a mocked up example of same:
h = matrix(data = rep(c(1:512), 512), byrow = T, nrow = 512, ncol = 512)

# convert to data.frame with all the rows of z-data in first column
h = data.frame(z = as.vector(t(h)))

# create equally spaced 512x512 x and y pixel coordinate vectors
x = c(-255:256)
y = c(-255:256)

# add x and y coordinate vectors to the data.frame
h$x = rep(t(x), 512)
h$y = rep(t(-y), each=512)

# plot the data
ggplot() + 
  coord_fixed() +
  geom_raster(data=h, aes(x,y,fill=z)) +
  scale_fill_gradient(low="black", high="white", na.value="transparent")

# save to png   
ggsave("testbaseplot.png", dpi=300)

这会在保存的文件中给出所需的结果。


接下来,在该基础层的顶部绘制一个或多个附加层。这些层也有 x、y 和 r。其中 r 是连续值或离散值,具体取决于区域。例如,定义区域边界的层将标记离散区域,而定义区域中的拓扑的层将定义拓扑。

理想情况下,我想使用 geom_raster() 两次,每个层(基础层、覆盖层)一次,并在每个层上使用不同的 aes(fill=),并使用不同的 scale_fill_*()对于每一层也是如此。但是,尝试这样做会导致错误:

# MWE of mock overlay = 4 square labeled regions
# create z-data for some example shapes
r = data.frame(r = as.vector(c(rep(rep(c('a','b'),each=100),100),
                               rep(rep(c('c','d'),each=100),100))))

# create x and y coordinates 
r$x = rep(c(-99:100),200)
r$y = rep(c(-99:100),each=200)

# plot base layer and 1 overlay layer, both as geom_raster
ggplot() + 
  coord_fixed() +
  geom_raster(data=h, aes(x,y,fill=z)) +
  scale_fill_gradient(low="black", high="white", na.value="transparent") + 
  geom_raster(data=r, aes(x,y,fill=r)) +
  scale_fill_manual(values=c("red","white","blue","yellow"))

错误信息:

Scale for 'fill' is already present. Adding another scale for 'fill', which will replace the existing scale. Error: Continuous value supplied to discrete scale


我想也许我可以用 aes(fill=z) 做一个 geom_raster(),用 aes(colour=r)scale_colour_manual 做一个,但是 geom_raster() 不识别 aes(colour=) 选项,所以我用 geom_tile(aes(colour=r)) 代替:

# plot looks fine
ggplot() + 
  coord_fixed() +
  geom_raster(data=h, aes(x,y,fill=z)) +
  scale_fill_gradient(low="black", high="white", na.value="transparent") + 
  geom_tile(data=r, aes(x,y,colour=r), fill="transparent") +
  scale_colour_manual(values=c("red","white","blue","yellow"))

# saved file does not
ggsave("testlayerplot.png", dpi=300)

该图在 RStudio 的预览器中看起来不错 window,但是当另存为文件(pdf、png 等)时,图块层上有不需要的线条网格。

我相信这些线条会出现,因为 geom_tile 默认填充是灰色的。这是由于文件格式吗?由于 geom_tile 的默认灰色填充忽略了 fill="transparent" 选项?其他原因?


我觉得我正在接近这个错误。我可能不必要地将矩阵格式的原始栅格数据转换为 x,y,z data.frame 格式...因为我更了解 data.frame 格式。

第一部分: 我可以使用 ggplot 在一个光栅上绘制另一个光栅而不得到不需要的线吗?如果是这样,如何?我还可以向叠加层添加 alpha=.5 透明度吗?

第二部分:有没有一种方法可以在不先转换为 x,y,z data.frame 格式的情况下绘制原始光栅矩阵格式?如果是这样,如何?我可以在叠加层上设置透明度吗?

通过一些工作,您可以使用 annotate 创建背景 而无需 映射,因此填充比例仍然适用于中心:

h$z <- (h$z - min(h$z)) / diff(range(h$z))
ggplot() + 
  coord_fixed() +
  annotate(
    geom = 'raster', 
    x = h$x, 
    y = h$y, 
    fill = scales::colour_ramp(c("black", "white"))(h$z)
  ) +
  geom_raster(aes(x, y, fill = r), data = r) +
  scale_fill_manual(values=c("red", "white", "blue", "yellow"))