跟着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

相关文章

SpringBoot分段处理List集合多线程批量插入数据方式

《SpringBoot分段处理List集合多线程批量插入数据方式》文章介绍如何处理大数据量List批量插入数据库的优化方案:通过拆分List并分配独立线程处理,结合Spring线程池与异步方法提升效率... 目录项目场景解决方案1.实体类2.Mapper3.spring容器注入线程池bejsan对象4.创建

PHP轻松处理千万行数据的方法详解

《PHP轻松处理千万行数据的方法详解》说到处理大数据集,PHP通常不是第一个想到的语言,但如果你曾经需要处理数百万行数据而不让服务器崩溃或内存耗尽,你就会知道PHP用对了工具有多强大,下面小编就... 目录问题的本质php 中的数据流处理:为什么必不可少生成器:内存高效的迭代方式流量控制:避免系统过载一次性

C#实现千万数据秒级导入的代码

《C#实现千万数据秒级导入的代码》在实际开发中excel导入很常见,现代社会中很容易遇到大数据处理业务,所以本文我就给大家分享一下千万数据秒级导入怎么实现,文中有详细的代码示例供大家参考,需要的朋友可... 目录前言一、数据存储二、处理逻辑优化前代码处理逻辑优化后的代码总结前言在实际开发中excel导入很

SpringBoot+RustFS 实现文件切片极速上传的实例代码

《SpringBoot+RustFS实现文件切片极速上传的实例代码》本文介绍利用SpringBoot和RustFS构建高性能文件切片上传系统,实现大文件秒传、断点续传和分片上传等功能,具有一定的参考... 目录一、为什么选择 RustFS + SpringBoot?二、环境准备与部署2.1 安装 RustF

Python实现Excel批量样式修改器(附完整代码)

《Python实现Excel批量样式修改器(附完整代码)》这篇文章主要为大家详细介绍了如何使用Python实现一个Excel批量样式修改器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录前言功能特性核心功能界面特性系统要求安装说明使用指南基本操作流程高级功能技术实现核心技术栈关键函

MyBatis-plus处理存储json数据过程

《MyBatis-plus处理存储json数据过程》文章介绍MyBatis-Plus3.4.21处理对象与集合的差异:对象可用内置Handler配合autoResultMap,集合需自定义处理器继承F... 目录1、如果是对象2、如果需要转换的是List集合总结对象和集合分两种情况处理,目前我用的MP的版本

GSON框架下将百度天气JSON数据转JavaBean

《GSON框架下将百度天气JSON数据转JavaBean》这篇文章主要为大家详细介绍了如何在GSON框架下实现将百度天气JSON数据转JavaBean,文中的示例代码讲解详细,感兴趣的小伙伴可以了解下... 目录前言一、百度天气jsON1、请求参数2、返回参数3、属性映射二、GSON属性映射实战1、类对象映

Redis实现高效内存管理的示例代码

《Redis实现高效内存管理的示例代码》Redis内存管理是其核心功能之一,为了高效地利用内存,Redis采用了多种技术和策略,如优化的数据结构、内存分配策略、内存回收、数据压缩等,下面就来详细的介绍... 目录1. 内存分配策略jemalloc 的使用2. 数据压缩和编码ziplist示例代码3. 优化的

C# LiteDB处理时间序列数据的高性能解决方案

《C#LiteDB处理时间序列数据的高性能解决方案》LiteDB作为.NET生态下的轻量级嵌入式NoSQL数据库,一直是时间序列处理的优选方案,本文将为大家大家简单介绍一下LiteDB处理时间序列数... 目录为什么选择LiteDB处理时间序列数据第一章:LiteDB时间序列数据模型设计1.1 核心设计原则

Python 基于http.server模块实现简单http服务的代码举例

《Python基于http.server模块实现简单http服务的代码举例》Pythonhttp.server模块通过继承BaseHTTPRequestHandler处理HTTP请求,使用Threa... 目录测试环境代码实现相关介绍模块简介类及相关函数简介参考链接测试环境win11专业版python