【问题标题】:Plotting multiple layers with geom_raster() or geom_tile or geom_rect()使用 geom_raster() 或 geom_tile 或 geom_rect() 绘制多个图层
【发布时间】:2017-09-09 18:00:25
【问题描述】:

我有一个 b/w 光栅图像数据的 data.frame,用作背景/基础层。它有三列: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"))

错误信息:

“填充”比例已存在。为“填充”添加另一个比例,它将替换现有比例。 错误:提供给离散刻度的连续值


我想也许我可以用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 的预览器窗口中看起来不错,但是当保存为文件(pdf、png 等)时,平铺层上有一个不需要的线条网格。

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


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

第一部分:我可以使用ggplot 将一个光栅绘制在另一个光栅上而不会出现不需要的线条吗?如果是这样,怎么做?我还可以在覆盖层中添加alpha=.5 透明度吗?

第二部分:有没有一种方法可以绘制原始光栅矩阵格式,而无需先转换为 x,y,z data.frame 格式?如果是这样,怎么做?可以在叠加层上设置透明度吗?

【问题讨论】:

  • 我也试过了,但没有找到保存文件中没有行的任何大小。

标签: r image-processing plot ggplot2 raster


【解决方案1】:

稍加工作,您就可以使用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(data = r, aes(x,y,fill=r)) +
  scale_fill_manual(values=c("red","white","blue","yellow"))

【讨论】:

  • 酷!但是,如果我有多个覆盖层怎么办?
  • 视情况而定,要么将它们映射到一个共同的比例,要么进一步滥用annotate
  • 这样做的好处是它的渲染速度比使用geom_tile
猜你喜欢
  • 1970-01-01
  • 2019-09-18
  • 1970-01-01
  • 1970-01-01
  • 2021-03-03
  • 2021-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多