R版本与运行环境信息

  1. Date:2021-7-22
  2. R version 4.0.3 (2020-10-10)
  3. Platform: x86_64-w64-mingw32/x64 (64-bit)
  4. Running under: Windows 10 x64 (build 18363)

柱状图

  1. library(tidyverse)
  2. library(ggsci)
  3. library(ggbreak)
  4. plot_theme <-
  5. theme_bw()+
  6. theme(axis.title.x = element_text(size = 18),
  7. axis.title.y = element_text(size = 18),
  8. axis.text = element_text(size = 13,color = "black"))
  9. #数据集为三个不同地点的物种组成数据
  10. setwd("G:\\Desktop\\s_note\\data\\plot")
  11. df <- read_csv("df2.csv")
  12. > df
  13. # A tibble: 27 x 4
  14. group Phylum mean sd
  15. <chr> <chr> <dbl> <dbl>
  16. 1 site_A Acidobacteria 0.0665 0.0139
  17. 2 site_A Actinobacteria 0.0720 0.0151
  18. 3 site_A Bacteroidetes 0.143 0.0218
  19. 4 site_A Chloroflexi 0.0239 0.00538
  20. 5 site_A Firmicutes 0.0529 0.0227
  21. 6 site_A Gemmatimonadetes 0.0416 0.00800
  22. 7 site_A Oxyphotobacteria 0.00199 0.000669
  23. 8 site_A Proteobacteria 0.590 0.0318
  24. 9 site_A others 0.00822 0.00177
  25. 10 site_B Acidobacteria 0.0862 0.0111
  26. # ... with 17 more rows

分组柱状图和堆积图

分组柱状图

  1. #errorbar和bar以及point的 position_dodge() 要一样,否则会重叠
  2. ggplot(df) +
  3. geom_bar(aes(group,mean,fill = Phylum),
  4. stat = "identity",
  5. position = position_dodge(0.75),width = 0.8) +
  6. geom_errorbar(aes(group,sd,ymax = mean + sd,ymin = mean -sd,group = Phylum),
  7. position = position_dodge(0.75),
  8. alpha = 0.75,
  9. width = 0.5,
  10. size = 0.73) +
  11. scale_fill_npg() +
  12. geom_point(aes(group,mean,color = Phylum),
  13. position = position_dodge(0.75)) +
  14. scale_color_npg() +
  15. labs(y = "Relative abundance(%)",x = "Site")+
  16. ggtitle("Test of bar")+
  17. plot_theme

柱状图 - 图1

堆积图

  1. #将position_dodge改为position_fill即可
  2. ggplot(df) +
  3. geom_bar(aes(group,mean,fill = Phylum),
  4. stat = "identity",
  5. position = position_fill(0.5),width = 0.3) +
  6. scale_fill_npg() +
  7. labs(y = "Relative abundance(%)",x = "Site")+
  8. plot_theme

柱状图 - 图2