在网格中制作一个矩形图例,标有行和列 [英] Make a rectangular legend, with rows and columns labeled, in grid

查看:17
本文介绍了在网格中制作一个矩形图例,标有行和列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ggplot,我将因子映射到填充和 alpha,如下所示:

I've got a ggplot where I'm mapping factors to both fill and alpha, like this:

set.seed(47)
the_data <- data.frame(value = rpois(6, lambda=20),
                       cat1 = rep(c("A", "B"), each = 3),
                       cat2 = rep(c("X", "Y", "Z"), 2))

ggplot(the_data, aes(y = value, x = cat2, alpha = cat1, fill = cat2)) +
    geom_bar(stat = "identity", position = "dodge") +
    scale_alpha_discrete(range = c(0.5, 1)) +
    theme_bw()

我为之制作它的人并不觉得 alpha 的图例很清楚.我认为一个不错的选择是这样的(我在基本图形中将其组合在一起):

The people I'm producing it for don't find the legend for alpha very clear. I think a good alternative would be something like this (which I hacked together in base graphics):

我知道我无法使用高级 ggplot 命令生成这样的图例,但是我可以在 grid 中生成并将其放在我的绘图之上吗?

I know I can't generate a legend like that with high-level ggplot commands, but can I do it in grid and put it on top of my plot?

推荐答案

这是一个可能的起点.我创建了两个不同的图,它们具有适当的图例——明亮"和苍白".从绘图对象中提取图例.然后使用 grid viewports,一个用于情节,一个用于每个图例,将碎片放在一起.

Here is one possible starting point. I create two different plots which have the appropriate legends - a 'bright' and a 'pale'. Extract the legends from the plot objects. Then use grid viewports, one for the plot, and one for each legend, to put the pieces together.

library(grid)
library(gtable)

# create plot with legend with alpha = 1
g1 <- ggplot(the_data, aes(y = value, x = cat2, alpha = cat1, fill = cat2)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_alpha_discrete(range = c(0.5, 1)) +
  theme_bw() +
  guides(fill = guide_legend(title = "A",
                             title.hjust = 0.4),
         alpha = FALSE) +
  theme_bw() +
  theme(legend.text = element_blank())

g1

# grab legend
legend_g1 <- gtable_filter(ggplot_gtable(ggplot_build(g1)), "guide-box") 


# create plot with 'pale' legend
g2 <- ggplot(the_data, aes(y = value, x = cat2, alpha = cat1, fill = cat2)) +
  geom_bar(stat = "identity", position = "dodge") +
  scale_alpha_discrete(range = c(0.5, 1)) +
  guides(fill = guide_legend(override.aes = list(alpha = 0.5),
                             title = "B",
                             title.hjust = 0.3),
         alpha = FALSE) +
  theme_bw()
g2

# grab legend
legend_g2 <- gtable_filter(ggplot_gtable(ggplot_build(g2)), "guide-box") 



# arrange plot and legends

# legends to the right

# define plotting regions (viewports)
vp_plot <- viewport(x = 0.4, y = 0.5,
                    width = 0.8, height = 1)

vp_legend_g1 <- viewport(x = 0.85, y = 0.5,
                           width = 0.4, height = 0.4)

vp_legend_g2 <- viewport(x = 0.90, y = 0.5,
                           width = 0.4, height = 0.4)


# clear current device
grid.newpage()

# add objects to the viewports
# plot without legend
print(g1 + theme(legend.position = "none"), vp = vp_plot)
upViewport(0)

pushViewport(vp_legend_g1)
grid.draw(legend_g1)
upViewport(0)

pushViewport(vp_legend_g2)
grid.draw(legend_g2) 

# legends on top
vp_plot <- viewport(x = 0.5, y = 0.4,
                    width = 1, height = 0.85)

vp_legend_g1 <- viewport(x = 0.5, y = 0.9,
                         width = 0.4, height = 0.4)

vp_legend_g2 <- viewport(x = 0.55, y = 0.9,
                         width = 0.4, height = 0.4)

grid.newpage()

print(g1 + theme(legend.position = "none"), vp = vp_plot)
upViewport(0)

pushViewport(vp_legend_g1)
grid.draw(legend_g1)
upViewport(0)

pushViewport(vp_legend_g2)
grid.draw(legend_g2)  

这篇关于在网格中制作一个矩形图例,标有行和列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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