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

藏宝库编辑 2024-9-21 20:57:02 79 0 来自 中国
论文

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中左下角的小图
论文中提供的原始数据集如下
需要将其整理成3个单独的数据集
3.png 首先是做数据整理的代码

library(readxl)library(ggplot2)library(tidyverse)dat01<-read_excel("data/20220518/dat01.xlsx")dat01dat01 %>%   mutate(x=8:2,         mean_value=`Duplication Per`,         lower95=`Duplication Per`,         upper95=`Duplication Per`,         group="Experimental") %>%   select(x,mean_value,lower95,upper95,group)-> dat01dat02<-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) -> dat02dat03<-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="ositive simulation") %>%   select(x,mean_value,lower95,upper95,group) -> dat03dat<-bind_rows(dat01,dat02,dat03)dat %>%   mutate(group=fct_rev(group)) -> dat作图代码

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="ercent subtree")最终结果

4.png 这里最终的结果和论文中的图还是有些差异的,因为我没有看到论文中用的是置信区间作为数据范围还是其他,我这里选择的是置信区间,然后对数值进行了加减1
示例数据可以到论文中去下载,代码可以在推文中复制
欢迎大家关注我的公众号
小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
您需要登录后才可以回帖 登录 | 立即注册

Powered by CangBaoKu v1.0 小黑屋藏宝库It社区( 冀ICP备14008649号 )

GMT+8, 2024-10-18 16:44, Processed in 0.149840 second(s), 35 queries.© 2003-2025 cbk Team.

快速回复 返回顶部 返回列表