跟着Nature Communications学作图:R语言ggplot2绘制带有条纹的分组柱形图

论文

Pan-African genome demonstrates how population-specific genome graphs improve high-throughput sequencing data analysis

https://www.nature.com/articles/s41467-022-31724-3

本地pdf s41467-022-31724-3.pdf

论文中公布了大部分图的数据,但是没有公布对应的作图代码,没有关系,我们可以自己写代码试着模仿,今天的推文重复一下论文中的Figure 2A 带有条纹的分组柱形图

示例数据截图

image.png

这里实现条纹柱形图用到的是 ggpattern这个R包

参考链接

https://coolbutuseless.github.io/package/ggpattern/index.html

https://github.com/coolbutuseless/ggpattern

安装

remotes::install_github("coolbutuseless/ggpattern")

因为是ggplot2的扩展包,除了把作图函数替换,其余的细节都可以用ggplot2的语法来调节

读取数据

library(readxl)
dffig2a<-read_excel("data/20220806/41467_2022_31724_MOESM4_ESM.xlsx",
                    sheet = "figure 2a")
dffig2a

library(tidyverse)
dffig2a %>% 
  pivot_longer(-'Super-population') -> new.dffig2a

作图代码

library(ggplot2)

cols<-c("#ffa657","#fd8011","#6cbe6c","#349734",
        "#eba0d5","#da7dbd","#63a0cb","#1f7ab4",
        "#d0d166","#bbbe21")

ggplot(data = new.dffig2a,aes(x=`Super-population`,y=value))+
  geom_bar_pattern(stat="identity",
                   position = "dodge",
                   aes(pattern=name,
                       fill=name),
                   pattern_density=0.01,
                   fill=cols,
                   color="black",
                   show.legend = FALSE)+
  scale_pattern_manual(values = c('Divergence'='stripe',
                                  'Diversity'="none"))+
  scale_y_continuous(expand = expansion(mult = c(0,0.1)),
                     labels = scales::percent,
                     limits = c(0,0.25/100),
                     breaks = seq(0,0.25/100,by=0.05/100))+
  labs(x=NULL,y=NULL)+
  theme_classic()+
  theme(axis.line.y = element_blank(),
        axis.ticks.y = element_blank(),
        panel.grid = element_line(linetype = "dashed"),
        panel.grid.major = element_line(),
        panel.grid.minor = element_blank()) -> p1

p1

ggplot()+
  geom_rect_pattern(data=data.frame(x=1,xend=2,y=1,yend=2),
                    aes(xmin=x,ymin=y,xmax=xend,ymax=yend),
                    pattern_density=1,
                    fill="white",
                    color="black")+
  geom_rect_pattern(data=data.frame(x=1,xend=2,y=2.5,yend=3.5),
                    aes(xmin=x,ymin=y,xmax=xend,ymax=yend),
                    pattern="none",
                    pattern_density=1,
                    fill="grey",
                    color="black")+
  theme_void()+
  geom_text(data=data.frame(x=2,y=1.5),
            aes(x=x,y=y),label="Divergence",
            hjust=-0.1)+
  geom_text(data=data.frame(x=2,y=3),
            aes(x=x,y=y),label="Diversity",
            hjust=-0.1)+
  xlim(1,4) -> p2

p1+
  annotation_custom(grob = ggplotGrob(p2),
                    xmin = 4,xmax = Inf,
                    ymin = 0.2/100,ymax=0.25/100) -> p3
p3

library(patchwork)


p3+p3
image.png

示例数据可以在论文中去下载,代码直接在推文中复制,如果需要我整理好的数据和代码可以给推文打赏1元获取

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!

你可能感兴趣的:(跟着Nature Communications学作图:R语言ggplot2绘制带有条纹的分组柱形图)