R ggplot透明度-阿尔法值取决于其他变量
我有以下数据:[来自R graphics Cookbook的示例]R ggplot透明度-阿尔法值取决于其他变量,r,colors,plot,ggplot2,R,Colors,Plot,Ggplot2,我有以下数据:[来自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
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
我想要一个条形图,其中条形图的透明度取决于big
变量
我尝试了以下方法,根据不同的big
值设置alpha
值:
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))
我希望根据大变量的值在条中有不同的透明度。非常感谢任何帮助或指导 这里的问题是变量是离散的,而
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))
最终结果:
另一种可能是使用
比例α离散
,其中范围
参数可用于为每个“大”级别设置所需的α
值
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))
我认为这是一个更好的解决方案,至少有一个正确的图例,并且不需要事先计算alpha值。对于以0为中心的连续变量,是否可以给出梯度而不是范围?比如说
c(0.9,0.3,0.9)
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))