R ggplot透明度 - alpha值取决于其他变量 [英] R ggplot transparency - alpha values conditional on other variable
问题描述
我有以下数据:[来自R graphics Cookbook的例子]
I have the following data: [example from R graphics Cookbook]
Cultivar Date Weight sd n se big
c39 d16 3.18 0.9566144 10 0.30250803 TRUE
c39 d20 2.8 0.2788867 10 0.08819171 TRUE
c39 d21 2.74 0.9834181 10 0.3109841 TRUE
c52 d16 2.26 0.4452215 10 0.14079141 FALSE
c52 d20 3.11 0.7908505 10 0.25008887 TRUE
c52 d21 1.47 0.2110819 10 0.06674995 FALSE
我想要一个条形图其中透明度依赖于大
变量。
I would like to have a bar plot where bar transparency depends on big
variable.
我尝试了以下方法,我尝试设置 alpha
值取决于不同的 big
值:
I have tried the following, where I tried to set alpha
values depending on different big
values:
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
geom_bar(position="dodge", stat="identity")
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
geom_bar(position="dodge", stat="identity", alpha=cabbage_exp$big=c("TRUE"= 0.9, "FALSE" = 0.35))
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) +
geom_bar(position="dodge", stat="identity", alpha=big=c("TRUE"= 0.9, "FALSE" = 0.35))
我想根据大变量的值在条中设置不同的透明度。任何帮助或指导极大的赞赏!
I would like to have different transparency in the bars depending on the value of the big variable. Any help or guidance hugely appreciated!
推荐答案
使用 scale_alpha_discrete
的另一种可能性,其中范围
参数可用于为每个'big'级别设置所需的 alpha
值。
Another possibility using scale_alpha_discrete
, where the range
argument may be used to set your desired alpha
values for each level of 'big'.
ggplot(data = cabbage_exp, aes(x = Date, y = Weight, fill = Cultivar, alpha = big)) +
geom_bar(position = "dodge", stat = "identity") +
scale_alpha_discrete(range = c(0.35, 0.9))
这篇关于R ggplot透明度 - alpha值取决于其他变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!