use*_*584 14 plot r colors ggplot2
我有以下数据:[来自R图形食谱的例子]
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
Run Code Online (Sandbox Code Playgroud)
我想要一个条形图,其中条形透明度取决于big变量.
我尝试了以下内容,我尝试alpha根据不同的big值设置值:
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))
Run Code Online (Sandbox Code Playgroud)
我想在条形图中有不同的透明度,具体取决于大变量的值.任何帮助或指导非常感谢!
Hen*_*rik 27
使用的另一种可能性scale_alpha_discrete,其中range参数可用于alpha为每个"大"级别设置所需的值.
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))
Run Code Online (Sandbox Code Playgroud)

这里的问题是你的变量是离散的,而alpha 规模是连续的.一种方法是在绘制之前手动计算alpha值:
alpha <- ifelse(d$big, 0.9, 0.35)
ggplot(d, aes(x=Date, y=Weight, fill=Cultivar)) +
geom_bar(position="dodge", stat="identity", aes(alpha=alpha))
Run Code Online (Sandbox Code Playgroud)
缺点是你不会得到正确的alpha值传奇.你可以用这个删除它:
ggplot(d, aes(x=Date, y=Weight, fill=Cultivar)) +
geom_bar(position="dodge", stat="identity", aes(alpha=big)) +
scale_alpha_continuous(guide=FALSE)
Run Code Online (Sandbox Code Playgroud)
最后结果 :
