R嵌套填充颜色和渐变颜色

R嵌套填充颜色和渐变颜色,r,colors,ggplot2,nested,R,Colors,Ggplot2,Nested,我有一个类似的情节 使用以下数据: Cultivar Date Weight sd n se 1 c39 d16 3.18 0.9566144 10 0.30250803 2 c39 d20 2.80 0.2788867 10 0.08819171 3 c39 d21 2.74 0.9834181 10 0.31098410 4 c52 d16 2.26 0.4452215 10 0.14079

我有一个类似的情节

使用以下数据:

  Cultivar Date Weight        sd  n         se
1      c39  d16   3.18 0.9566144 10 0.30250803
2      c39  d20   2.80 0.2788867 10 0.08819171
3      c39  d21   2.74 0.9834181 10 0.31098410
4      c52  d16   2.26 0.4452215 10 0.14079141
5      c52  d20   3.11 0.7908505 10 0.25008887
6      c52  d21   1.47 0.2110819 10 0.06674995
我用以下代码进行了绘图:

ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) + 
  geom_bar(position="dodge") + 
  scale_fill_manual (values =c("#90353B", "burlywood1"))
我想做的是:根据se的值为C52(
burlywood1
)分配不同的颜色密度/亮度,因此se的值越高,色调越深,值越低,色调越浅

我曾尝试在
缩放填充手册
中嵌套
缩放颜色梯度2
,但没有成功。有没有办法做到这一点

ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) + 
  geom_bar(position="dodge") + 
  scale_fill_manual (values =c("#90353B", scale_colour_gradient2(low="#22FF00", mid="white", high="#FF0000", midpoint=median(cabbage_exp$se)))) 

只需将
se
映射到
alpha
的静脉中:

library(ggplot2)
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar, alpha = se)) + 
geom_bar(position="dodge", stat = "identity") + 
scale_alpha(range = c(.5, 1)) + 
scale_fill_manual (values =c("#90353B", "burlywood1"))
这将创建范围为0.5到1(=不透明)的alpha值

编辑:

如果您只希望1种颜色是透明的,请添加另一个alpha列,例如:

cabbage_exp$alpha[cabbage_exp$Cultivar == "c52"] <- scales::rescale(cabbage_exp$se[cabbage_exp$Cultivar == "c52"], to = c(.5, 1)) 
ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar, alpha = alpha)) + 
geom_bar(position="dodge", stat = "identity") + 
scale_alpha(range = c(.5, 1)) + 
scale_fill_manual (values =c("#90353B", "burlywood1"))
collable\u exp$alpha[collable\u exp$richar==“c52”]