R并排分组箱图 [英] R Side-by-side grouped boxplot
问题描述
我有来自两种植物的气体排放的时间数据,这两种植物都经过相同的处理。对于某些,其中图例是清楚的。
作为一种解决方法,您可以创建 $ c>和
处理
并手动分配填充颜色:
(GGPLOT2)
库(RColorBrewer)
D&下; - expand.grid(星期=系数(1:4),物种=因子(三(Heisteria,Simarouba)),
trt =因子(c(C,L,L +),levels = c(L-,C,L +)))
d& - d [rep(1:24,each = 30),]
d $ flux< - runif(NROW(d))
#创建用于编码颜色$ b的组合因子$ bd $ spec.trt< - interaction(d $ species,d $ trt,lex.order = TRUE,sep = - )
ggplot(d,aes(x = week,y =流量,填写= spec.trt))+
stat_boxplot(GEOM ='errorbar')+ geom_boxplot()+
scale_fill_manual(值= C(brewer.pal(3,绿党),啤酒.pal(3,Reds)))
I have temporal data of gas emissions from two species of plant, both of which have been subjected to the same treatments. With some previous help to get this code together [edit]:
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
Producing this which works well but has its flaws.
Whilst it conveys all the information I need it to, despite Google trawls and looking through here I just couldn't get the 'Treatment' part of the legend to show that L- is light and L+ darkest. I've also been told that a monochrome colour scheme is easier to differentiate hence I'm trying to get something like this where the legend is clear.
As a workaround you could create a combined factor from species
and treatment
and assign the fill colors manually:
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")))
这篇关于R并排分组箱图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!