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

2023-02-22  本文已影响0人  KS科研分享与服务

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

图片 图片

读入数据整理一下:

setwd("D:/KS项目/公众号文章/蜂群图结合柱状图")
df <- read.csv("df.csv", header = T)

library(tidyr)
data <-gather(df, gene, value, 1:7)

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


library(ggbeeswarm)
library(ggplot2)
ggplot(data, aes(x=gene,y=value,shape=gene))+
  geom_bar(stat="summary",
           width=0.9,#宽度
           size=0.5,color='black', fill='white')+
  stat_summary(fun.data = 'mean_se', 
               geom = "errorbar", 
               colour = "black",
               width = 0.2,
               position=position_dodge(0.7))+
  geom_beeswarm(dodge.width = 0.8,aes(y = value,x=gene,fill=gene),
                size=3, show.legend = FALSE)+
  scale_shape_manual(values =c(21,21,22,22,21,21,21))+
  scale_fill_manual(values = c("black","#68317F","#E7628C","#00A66C",
                                      "#5363A5","#F6D6B7","orange"))+
  theme_classic()+
  theme(axis.text = element_text(size = 12, color="black"),
        axis.line.y = element_line(color = 'black'),
        axis.title.y = element_text(size = 14, color="black"))+
  theme(axis.title.x = element_blank())+
  theme(panel.grid = element_blank(),
        panel.background = element_blank())+
  scale_y_continuous(expand=c(0,0))
图片

看看普通抖动点的效果:


ggplot(data, aes(x=gene,y=value,shape=gene))+
  geom_bar(stat="summary",
           width=0.9,#宽度
           size=0.5,color='black', fill='white')+ 
  stat_summary(fun.data = 'mean_se', 
               geom = "errorbar", 
               colour = "black",
               width = 0.2,
               position=position_dodge(0.7))+
  geom_jitter(data = data, aes(y = value,x=gene,fill=gene),
              size = 4,
              stroke = 0.15, show.legend = FALSE, 
              position = position_jitterdodge(jitter.height=0.5,
                                              jitter.width = 0.1,
                                              dodge.width = 0.8))+
  scale_shape_manual(values =c(21,21,22,22,21,21,21))+
  scale_fill_manual(values = c("black","#68317F","#E7628C","#00A66C",
                                      "#5363A5","#F6D6B7","orange"))+
                                        theme_classic()+
  theme(axis.text = element_text(size = 12, color="black"),
        axis.line.y = element_line(color = 'black'),
        axis.title.y = element_text(size = 14, color="black"))+
  theme(axis.title.x = element_blank())+
  theme(panel.grid = element_blank(),
        panel.background = element_blank())+
  scale_y_continuous(expand=c(0,0))
图片

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

上一篇 下一篇

猜你喜欢

热点阅读