美文网首页
ggplot学做NC文章分许柱状图、添加抖动点、添加误差线

ggplot学做NC文章分许柱状图、添加抖动点、添加误差线

作者: KS科研分享与服务 | 来源:发表于2022-07-13 15:40 被阅读0次

    今天使用R语言ggplot做一下NC文章的分组柱状图。原文提供原始作图数据,本文使用数据及整合、还有注释代码已上传群文件。

    更多内容请至我的公众号---KS科研分享与服务

    image.png

    (Reference:Senescent cells limit p53 activity via multiple mechanisms to remain viable)

    原文提供的两组基因表达数据,首先进行数据处理。

    
    sh1 <- read.csv("sh1.csv",header = T)
    sh2 <- read.csv("sh2.csv",header = T)
    

    数据转化与合并:

    
    library(tidyr)
    sh1 <-gather(sh1, gene, value, 1:7)
    sh1$group <- "sh1"
    sh2 <-gather(sh2, gene, value, 1:7)
    sh2$group <- "sh2"
    
    sh1<-na.omit(sh1)
    sh2<-na.omit(sh2)
    data <- rbind(sh1, sh2)
    

    因子排序:

    library(forcats)
    data$gene <- as.factor(data$gene)
    data$gene <- fct_inorder(data$gene)
    

    ggplot作图:

    
    ggplot(data, aes(fill=group, y=value, x=gene))+
      geom_bar(position=position_dodge(),
               stat="summary",
               width=0.7,
               colour = "black",
               size=1)+
      theme_classic(base_size = 12)+
      geom_hline(aes(yintercept=100),linetype=2,cex=0.5)+
      labs(title = "", y="Survival(%)", x = "")+
      scale_y_continuous(limits = c(0,150),expand = c(0,0))+
      theme(axis.text.x = element_text(size = 12,angle = 90, 
                                       color = 'black',face='italic',
                                       hjust = 1))+
      theme(axis.text.y = element_text(size = 12, color = 'black'))+
      theme(legend.title = element_text(color = 'red'))+
      geom_jitter(data = data, aes(y = value),
                  size = 3, shape = 16,
                  color="#91393B",
                  stroke = 0.15, show.legend = FALSE, 
                  position = position_jitterdodge(jitter.height=0.5,
                                                  jitter.width = 0.1,
                                                  dodge.width = 0.8))+ 
      stat_summary(fun.data = 'mean_se', 
                   geom = "errorbar", 
                   colour = "black",
                   width = 0.2,
                   position=position_dodge(0.7))+
      scale_fill_manual(values = c('#EE2024','#F69CA4'))+
      annotate(geom = 'text', label="n=9", x=0.8, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=9", x=1.2, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=12", x=2.2, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=9", x=2.8, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=9", x=3.2, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=12", x=3.8, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=12", x=4.2, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=12", x=4.8, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=12", x=5.2, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=12", x=5.8, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=12", x=6.2, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=12", x=6.8, y=10, angle=90,size=5)+
      annotate(geom = 'text', label="n=9", x=7.2, y=10, angle=90,size=5)
    

    这样就可以了,具体细节还需自己调整,P值的添加也可以用annotate函数。

    image.png

    相关文章

      网友评论

          本文标题:ggplot学做NC文章分许柱状图、添加抖动点、添加误差线

          本文链接:https://www.haomeiwen.com/subject/llhtirtx.html