R语言绘制分组柱状图
解决问题:多组数据不同属性间的比较。例如不同等级降雨量在SSP245和SSP585两种情景下的比较。横坐标为不同等级降雨的分组数据,每一组数据分别包含ssp245和ssp585两种情景。
library(ggplot2)
#读取数据
stat <- read.csv('J:/TYUT/R/stat1.csv',stringsAsFactors = FALSE)
#factor函数排序坐标轴
stat$taxonomy<-factor(stat$taxonomy, levels = c('Alphaproteobacteria', 'Acidobacteria', 'Actinobacteria'))
stat$mean <- stat$mean * 100
stat$se <- stat$se * 100
#绘图(数据/横纵坐标/柱子宽度/误差棒/颜色/坐标标签/坐标范围)
p <- ggplot(stat, aes(taxonomy, mean, fill = group)) +
geom_col(position = position_dodge(width = 0.7), width = 0.7) +
geom_errorbar(aes(ymin = mean - se, ymax = mean + se), width = 0.25, size = 0.3, position = position_dodge(0.9)) +
scale_fill_manual(values = c("#3C5488B2","#00A087B2", "#F39B7FB2")) +
labs(title = NULL, x = NULL, y = 'Relative abundance (%)', fill = NULL) +
theme(panel.grid = element_blank(), panel.background = element_blank(), axis.line = element_line(colour = 'black'), legend.position = c(0.9, 0.85)) +
theme(axis.text.x = element_text(size = 11, angle = 45, hjust = 1)) +
scale_y_continuous(expand = c(0, 0), limit = c(0, 20))
数据
结果