R版本与运行环境信息
Date:2021-7-22
R version 4.0.3 (2020-10-10)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 18363)
柱状图
library(tidyverse)
library(ggsci)
library(ggbreak)
plot_theme <-
theme_bw()+
theme(axis.title.x = element_text(size = 18),
axis.title.y = element_text(size = 18),
axis.text = element_text(size = 13,color = "black"))
#数据集为三个不同地点的物种组成数据
setwd("G:\\Desktop\\s_note\\data\\plot")
df <- read_csv("df2.csv")
> df
# A tibble: 27 x 4
group Phylum mean sd
<chr> <chr> <dbl> <dbl>
1 site_A Acidobacteria 0.0665 0.0139
2 site_A Actinobacteria 0.0720 0.0151
3 site_A Bacteroidetes 0.143 0.0218
4 site_A Chloroflexi 0.0239 0.00538
5 site_A Firmicutes 0.0529 0.0227
6 site_A Gemmatimonadetes 0.0416 0.00800
7 site_A Oxyphotobacteria 0.00199 0.000669
8 site_A Proteobacteria 0.590 0.0318
9 site_A others 0.00822 0.00177
10 site_B Acidobacteria 0.0862 0.0111
# ... with 17 more rows
分组柱状图和堆积图
分组柱状图
#errorbar和bar以及point的 position_dodge() 要一样,否则会重叠
ggplot(df) +
geom_bar(aes(group,mean,fill = Phylum),
stat = "identity",
position = position_dodge(0.75),width = 0.8) +
geom_errorbar(aes(group,sd,ymax = mean + sd,ymin = mean -sd,group = Phylum),
position = position_dodge(0.75),
alpha = 0.75,
width = 0.5,
size = 0.73) +
scale_fill_npg() +
geom_point(aes(group,mean,color = Phylum),
position = position_dodge(0.75)) +
scale_color_npg() +
labs(y = "Relative abundance(%)",x = "Site")+
ggtitle("Test of bar")+
plot_theme
堆积图
#将position_dodge改为position_fill即可
ggplot(df) +
geom_bar(aes(group,mean,fill = Phylum),
stat = "identity",
position = position_fill(0.5),width = 0.3) +
scale_fill_npg() +
labs(y = "Relative abundance(%)",x = "Site")+
plot_theme