R并排分组箱图

时间:2014-11-06 10:49:33

标签: r colors legend boxplot

我有两种植物的气体排放的时间数据,这两种植物都经过相同的处理。使用一些previous help将此代码放在一起[编辑]:

soilflux = read.csv("soil_fluxes.csv") 
library(ggplot2)
soilflux$Treatment <- factor(soilflux$Treatment,levels=c("L-","C","L+"))
soilplot = ggplot(soilflux, aes(factor(Week), Flux, fill=Species, alpha=Treatment)) + stat_boxplot(geom ='errorbar') + geom_boxplot()
soilplot = soilplot + labs(x = "Week", y = "Flux (mg m-2 d-1)") + theme_bw(base_size = 12, base_family = "Helvetica")
soilplot

制作效果良好但有缺陷的产品。

img

虽然它传达了我需要的所有信息,尽管谷歌拖网并在这里查看,但我无法得到传说中的“治疗”部分,以表明L-轻,L +最暗。我也被告知单色配色方案更容易区分,因此我试图得到像this这样的传说清晰的东西。

img

1 个答案:

答案 0 :(得分:2)

作为一种解决方法,您可以创建speciestreatment的组合因子,并手动指定填充颜色:

library(ggplot2)
library(RColorBrewer)
d <- expand.grid(week = factor(1:4), species = factor(c("Heisteria", "Simarouba")),
                 trt = factor(c("C", "L-", "L+"), levels = c("L-", "C", "L+")))

d <- d[rep(1:24, each = 30), ]
d$flux <- runif(NROW(d))

# Create a combined factor for coding the color
d$spec.trt <- interaction(d$species, d$trt, lex.order = TRUE, sep = " - ")

ggplot(d, aes(x = week, y = flux, fill = spec.trt)) +
   stat_boxplot(geom ='errorbar') + geom_boxplot() +
   scale_fill_manual(values = c(brewer.pal(3, "Greens"), brewer.pal(3, "Reds"))) 

grouped boxplot