当前位置:   article > 正文

复现Nature medicine图表:柱状图结合蜂群散点图_nature 柱状图美化

nature 柱状图美化

今天我们来复现一下一篇NM上的图表,是柱状图结合和散点图,只不过散点图是蜂群散点图。关于蜂群图的做法我们之前介绍过这个R包了,参考:复现《nature communications》散点小提琴图+蜜蜂图。示例数据及代码已上传群文件!!!

读入数据整理一下:

  1. setwd("D:/KS项目/公众号文章/蜂群图结合柱状图")
  2. df <- read.csv("df.csv", header = T)
  3. library(tidyr)
  4. data <-gather(df, gene, value, 1:7)

作图,主要是用ggbeeswarm包实现效果。

  1. library(ggbeeswarm)
  2. library(ggplot2)
  3. ggplot(data, aes(x=gene,y=value,shape=gene))+
  4. geom_bar(stat="summary",
  5. width=0.9,#宽度
  6. size=0.5,color='black', fill='white')+
  7. stat_summary(fun.data = 'mean_se',
  8. geom = "errorbar",
  9. colour = "black",
  10. width = 0.2,
  11. position=position_dodge(0.7))+
  12. geom_beeswarm(dodge.width = 0.8,aes(y = value,x=gene,fill=gene),
  13. size=3, show.legend = FALSE)+
  14. scale_shape_manual(values =c(21,21,22,22,21,21,21))+
  15. scale_fill_manual(values = c("black","#68317F","#E7628C","#00A66C",
  16. "#5363A5","#F6D6B7","orange"))+
  17. theme_classic()+
  18. theme(axis.text = element_text(size = 12, color="black"),
  19. axis.line.y = element_line(color = 'black'),
  20. axis.title.y = element_text(size = 14, color="black"))+
  21. theme(axis.title.x = element_blank())+
  22. theme(panel.grid = element_blank(),
  23. panel.background = element_blank())+
  24. scale_y_continuous(expand=c(0,0))

看看普通抖动点的效果:

  1. ggplot(data, aes(x=gene,y=value,shape=gene))+
  2. geom_bar(stat="summary",
  3. width=0.9,#宽度
  4. size=0.5,color='black', fill='white')+
  5. stat_summary(fun.data = 'mean_se',
  6. geom = "errorbar",
  7. colour = "black",
  8. width = 0.2,
  9. position=position_dodge(0.7))+
  10. geom_jitter(data = data, aes(y = value,x=gene,fill=gene),
  11. size = 4,
  12. stroke = 0.15, show.legend = FALSE,
  13. position = position_jitterdodge(jitter.height=0.5,
  14. jitter.width = 0.1,
  15. dodge.width = 0.8))+
  16. scale_shape_manual(values =c(21,21,22,22,21,21,21))+
  17. scale_fill_manual(values = c("black","#68317F","#E7628C","#00A66C",
  18. "#5363A5","#F6D6B7","orange"))+
  19. theme_classic()+
  20. theme(axis.text = element_text(size = 12, color="black"),
  21. axis.line.y = element_line(color = 'black'),
  22. axis.title.y = element_text(size = 14, color="black"))+
  23. theme(axis.title.x = element_blank())+
  24. theme(panel.grid = element_blank(),
  25. panel.background = element_blank())+
  26. scale_y_continuous(expand=c(0,0))

仔细思考阅读,对于初学者在ggplot的绘图学习上我想会有很大的帮助。觉得分享有用的点个赞再走呗!

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/131751
推荐阅读
相关标签
  

闽ICP备14008679号