使用geom_raster()或geom_tile或geom_rect()绘制多个图层 [英] Plotting multiple layers with geom_raster() or geom_tile or geom_rect()

查看:379
本文介绍了使用geom_raster()或geom_tile或geom_rect()绘制多个图层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a data.frame of b/w raster image data that I'm using as a background/base layer. It has three columns: x, y, z. Where x and y are the respective coordinates and z is a continuous value defining a 512 x 512 image. I set this background fill to a black-white gradient and plot using geom_raster() in ggplot2 and then save the image to a PDF file.

### 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)

这将在保存的文件中提供所需的结果.

This gives the desired result in the saved file.

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

Next, on top of that base layer I plot one or more additional layers. These layers also have x, y, and r. Where r is either a continuous or discrete value depending on the region. For example, a layer defining region borders would label the discrete regions, whereas a layer defining topology in the region would define the topology.

理想情况下,我想两次使用geom_raster(),对于每个层一次(基层,覆盖层),并在每个层上使用不同的aes(fill=),对于每个层也使用不同的scale_fill_*().但是,尝试执行此操作会导致错误:

Ideally, I'd like to use geom_raster() twice, once for each layer (base, overlay) and use a different aes(fill=) on each layer, with a different scale_fill_*() for each layer as well. However, attempting this results in errors:

# 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)):


I thought maybe I could do one geom_raster() with aes(fill=z) and one with the aes(colour=r) and scale_colour_manual but geom_raster() does not recognize the aes(colour=) option, so I used geom_tile(aes(colour=r)) instead:

# 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等)时,图块层上有不需要的线条网格.

The plot looks fine in RStudio's previewer window, but when saved as a file (pdf, png, etc.), the tile layer has a grid of unwanted lines on it.

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

I believe these lines are showing up because the geom_tile default fill is grey. Is this due to the file format? Due to the default grey fill of geom_tile ignoring the fill="transparent" option? Some other reason?

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

I feel like I am approaching this wrong. I am probably converting the original raster data in matrix format into x,y,z data.frame format unnecessarily... and because I understand data.frame format better.

第一部分::我可以使用ggplot绘制一个栅格而不是另一个栅格吗?如果是这样,怎么办?我还可以在覆盖层中添加alpha=.5透明度吗?

First part: Can I use ggplot to plot one raster over another without getting the unwanted lines? If so, how? Can I also add an alpha=.5 transparency to the overlay layer?

第二部分:是否可以在不首先转换为x,y,z data.frame格式的情况下绘制原始栅格矩阵格式的方法?如果是这样,怎么办?我可以在覆盖层上设置透明度吗?

Second part: Is there a way that I can plot the original raster matrix format without converting to x,y,z data.frame format first? If so, how? Can I set transparency on the overlay layer?

推荐答案

只需做一些工作,您就可以使用annotate创建背景而没有映射,因此填充比例仍可用于中心:

With a little work, you can use annotate to create the background without mapping, so the fill scales remains available for the center:

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"))

这篇关于使用geom_raster()或geom_tile或geom_rect()绘制多个图层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