ggplot2 boxplots 中的 alpha 和填充图例? [英] alpha and fill legends in ggplot2 boxplots?

查看:70
本文介绍了ggplot2 boxplots 中的 alpha 和填充图例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试结合 alphafill 美学.当我使用 geom_bar(或 geom_points,用于 color)时它有效,但是当我使用时 alpha 图例不起作用使用 geom_boxplot.

I'm trying to combine alpha and fill aesthetics. It works when I'm using geom_bar (or geom_points, for color), but the alpha legend doesn't work in when I'm using geom_boxplot.

library(data.table)
library(ggplot2)
dt = data.table(x = rep(1:5,6), y = rnorm(30),
               tag1 = rep(c('hey', 'what'), 15),
               tag2 = rep(c('yeah', 'yeah', 'so', 'so', 'so'), 6))

它适用于酒吧:

ggplot(dt[, list(y = mean(y)), by = list(x, tag1, tag2)],
       aes(x = x, y = y, fill = tag1, alpha = tag2,
           group = interaction(x,tag1,tag2))) +
  geom_bar(stat = 'identity', position = 'dodge')

但不适用于箱线图 - alpha 图例为空.

But not for boxplot - the alpha legend is empty.

ggplot(dt, aes(x = x, y = y, fill = tag1, alpha = tag2,
               group = interaction(x, tag1, tag2))) +
  geom_boxplot()

可以在没有填充的情况下完成更简单的版本 - 似乎条形默认为灰色/浅灰色,而箱线图默认为白色/浅白色:

A simpler version can be done with no fill - it seems like bar defaults to gray/lightgray, and boxplot defaults to white/lightwhite:

ggplot(dt[, list(y = mean(y)), by = list(x, tag2)],
       aes(x = x, y = y, alpha = tag2,
           group = interaction(x,tag2))) +
 geom_bar(stat = 'identity')

ggplot(dt, aes(x = x, y = y, alpha = tag2,
               group = interaction(x, tag2))) +
  geom_boxplot()

但我不确定如何解决这个问题.有什么想法吗?

But I'm not really sure how to fix this. Any thoughts?

推荐答案

我不知道为什么 ggplot 实际上不为箱线图提供图例中的 alpha 级别,但是您可以使用 override 对其进行硬编码.aes.(编者注:我发现 alpha 美学对于箱线图或条形图都有些混乱.很难在心理上将透明度与填充颜色分开,灰度 alpha 图例加剧了这个问题,因为没有任何东西被映射到灰色情节.)

I'm not sure why ggplot doesn't actually provide the alpha levels in the legend for boxplots, but you can hard code it using override.aes. (Editorial note: I find the alpha aesthetic a bit confusing for either the boxplot or the bar plot. It's hard to mentally separate the transparency from the fill color and the grey-scale alpha legend exacerbates the problem, because nothing is mapped to grey in the plot.)

在下面的代码中,为了提高图例的可见性,我从 alpha 图例中删除了框线并增加了图例键的高度.我还编辑了美学以消除对 group 参数的需要.

In the code below, to improve visibility of the legend, I've removed the box lines from the alpha legend and increased the legend key height. I've also edited the aesthetics to remove the need for the group argument.

ggplot(dt, aes(x=factor(x), y=y, fill=tag1, alpha=tag2)) + 
  geom_boxplot() +
  scale_alpha_manual(values=c(0.2,0.7)) +
  guides(alpha=guide_legend(override.aes=list(fill=hcl(c(15,195),100,0,alpha=c(0.2,0.7)),
                                              colour=NA))) +
  theme(legend.key.height=unit(1,"cm"))

另一种选择是将 interaction 用于填充和 alpha 美学,但事实证明 ggplot 在这种情况下不包含任何颜色:

Another option would be to use interaction for both the fill and alpha aesthetics, but it turns out ggplot doesn't include any colors in that case:

ggplot(dt, aes(x=factor(x), y=y, alpha=interaction(tag1,tag2)), 
       fill=interaction(tag1,tag2)) + 
  geom_boxplot() +
  scale_fill_manual(values=rep(hcl(c(15,195),100,65), 2)) +
  scale_alpha_manual(values=rep(c(0.3, 1), each=2)) +
  theme(legend.key.height=unit(2,"cm")) 

因此,您可以使用填充美感来完成所有操作,但在颜色规范中包含透明度.这是有效的,但是,再一次,因为透明度和颜色在视觉感知中有些混合,所以最好使用四种不同的颜色.

So, instead you can do it all with the fill aesthetic, but include transparency in the colour specification. This works, but, once again, because transparency and color are somewhat intermingled in visual perception, it's probably better to just go with four different colors.

ggplot(dt, aes(x=factor(x), y=y, fill=interaction(tag1,tag2,sep="-"))) + 
  geom_boxplot() +
  scale_fill_manual(values=hcl(c(15,195,15,195),100,65, alpha=c(0.4,0.4,1,1))) +
  theme(legend.key.height=unit(1,"cm")) +
  labs(fill="Tag 1 - Tag 2")

这篇关于ggplot2 boxplots 中的 alpha 和填充图例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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