R版本与运行环境信息

  1. Date:2021-7-21
  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. #devtools::install_github('erocoar/gghalves')
  2. library(gghalves)
  3. library(tidyverse)
  4. library(ggsci)
  5. plot_theme <-
  6. theme_bw()+
  7. theme(axis.title.x = element_text(size = 18),
  8. axis.title.y = element_text(size = 18),
  9. axis.text = element_text(size = 13,color = "black"))
  10. #使用自带的iris数据集
  11. df <- iris %>% as_tibble() %>% select(Species,Sepal.Length,Petal.Length)
  12. df.mean <-
  13. df %>% group_by(Species) %>% summarise(mean=mean(Sepal.Length),
  14. sd = sd(Sepal.Length))

查看数据

  1. > df
  2. # A tibble: 150 x 3
  3. Species Sepal.Length Petal.Length
  4. <fct> <dbl> <dbl>
  5. 1 setosa 5.1 1.4
  6. 2 setosa 4.9 1.4
  7. 3 setosa 4.7 1.3
  8. 4 setosa 4.6 1.5
  9. 5 setosa 5 1.4
  10. 6 setosa 5.4 1.7
  11. 7 setosa 4.6 1.4
  12. 8 setosa 5 1.5
  13. 9 setosa 4.4 1.4
  14. 10 setosa 4.9 1.5
  15. > df.mean
  16. # A tibble: 3 x 3
  17. Species mean sd
  18. <fct> <dbl> <dbl>
  19. 1 setosa 5.01 0.352
  20. 2 versicolor 5.94 0.516
  21. 3 virginica 6.59 0.636

绘图

  1. ggplot(df,mapping = aes(Species,Sepal.Length,fill = Species)) +
  2. geom_half_boxplot(outlier.alpha = 0.8,
  3. width = 0.3,
  4. errorbar.length = 0.1 ) +
  5. geom_half_violin(side = "r",
  6. position=position_nudge(x = .12, y = 0),
  7. trim = F) +
  8. geom_half_point(aes(color = Species),
  9. position = position_nudge(x = -.5, y = 0),
  10. size = 1,
  11. alpha = 0.8,
  12. transformation = position_jitter(),
  13. side = "r") +
  14. geom_errorbar(df.mean,
  15. mapping = aes(x=Species,y=mean,ymax = mean + sd,ymin = mean - sd,color = Species),
  16. position = position_nudge(x = 0.06, y = 0),
  17. width = 0.05) +
  18. geom_point(df.mean,
  19. mapping = aes(x=Species,y=mean,color = Species),
  20. position = position_nudge(x = 0.06, y = 0)) +
  21. coord_flip() +
  22. scale_fill_npg()+
  23. scale_color_npg()+
  24. plot_theme +
  25. ggtitle("Test of gghalf",subtitle = "test")

云雨图 - 图1