跟着PNAS学作图 | 提供全文数据和代码

2023-10-29 05:59

本文主要是介绍跟着PNAS学作图 | 提供全文数据和代码,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

论文

题目:Death rates at specific life stages mold the sex gap in life expectancy

网址: https://www.pnas.org/doi/full/10.1073/pnas.2010588118

代码网址

https://github.com/CPop-SDU/sex-gap-e0-pnas

该文章发表于2021年,论文中图形对我们一部分同学仍具参考价值。作者提供的全套的代码和数据,可以直接使用。此外,作者的数据和代码写的非常的规整。但是,需要看懂和运行代码,还是需要有一定的基础。

论文主图

论文主图仅有两张,如下图所示。
Figure 1
Figure 2

代码

Figure 1


# function to localize pathsdevtools::source_gist("32e9aa2a971c6d2682ea8d6af5eb5cde")# prepare session
source(lp("0-prepare-session.R"))# theme -------------------------------------------------------------------
load("../dat/palettes.rda" %>% lp)theme_custom <- theme_minimal(base_family = font_rc) +theme(legend.position = "bottom",strip.background = element_blank(),strip.text = element_blank(),panel.grid.minor =  element_blank(),panel.grid.major =  element_line(size = .25),panel.ontop = T)

作者将相关的代码编写在其他的R脚本中,使用时直接进行调用。

