zl程序教程

您现在的位置是:首页 >  其他

当前栏目

跟着Nature学作图:R语言ggplot2分组折线图完整示例(Extended Fig2)

语言 示例 完整 分组 Nature ggplot2 跟着 作图
2023-06-13 09:16:32 时间

论文

A saturated map of common genetic variants associated with human height

https://www.nature.com/articles/s41586-022-05275-y

s41586-022-05275-y.pdf

代码没有公开,但是作图数据基本都公开了,争取把每个图都重复一遍

今天的推文重复论文中的extended Figure2 分组折线图

image.png

示例数据集

image.png

作图代码

library(readxl)

dat01<-read_excel("extendFig2.xlsx",
                  skip = 1)
dat01

colnames(dat01)

library(tidyverse)
library(ggplot2)
library(ggh4x)

dat01 %>% 
  pivot_longer(-`Distance to closest EUR COJO SNP (kb)`) %>% 
  mutate(name=factor(name,
                     levels = c("AFR","EAS","SAS","HIS"))) %>% 
  ggplot(aes(x=`Distance to closest EUR COJO SNP (kb)`,
             y=value))+
  geom_line(aes(color=name))+
  theme_classic()+
  scale_y_continuous(limits = c(0,1),
                     breaks = seq(0,1,0.2))+
  guides(x=guide_axis_truncated(trunc_lower = 0,
                                trunc_upper = 200),
         y=guide_axis_truncated(trunc_lower = 0,
                                trunc_upper = 1))+
  labs(y="Proportion of non-EUR COJO SNPs")+
  scale_color_manual(name="Median distance",
                     labels=c("AFR"="AFR: 9.2 kb",
                              "EAS"="EAS: 10.3 kb",
                              "SAS"="SAS: 9.7 kb",
                              "HIS"="HIS: 9.7kb"),
                     values=c("AFR"="#288aec",
                              "EAS"="#ef806b",
                              "SAS"="#f091f1",
                              "HIS"="#daad50"))+
  geom_segment(data=data.frame(y=seq(0,1,0.1),
                               yend=seq(0,1,0.1)),
               aes(x=-Inf,xend=Inf,
                   y=y,
                   yend=yend),
               lty="dashed",
               color="gray")+
  geom_vline(xintercept = 100,color="gray")+
  theme(legend.position = c(0.8,0.4))