ggplot2绘图跟着SCI学作图good code

复现《nature communications》散点小提琴图+

2022-06-23  本文已影响0人  KS科研分享与服务

今天我们学做一下NC文章的小提琴图,有小提琴图,也有散点,其实看过之前系列文章的人如果能够联想,可以想到这个图是(ggplot分组散点图-坐标轴截断-添加四分位图-显著性检验)和(ggplot批量绘制小提琴图并添加趋势连线)的结合。只不过这篇文章的图有个特点是散点分布和小提琴图形状一致,在画散点的时候利用geom_quasirandom 代替geom_jitter即可。

image.png image.png

原文提供了原始作图数据,可去官网下载。

示例数据和注释代码已上传群文件,免费获取可加群!

作图:

读入数据

setwd("D:/KS项目/复现NC")
A <- read.csv("Fig3f.csv", header = T)
A$Integrated.density <- 0.001*A$Integrated.density

library(ggplot2)
library(ggbeeswarm)
library(ggpubr)
A$Biological.replicate <- as.factor(A$Biological.replicate)

计算平均值、sd等:

library(dplyr)
B <- A %>% 
  group_by(Treatment) %>% 
  mutate(upper =  quantile(Integrated.density, 0.75),
         lower = quantile(Integrated.density, 0.25),
         mean = mean(Integrated.density),
         median = median(Integrated.density),
         sd = sd(Integrated.density))

ggplot作图:

ggplot(A,aes(x=Treatment,y=Integrated.density))+
  geom_violin(width =0.8,fill='#EDEDED',color='#EDEDED')+
  geom_quasirandom(aes(color=Biological.replicate),width = 0.4,size=2.5)+
  scale_color_manual(name = 'Rep.',
                     values = c('#FFD7A8','#F2A9A9','#BAB099'),
                     labels = c('1','2','3'))+ 
  theme_classic()+
  labs(x=" ",
       y=expression('Integrated density (x'~10^3~')'))+ 
  theme(axis.title.y = element_text(colour = 'black',size = 16),
        axis.text = element_text(colour = 'black',size = 14),
        axis.line = element_line(size = 1),
        legend.title = element_text(size = 14),
        legend.text = element_text(size = 14))+
  guides(color=guide_legend(override.aes = list(size=4)))+
  geom_errorbar(data=B, aes(ymin = mean-sd, 
                            ymax = mean+sd),width = 0.2,size=0.5)+
  stat_summary(fun = "mean",
               geom = "crossbar",
               mapping = aes(ymin=..y..,ymax=..y..),
               width=0.4,
               size=0.3)+
  stat_summary(aes(fill=Biological.replicate), geom="point",
               fun = mean, shape=21, size=6,stroke=1.3)+
  scale_fill_manual(values = c('#FFAF51','#E65454','#756233'))+
  geom_signif(data=A,
             aes(xmin=1, xmax=2, annotations="0.3008367", 
                 y_position=310),
             textsize = 5,tip_length = c(0, 0),
             manual=TRUE, size = 0.5)+
  guides(fill=guide_legend(title = 'Mean'))
image.png

结果基本是一致的,不同之处在于误差线,我是按照mean±sd,和原文有出入。其次Mean的图例,这里没有修改,不知有啥好办法,原文只有一个圈,其实用annotate函数可以加上。

更多精彩请关注我的公众号《KS科研分享与服务》

上一篇下一篇

猜你喜欢

热点阅读