# Fig 1 -- RELATIVE ----------------------------------
load("../dat/a6gap33cntrs.rda" %>% lp)# relative
df6 %>% filter(country %>% is_in(c("SWE", "USA", "JPN", "RUS"))) %>%mutate(name = name %>% fct_recode(USA = "United States") %>% fct_rev()) %>%ggplot() +geom_col(aes(year, ctb_rel %>% multiply_by(100), fill = age_group),position = position_stack(reverse = TRUE),color = NA,width = 1) +facet_grid(name ~ ., scales = "free_y", space = "free") +coord_cartesian(ylim = c(-10, 120), expand = FALSE)+scale_x_continuous(breaks = seq(1800, 2000, 50))+scale_y_continuous(breaks = seq(0, 100, 25), position = "right")+scale_fill_manual(values = pal_six, guide  = guide_legend(ncol = 1, reverse = TRUE)) +theme_minimal(base_family = font_rc, base_size = 20) +theme(legend.position = c(.6, .5),strip.background = element_blank(),strip.text = element_blank(),panel.grid.minor =  element_blank(),panel.grid.major =  element_line(size = .1),panel.spacing = unit(0, "lines"),panel.ontop = T)+labs(x = NULL,y = "Contribution, %",fill = "Age group")+# label countriesgeom_text(data = . %>% select(name, row, column) %>%  distinct(),aes(label = name, color = name), x = 2015, y = 120, hjust = 1, vjust = 1, size = 9, fontface = 2,family = font_rc)+scale_color_manual(values = pal_four %>% rev, guide = FALSE)one_outer <- last_plot()
one_outer# plot ratio
load("../dat/df4qx.rda" %>% lp)df4qx %>%pivot_wider(names_from = sex, values_from = qx) %>% ggplot(aes(age, y = m/f, color = country))+geom_hline(yintercept = 1, color = "gray25",  size = .5)+geom_smooth(se = F, size = 1, color = "#ffffff", span = .25)+geom_smooth(se = F, size = .5, span = .25)+scale_x_continuous(breaks = c(0, 15, 40, 60, 80))+scale_y_continuous(trans = "log", breaks = c(.5, 1, 2, 3), labels = c("", 1, 2, 3),limits = c(.75, 3.5))+scale_color_manual(NULL, values = pal_four)+theme_minimal(base_family = font_rc, base_size = 16)+theme(legend.position = "none",panel.grid.minor = element_blank())+labs(y = "Sex ratio, log scale",x = "Age")+annotate("text", x = 50, y = .9, label = "Most recent year",size = 8.5, color = "grey50", alpha = .5,vjust = 1, family = font_rc, fontface = 2)one_a <- last_plot()
one_a
# Death risk Ratio, Sweden, years 1750, 1800, 1850, 1900, 1960, 2019
# plot qx
load("../dat/qxdiff.rda" %>% lp)qxdiff %>% filter(country == "SWE", year %>% is_in(c(1800, 1900, 1960, 2019 ))) %>% ggplot(aes(age, y = ratio, color = year %>% factor))+geom_hline(yintercept = 1, color = "gray25",  size = .5)+geom_smooth(se = F, size = .75, span = .4)+scale_x_continuous(breaks = c(0, 15, 40, 60, 80))+scale_y_continuous(trans = "log", breaks = c(.5, 1, 2, 3), labels = c("", 1, 2, 3),limits = c(.75, 3.5))+scale_color_viridis_d(end = .97)+theme_minimal(base_family = font_rc, base_size = 16)+theme(legend.position = c(.85, .75),legend.spacing.x = unit(.1, "line"),legend.key.height = unit(1, "line"),panel.grid.minor = element_blank())+labs(color = "Year",y = "Sex ratio, log scale",x = "Age")+annotate("text", x = 50, y = .9, label = "Sweden",size = 8.5, color = "#009C9C", vjust = 1, family = font_rc, fontface = 2)one_b <- last_plot()
one_b# plot difference
df4qx %>%pivot_wider(names_from = sex, values_from = qx) %>% ggplot(aes(x = age, y = m - f, color = country, group = country)) +geom_path(size = .5)+scale_color_manual(NULL, values = pal_four)+scale_x_continuous(breaks = c(0, 15, 40, 60, 80))+scale_y_continuous(trans = "log",breaks = c(.0001, .001, .01, .05),labels = c(.0001, .001, .01, .05) %>% paste %>% str_replace("0.", "."),limits = c(9e-6, .1))+theme_minimal(base_family = font_rc, base_size = 16)+theme(legend.position = c(.77, .25),legend.spacing.x = unit(.1, "line"),legend.key.height = unit(1, "line"),legend.text = element_text(size = 16),panel.grid.minor = element_blank())+labs(y = "Sex gap, log scale",x = "Age")one_c <- last_plot()
one_c# arrange and save
blank <- ggplot(tibble(x = 1, y = 1), aes(x, y))+geom_rect(xmin = -Inf, xmax = Inf,ymin = -Inf, ymax = Inf,fill = "#ffffff",color = NA)+theme_void()library(cowplot)
one <- ggdraw() +draw_plot(one_outer) +# white space for plotsdraw_plot(blank, x = 0, y = .75, width = 0.7, height = 0.25)+draw_plot(blank, x = 0, y = .55, width = 0.33, height = 0.42)+draw_plot(blank, x = 0, y = .33, width = 0.33, height = 0.67)+# inset plotsdraw_plot(one_a, x = 0, y = .66, width = .33, height = .33)+draw_plot(one_c, x = .34, y = .66, width = .33, height = .33)+draw_plot(one_b, x = 0, y = 0.35, width = .33, height = .33)+# annotate plot lettersdraw_text(LETTERS[c(1,3,2,4)],  x = c(.01, .35, .01, .01),y = c(.99, .99, .66, .3), hjust = 0,  vjust = 1, size = 20, family = font_rc, fontface = 2)ggsave(filename = "out/main-one.png" %>% lp, plot = one, width = 10, height = 10, type = "cairo-png"
)

**这样一连串的的就绘制出图1。但是,有多少同学可以知道作者绘制每个图形的数据类型是什么样呢?
**

如果大家有时间时间和精力可以可以试一下,如果不行,那么在本文的中点赞或留言,我们一起分开绘制每个图形,一起学习!!!!

附图

附图1

附图3

附图3

附图8


ENDING!!


