在ggplot中添加第二个geom_tile层 [英] add a second geom_tile layer in ggplot

查看:42
本文介绍了在ggplot中添加第二个geom_tile层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ggplot2中使用 geom_tile 有一个相对简单的热图.它只是连续数据的一小矩阵,如彩色框( df1 ),我想覆盖第二个逻辑geom_tile,该轮廓概述了 TRUE 值( df2).这样的事可以做吗?我知道将两个热图加在一起似乎很丑,但是这是一件很小且非常简单的事情.

I have a relatively simple heatmap using geom_tile in ggplot2. It's just a small matrix of continuous data as colored boxes (df1) and I'd like to overlay a second, logical geom_tile that outlines the TRUE values (df2). Can such a thing be done? I know that adding two heatmaps together seems like it would be ugly but these are small and pretty simple affairs.

library(ggplot2)
n <- 4
df1 <- data.frame(x = rep(letters[1:n], times = n),
                  y = rep(1:n, each = n),
                  z = rnorm(n ^ 2)) 

df2 <- data.frame(x = rep(letters[1:n], times = n),
                  y = rep(1:n, each = n),
                  z = FALSE) 
df2$z[c(2,14)] <- TRUE

p1 <- ggplot(df1, aes(x = x, y = y))
p1 <- p1 + geom_tile(aes(fill = z), colour = "grey20")
p1 <- p1 + scale_fill_gradient2(low = "darkgreen", 
                                mid = "white", 
                                high = "darkred",
                                breaks = c(min(df1$z), max(df1$z)),
                                labels = c("Low", "High"))
p1
# overlay df2 to outline the TRUE boxes or dim the FALSE boxes with alpha?
# p1 <- p1 + geom_tile(data = df2, aes(fill = z), colour = "grey20")

推荐答案

只需将两个数据集合并为一个,以便可以将第一个z值映射为fill,另一个映射为alpha:

Just merge the two datasets into one so you can map the first z-value to fill, and the other to alpha:

ggplot(merge(df1, df2, by = c('x', 'y')), aes(x = x, y = y)) + 
    geom_tile(aes(fill = z.x, alpha = z.y), colour = "grey20") + 
    scale_fill_gradient2(low = "darkgreen", 
                         mid = "white", 
                         high = "darkred",
                         breaks = c(min(df1$z), max(df1$z)),
                         labels = c("Low", "High"))

您可以使用笔触颜色而不是alpha,但是笔触会被 geom_tile 部分覆盖:

You could use stroke color instead of alpha, but strokes get partially covered by geom_tile:

ggplot(merge(df1, df2, by = c('x', 'y')), aes(x = x, y = y)) + 
    geom_tile(aes(fill = z.x, colour = z.y), size = 2) + 
    scale_fill_gradient2(low = "darkgreen", 
                         mid = "white", 
                         high = "darkred",
                         breaks = c(min(df1$z), max(df1$z)),
                         labels = c("Low", "High")) + 
    scale_color_manual(values = c('#00000000', 'blue'))

因此,要使其正确绘制,就必须先用一层填充,然后再用透明填充的轮廓来破解它:

so to get it to plot properly, you'll have to hack it through with one layer for fills, then one layer for outlines with clear fill:

ggplot(merge(df1, df2, by = c('x', 'y')), aes(x = x, y = y)) + 
    geom_raster(aes(fill = z.x)) +
    geom_tile(aes(colour = z.y), fill = '#00000000', size = 2) + 
    scale_fill_gradient2(low = "darkgreen", 
                         mid = "white", 
                         high = "darkred",
                         breaks = c(min(df1$z), max(df1$z)),
                         labels = c("Low", "High")) + 
    scale_color_manual(values = c('#00000000', 'blue'))

这篇关于在ggplot中添加第二个geom_tile层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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