【发布时间】:2016-11-15 14:57:11
【问题描述】:
我正在尝试结合 alpha 和 fill 美学。它在我使用geom_bar(或geom_points,对于color)时有效,但是当我使用geom_boxplot 时,alpha 图例不起作用。
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 图例是空的。
ggplot(dt, aes(x = x, y = y, fill = tag1, alpha = tag2,
group = interaction(x, tag1, tag2))) +
geom_boxplot()
一个更简单的版本可以不用填充 - 似乎条形默认为灰色/浅灰色,而箱线图默认为白色/浅白色:
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()
但我不确定如何解决这个问题。有什么想法吗?
【问题讨论】:
标签: r ggplot2 scale legend alpha