往期文章:
1. 最全WGCNA教程(替换数据即可出全部结果与图形)

WGCNA分析 | 全流程分析代码 | 代码一

WGCNA分析 | 全流程分析代码 | 代码二

WGCNA分析 | 全流程代码分享 | 代码三


2. 精美图形绘制教程

精美图形绘制教程

小杜的生信筆記,主要发表或收录生物信息学的教程,以及基于R的分析和可视化(包括数据分析,图形绘制等);分享感兴趣的文献和学习资料!!

这篇关于跟着PNAS学作图 | 提供全文数据和代码的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/298698

相关文章

使用Python开发一个Ditto剪贴板数据导出工具

《使用Python开发一个Ditto剪贴板数据导出工具》在日常工作中,我们经常需要处理大量的剪贴板数据,下面将介绍如何使用Python的wxPython库开发一个图形化工具,实现从Ditto数据库中读... 目录前言运行结果项目需求分析技术选型核心功能实现1. Ditto数据库结构分析2. 数据库自动定位3

python使用Akshare与Streamlit实现股票估值分析教程(图文代码)

《python使用Akshare与Streamlit实现股票估值分析教程(图文代码)》入职测试中的一道题,要求:从Akshare下载某一个股票近十年的财务报表包括,资产负债表,利润表,现金流量表,保存... 目录一、前言二、核心知识点梳理1、Akshare数据获取2、Pandas数据处理3、Matplotl

pandas数据的合并concat()和merge()方式

《pandas数据的合并concat()和merge()方式》Pandas中concat沿轴合并数据框(行或列),merge基于键连接(内/外/左/右),concat用于纵向或横向拼接,merge用于... 目录concat() 轴向连接合并(1) join='outer',axis=0(2)join='o

Django开发时如何避免频繁发送短信验证码(python图文代码)

《Django开发时如何避免频繁发送短信验证码(python图文代码)》Django开发时,为防止频繁发送验证码,后端需用Redis限制请求频率,结合管道技术提升效率,通过生产者消费者模式解耦业务逻辑... 目录避免频繁发送 验证码1. www.chinasem.cn避免频繁发送 验证码逻辑分析2. 避免频繁

批量导入txt数据到的redis过程

《批量导入txt数据到的redis过程》用户通过将Redis命令逐行写入txt文件,利用管道模式运行客户端,成功执行批量删除以Product*匹配的Key操作,提高了数据清理效率... 目录批量导入txt数据到Redisjs把redis命令按一条 一行写到txt中管道命令运行redis客户端成功了批量删除k

精选20个好玩又实用的的Python实战项目(有图文代码)

《精选20个好玩又实用的的Python实战项目(有图文代码)》文章介绍了20个实用Python项目,涵盖游戏开发、工具应用、图像处理、机器学习等,使用Tkinter、PIL、OpenCV、Kivy等库... 目录① 猜字游戏② 闹钟③ 骰子模拟器④ 二维码⑤ 语言检测⑥ 加密和解密⑦ URL缩短⑧ 音乐播放

Python使用Tenacity一行代码实现自动重试详解

《Python使用Tenacity一行代码实现自动重试详解》tenacity是一个专为Python设计的通用重试库,它的核心理念就是用简单、清晰的方式,为任何可能失败的操作添加重试能力,下面我们就来看... 目录一切始于一个简单的 API 调用Tenacity 入门:一行代码实现优雅重试精细控制:让重试按我

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

C#监听txt文档获取新数据方式

《C#监听txt文档获取新数据方式》文章介绍通过监听txt文件获取最新数据,并实现开机自启动、禁用窗口关闭按钮、阻止Ctrl+C中断及防止程序退出等功能,代码整合于主函数中,供参考学习... 目录前言一、监听txt文档增加数据二、其他功能1. 设置开机自启动2. 禁止控制台窗口关闭按钮3. 阻止Ctrl +