简体   繁体   中英

R Nesting fill colours and gradient colours

I have a plot similar to this

X轴:日期,Y轴:重量,填充变量:品种

Using the following data:

  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

I did the plot with the following code:

ggplot(cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar)) + 
  geom_bar(position="dodge") + 
  scale_fill_manual (values =c("#90353B", "burlywood1"))

What I would like to do: Assign different density/luminosity of colours to the C52 ( burlywood1 )depending on the value of se, so higher values of se have a darker tone and lower values a lighter yellow tone.

I have tried to nest the scale_colour_gradient2 in the scale_fill_manual but but it didn't work. Is there any way to do this?

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)))) 

Just map se to alpha in the veins of:

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"))

This creates alpha values ranging from 0.5 to 1 (= opaque).

Edit:

If you just want 1 color to be transparent, add another alpha column eg like this:

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"))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM