前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >跟着Nature Plants学作图:R语言ggplot2画分组折线图和置信区间

跟着Nature Plants学作图:R语言ggplot2画分组折线图和置信区间

作者头像
用户7010445
发布2022-05-23 15:58:14
1.4K0
发布2022-05-23 15:58:14
举报
文章被收录于专栏:小明的数据分析笔记本

论文

The flying spider-monkey tree fern genome provides insights into fern evolution and arborescence

https://www.nature.com/articles/s41477-022-01146-6#Sec44

数据下载链接

https://doi.org/10.6084/m9.figshare.19125641

今天的推文重复一下论文中的Figure1d中左下角的小图

image.png

论文中提供的原始数据集如下

image.png

需要将其整理成3个单独的数据集

image.png

首先是做数据整理的代码

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

dat01<-read_excel("data/20220518/dat01.xlsx")
dat01

dat01 %>% 
  mutate(x=8:2,
         mean_value=`Duplication Per`,
         lower95=`Duplication Per`,
         upper95=`Duplication Per`,
         group="Experimental") %>% 
  select(x,mean_value,lower95,upper95,group)-> dat01

dat02<-read_excel("data/20220518/dat02.xlsx")
head(dat02)
dat02 %>% 
  group_by(MRCA) %>% 
  summarise(mean_value = mean(`Duplication Per`),
            lower95 = Rmisc::CI(`Duplication Per`)[3],
            upper95 = Rmisc::CI(`Duplication Per`)[1]) %>% 
  mutate(x=8:2,
         group="Null simulation") %>% 
  select(x,mean_value,lower95,upper95,group) -> dat02

dat03<-read_excel("data/20220518/dat03.xlsx")
head(dat03)
dat03 %>% 
  group_by(MRCA) %>% 
  summarise(mean_value = mean(`Duplication Per`),
            lower95 = Rmisc::CI(`Duplication Per`)[3],
            upper95 = Rmisc::CI(`Duplication Per`)[1]) %>% 
  mutate(x=8:2,
         group="Positive simulation") %>% 
  select(x,mean_value,lower95,upper95,group) -> dat03

dat<-bind_rows(dat01,dat02,dat03)
dat %>% 
  mutate(group=fct_rev(group)) -> dat

作图代码

代码语言:javascript
复制
ggplot(data = dat,aes(x=x,y=mean_value))+
  geom_line(aes(color=group),size=1.5)+
  geom_ribbon(aes(ymin=lower95-1,ymax=upper95+1,
                  fill=group),
              alpha=0.5,
              show.legend = FALSE)+
  theme_minimal()+
  theme(panel.grid = element_blank(),
        axis.line = element_line(),
        axis.ticks = element_line(),
        legend.title = element_blank())+
  scale_x_continuous(breaks = 2:8,
                     labels = paste0("N",8:2))+
  scale_y_continuous(breaks = c(0,25))+
  labs(x="Subtree node",y="Percent subtree")

最终结果

image.png

这里最终的结果和论文中的图还是有些差异的,因为我没有看到论文中用的是置信区间作为数据范围还是其他,我这里选择的是置信区间,然后对数值进行了加减1

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

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

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

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

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 论文
  • 数据下载链接
  • 首先是做数据整理的代码
  • 作图代码
  • 最终结果
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档