前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Nature Communications学作图:R语言ggplot2做柱形图并添加误差线和显著性P值

跟着Nature Communications学作图:R语言ggplot2做柱形图并添加误差线和显著性P值

作者头像
用户7010445
发布2021-11-16 15:37:54
3.3K0
发布2021-11-16 15:37:54
举报
文章被收录于专栏:小明的数据分析笔记本

论文是

A giant NLR gene confers broad-spectrum resistance to Phytophthora sojae in soybean

image.png

论文里公布了大部分柱形图和箱线图的原始数据,今天的推文试着用论文中的数据模仿一下论文中的 Figure 2b c

image.png

Figure 2b 的数据

image.png

类似的图之前录制过视频进行介绍,如果习惯看视频的话可以关注下我的B站账号 小明的数据分析笔记本

image.png

首先读取数据

代码语言:javascript
复制
dfb<-read.csv("figure2b.csv",header=F)
dfb

宽格式数据转换为长格式

代码语言:javascript
复制
dfb %>% 
  pivot_longer(!V1) %>% 
  select(V1,value) %>% 
  na.omit() -> dfb.1

最基本的柱形图

代码语言:javascript
复制
library(ggplot2)

ggplot(data=dfb.1,aes(x=V1,y=value))+
  stat_summary(geom = "bar",
               fun = mean,
               fill="#c6c3c3")

image.png

添加误差线

这里误差线采用的是mean+-sem

代码语言:javascript
复制
library(ggplot2)

ebtop<-function(x){
  return(mean(x)+sd(x)/sqrt(length(x)))
}
ebbottom<-function(x){
  return(mean(x)-sd(x)/sqrt(length(x)))
}

ggplot(data=dfb.1,aes(x=V1,y=value))+
  stat_summary(geom = "bar",
               fun = mean,
               fill="#c6c3c3")+
  stat_summary(geom = "errorbar",
               fun.min = ebbottom,
               fun.max = ebtop,
               width=0.2)

image.png

添加图上的散点

代码语言:javascript
复制
library(ggplot2)

ebtop<-function(x){
  return(mean(x)+sd(x))
}
ebbottom<-function(x){
  return(mean(x)-sd(x))
}

ggplot(data=dfb.1,aes(x=V1,y=value))+
  stat_summary(geom = "bar",
               fun = mean,
               fill="#c6c3c3")+
  stat_summary(geom = "errorbar",
               fun.min = ebbottom,
               fun.max = ebtop,
               width=0.2)+
  geom_jitter(width = 0.3)

image.png

添加显著性p值

代码语言:javascript
复制
ggplot(data=dfb.1,aes(x=V1,y=value))+
  stat_summary(geom = "bar",
               fun = mean,
               fill="#c6c3c3")+
  stat_summary(geom = "errorbar",
               fun.min = ebbottom,
               fun.max = ebtop,
               width=0.2)+
  geom_jitter(width = 0.3)+
  geom_signif(comparisons = list(c("Control","F5"),
                                 c("Control","pAtUbi3:CDS-Rps11-1"),
                                 c("Control","pAtUbi3:CDS-Rps11-2")),
              test = t.test,
              test.args = list(var.equal=T,
                               alternative="two.side"),
              y_position = c(1.1,1.3,1.5),
              #annotations = c(""),
              parse = T)

image.png

如何在geom_signif()函数里调整P值的文字格式暂时想不到办法了,使用annotate()函数吧

代码语言:javascript
复制
ggplot(data=dfb.1,aes(x=V1,y=value))+
  stat_summary(geom = "bar",
               fun = mean,
               fill="#c6c3c3")+
  stat_summary(geom = "errorbar",
               fun.min = ebbottom,
               fun.max = ebtop,
               width=0.2)+
  geom_jitter(width = 0.3)+
  geom_signif(comparisons = list(c("Control","F5"),
                                 c("Control","pAtUbi3:CDS-Rps11-1"),
                                 c("Control","pAtUbi3:CDS-Rps11-2")),
              test = t.test,
              test.args = list(var.equal=T,
                               alternative="two.side"),
              y_position = c(1.1,1.3,1.5),
              annotations = c(""),
              parse = T)+
  annotate(geom = "text",
           x=1.5,y=1.15,
           label=expression(italic(P)~'='~1.83%*%10^-6))+
  annotate(geom = "text",
           x=2,y=1.35,
           label=expression(italic(P)~'='~2.71%*%10^-5))+
  annotate(geom = "text",
           x=2.5,y=1.55,
           label=expression(italic(P)~'='~5.75%*%10^-8))

image.png

这里遇到的警告信息暂时搞不懂是什么意思了

image.png

接下来是细节的调整

代码语言:javascript
复制
ggplot(data=dfb.1,aes(x=V1,y=value))+
  stat_summary(geom = "bar",
               fun = mean,
               fill="#c6c3c3")+
  stat_summary(geom = "errorbar",
               fun.min = ebbottom,
               fun.max = ebtop,
               width=0.2)+
  geom_jitter(width = 0.3)+
  geom_signif(comparisons = list(c("Control","F5"),
                                 c("Control","pAtUbi3:CDS-Rps11-1"),
                                 c("Control","pAtUbi3:CDS-Rps11-2")),
              test = t.test,
              test.args = list(var.equal=T,
                               alternative="two.side"),
              y_position = c(1.1,1.3,1.5),
              annotations = c(""),
              parse = T)+
  annotate(geom = "text",
           x=1.5,y=1.15,
           label=expression(italic(P)~'='~1.83%*%10^-6))+
  annotate(geom = "text",
           x=2,y=1.35,
           label=expression(italic(P)~'='~2.71%*%10^-5))+
  annotate(geom = "text",
           x=2.5,y=1.55,
           label=expression(italic(P)~'='~5.75%*%10^-8))+
  scale_y_continuous(expand = c(0,0),
                     limits = c(0,1.6),
                     breaks = seq(0,1,0.2))+
  theme_minimal()+
  theme(panel.grid = element_blank(),
        axis.line.y = element_line(),
        axis.ticks.y = element_line(),
        axis.title.y = element_text(hjust=0.25,
                                    size=15),
        axis.text.x = element_text(angle = 30,
                                   hjust = 1,
                                   size=10))+
  guides(y=guide_axis_truncated(trunc_lower = 0,
                               trunc_upper = 1))+
  labs(x=NULL,y="Survival Rate")

image.png

Figure 2c 的数据也有,大家可以试着用以上代码试着复现一下figure2c的数据

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-11-09,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 小明的数据分析笔记本 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文是
  • 首先读取数据
  • 宽格式数据转换为长格式
  • 最基本的柱形图
  • 添加误差线
  • 添加图上的散点
  • 添加显著性p值
  • 接下来是细节的调整
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档