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

相关文章

SQL Server修改数据库名及物理数据文件名操作步骤

《SQLServer修改数据库名及物理数据文件名操作步骤》在SQLServer中重命名数据库是一个常见的操作,但需要确保用户具有足够的权限来执行此操作,:本文主要介绍SQLServer修改数据... 目录一、背景介绍二、操作步骤2.1 设置为单用户模式(断开连接)2.2 修改数据库名称2.3 查找逻辑文件名

Python实例题之pygame开发打飞机游戏实例代码

《Python实例题之pygame开发打飞机游戏实例代码》对于python的学习者,能够写出一个飞机大战的程序代码,是不是感觉到非常的开心,:本文主要介绍Python实例题之pygame开发打飞机... 目录题目pygame-aircraft-game使用 Pygame 开发的打飞机游戏脚本代码解释初始化部

canal实现mysql数据同步的详细过程

《canal实现mysql数据同步的详细过程》:本文主要介绍canal实现mysql数据同步的详细过程,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的... 目录1、canal下载2、mysql同步用户创建和授权3、canal admin安装和启动4、canal

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

使用SpringBoot整合Sharding Sphere实现数据脱敏的示例

《使用SpringBoot整合ShardingSphere实现数据脱敏的示例》ApacheShardingSphere数据脱敏模块,通过SQL拦截与改写实现敏感信息加密存储,解决手动处理繁琐及系统改... 目录痛点一:痛点二:脱敏配置Quick Start——Spring 显示配置:1.引入依赖2.创建脱敏

详解如何使用Python构建从数据到文档的自动化工作流

《详解如何使用Python构建从数据到文档的自动化工作流》这篇文章将通过真实工作场景拆解,为大家展示如何用Python构建自动化工作流,让工具代替人力完成这些数字苦力活,感兴趣的小伙伴可以跟随小编一起... 目录一、Excel处理:从数据搬运工到智能分析师二、PDF处理:文档工厂的智能生产线三、邮件自动化:

深入解析 Java Future 类及代码示例

《深入解析JavaFuture类及代码示例》JavaFuture是java.util.concurrent包中用于表示异步计算结果的核心接口,下面给大家介绍JavaFuture类及实例代码,感兴... 目录一、Future 类概述二、核心工作机制代码示例执行流程2. 状态机模型3. 核心方法解析行为总结:三

python获取cmd环境变量值的实现代码

《python获取cmd环境变量值的实现代码》:本文主要介绍在Python中获取命令行(cmd)环境变量的值,可以使用标准库中的os模块,需要的朋友可以参考下... 前言全局说明在执行py过程中,总要使用到系统环境变量一、说明1.1 环境:Windows 11 家庭版 24H2 26100.4061

Python数据分析与可视化的全面指南(从数据清洗到图表呈现)

《Python数据分析与可视化的全面指南(从数据清洗到图表呈现)》Python是数据分析与可视化领域中最受欢迎的编程语言之一,凭借其丰富的库和工具,Python能够帮助我们快速处理、分析数据并生成高质... 目录一、数据采集与初步探索二、数据清洗的七种武器1. 缺失值处理策略2. 异常值检测与修正3. 数据

pandas实现数据concat拼接的示例代码

《pandas实现数据concat拼接的示例代码》pandas.concat用于合并DataFrame或Series,本文主要介绍了pandas实现数据concat拼接的示例代码,具有一定的参考价值,... 目录语法示例:使用pandas.concat合并数据默认的concat:参数axis=0,join=