R ggplot2 比例 alpha 离散显示在图例中
R ggplot2 scale alpha discrete to display in legend
我正在尝试绘制两个因素(应变和性别)的图,并使用 alpha 值来传达性别。这是我的代码和结果图:
ggplot(subset(df.zfish.data.overall.long, day=='day_01' & measure=='distance.from.bottom'), aes(x=Fish.name, y=value*100)) +
geom_boxplot(aes(alpha=Sex, fill=Fish.name), outlier.shape=NA) +
scale_alpha_discrete(range=c(0.3,0.9)) +
scale_fill_brewer(palette='Set1') +
coord_cartesian(ylim=c(0,10)) +
ylab('Distance From Bottom (cm)') +
xlab('Strain') +
scale_x_discrete(breaks = c('WT(AB)', 'WT(TL)', 'WT(TU)', 'WT(WIK)'), labels=c('AB', 'TL', 'TU', 'WIK')) +
guides(color=guide_legend('Fish.name'), fill=FALSE) +
theme_classic(base_size=10)
我希望图例能够将图中的 alpha 值(即 alpha 值 F = 0.3,alpha 值 M=0.9)反映为 greyscale/black,因为我认为这很直观。
我试过更改 scale_alpha_discrete,但不知道如何为图例发送单一颜色。我也试过玩 'guides()' 但运气不佳。我怀疑有一个简单的解决方案,但我看不到它。
实现所需结果的一个选项是通过 guide_legend
的 override.aes
参数设置 alpha
图例的填充颜色。
使用 mtcars
作为示例数据:
library(ggplot2)
ggplot(mtcars, aes(x = cyl, y = mpg)) +
geom_boxplot(aes(fill = factor(cyl), alpha = factor(am))) +
scale_alpha_discrete(range = c(0.3, 0.9), guide = guide_legend(override.aes = list(fill = "black"))) +
scale_fill_brewer(palette='Set1') +
theme_classic(base_size=10) +
guides(fill = "none")
#> Warning: Using alpha for a discrete variable is not advised.
我正在尝试绘制两个因素(应变和性别)的图,并使用 alpha 值来传达性别。这是我的代码和结果图:
ggplot(subset(df.zfish.data.overall.long, day=='day_01' & measure=='distance.from.bottom'), aes(x=Fish.name, y=value*100)) +
geom_boxplot(aes(alpha=Sex, fill=Fish.name), outlier.shape=NA) +
scale_alpha_discrete(range=c(0.3,0.9)) +
scale_fill_brewer(palette='Set1') +
coord_cartesian(ylim=c(0,10)) +
ylab('Distance From Bottom (cm)') +
xlab('Strain') +
scale_x_discrete(breaks = c('WT(AB)', 'WT(TL)', 'WT(TU)', 'WT(WIK)'), labels=c('AB', 'TL', 'TU', 'WIK')) +
guides(color=guide_legend('Fish.name'), fill=FALSE) +
theme_classic(base_size=10)
我希望图例能够将图中的 alpha 值(即 alpha 值 F = 0.3,alpha 值 M=0.9)反映为 greyscale/black,因为我认为这很直观。
我试过更改 scale_alpha_discrete,但不知道如何为图例发送单一颜色。我也试过玩 'guides()' 但运气不佳。我怀疑有一个简单的解决方案,但我看不到它。
实现所需结果的一个选项是通过 guide_legend
的 override.aes
参数设置 alpha
图例的填充颜色。
使用 mtcars
作为示例数据:
library(ggplot2)
ggplot(mtcars, aes(x = cyl, y = mpg)) +
geom_boxplot(aes(fill = factor(cyl), alpha = factor(am))) +
scale_alpha_discrete(range = c(0.3, 0.9), guide = guide_legend(override.aes = list(fill = "black"))) +
scale_fill_brewer(palette='Set1') +
theme_classic(base_size=10) +
guides(fill = "none")
#> Warning: Using alpha for a discrete variable is not advised.