跟着Nature Communications学作图:R语言ggplot2做堆积柱形图展示群体基因组学的结果

简介: 跟着Nature Communications学作图:R语言ggplot2做堆积柱形图展示群体基因组学的结果

论文

Genomic insights into local adaptation and future climate-induced vulnerability of a keystone forest tree in East Asia

https://www.nature.com/articles/s41467-022-34206-8#Sec23

完整的数据分析代码 涉及到群体基因组学

作图数据 ``

https://github.com/jingwanglab/Populus_genomic_prediction_climate_vulnerability

作者的github主页还有很多其他内容 https://github.com/jingwanglab

今天的图推文重复一下论文中的figure2a

论文中提供的代码是

https://github.com/jingwanglab/Populus_genomic_prediction_climate_vulnerability/blob/main/3-Population_genetics/1structure.sh

完整代码


Q2=read.table("pk230_ldpruned.2.Q.txt",header=F)
dim(Q2)
Q3=read.table("pk230_ldpruned.3.Q.txt",header=F)
dim(Q3)

myorder <- c("ZHY-03-1","ZHY-03-10",
             "ZHY-03-12","ZHY-03-17",
             "ZHY-03-2","ZHY-03-3",
             "ZHY-03-4","ZHY-03-6",
             "ZHY-03-7","ZHY-03-9",
             "ZHY-09-1","ZHY-09-11","ZHY-09-15",
             "ZHY-09-16","ZHY-09-17","ZHY-09-18",
             "ZHY-09-2","ZHY-09-6","ZHY-09-8","ZHY-10-1",
             "ZHY-10-11","ZHY-10-13","ZHY-10-14",
             "ZHY-10-15","ZHY-10-16","ZHY-10-3",
             "ZHY-10-4","ZHY-10-6","ZHY-10-9",
             "LiuJQ-MZL-2013-249-1","LiuJQ-MZL-2013-249-10",
             "LiuJQ-MZL-2013-249-3","LiuJQ-MZL-2013-249-4",
             "LiuJQ-MZL-2013-249-5","LiuJQ-MZL-2013-249-6",
             "LiuJQ-MZL-2013-249-7","LiuJQ-MZL-2013-249-8",
             "LiuJQ-MZL-2013-249-9","LiuJQ-MZL-2013-262-1",
             "LiuJQ-MZL-2013-262-10","LiuJQ-MZL-2013-262-11",
             "LiuJQ-MZL-2013-262-3","LiuJQ-MZL-2013-262-5",
             "LiuJQ-MZL-2013-262-6","LiuJQ-MZL-2013-262-7",
             "LiuJQ-MZL-2013-262-8","LiuJQ-MZL-2013-262-9",
             "LiuJQ-MZL-2013-283-1","LiuJQ-MZL-2013-283-10",
             "LiuJQ-MZL-2013-283-12","LiuJQ-MZL-2013-283-15","LiuJQ-MZL-2013-283-3","LiuJQ-MZL-2013-283-4","LiuJQ-MZL-2013-283-5","LiuJQ-MZL-2013-283-6","LiuJQ-MZL-2013-283-8","LiuJQ-MZL-2013-283-9","LiuJQ-MZL-2013-297-1","LiuJQ-MZL-2013-297-10","LiuJQ-MZL-2013-297-2","LiuJQ-MZL-2013-297-3","LiuJQ-MZL-2013-297-4","LiuJQ-MZL-2013-297-5","LiuJQ-MZL-2013-297-6","LiuJQ-MZL-2013-297-7","LiuJQ-MZL-2013-297-8","LiuJQ-MZL-2013-297-9","ZHY-14-1","ZHY-14-12","ZHY-14-13","ZHY-14-2","ZHY-14-3","ZHY-14-4","ZHY-14-5","ZHY-14-6","ZHY-14-7","ZHY-14-9","ZHY-16-1","ZHY-16-12","ZHY-16-13","ZHY-16-14","ZHY-16-15","ZHY-16-2","ZHY-16-3","ZHY-16-4","ZHY-16-6","ZHY-16-8","ZHY-17-1","ZHY-17-12","ZHY-17-13","ZHY-17-14","ZHY-17-15","ZHY-17-5","ZHY-17-6","ZHY-17-8","ZHY-17-9","ZHY-18-10","ZHY-18-13","ZHY-18-2","ZHY-18-3","ZHY-18-4","ZHY-18-5","ZHY-18-7","ZHY-18-8","ZHY-18-9","ZHY-19-10","ZHY-19-11","ZHY-19-12","ZHY-19-13","ZHY-19-14","ZHY-19-15","ZHY-19-5","ZHY-19-6","ZHY-19-8","ZHY-19-9","ZHY-21-1","ZHY-21-11","ZHY-21-12","ZHY-21-14","ZHY-21-2","ZHY-21-3","ZHY-21-4","ZHY-21-5","ZHY-21-7","ZHY-21-8","ZHY-22-1","ZHY-22-10","ZHY-22-11","ZHY-22-12","ZHY-22-3","ZHY-22-6","ZHY-22-7","ZHY-22-8","ZHY-22-9","LiuJQ-MZL-2013-323-0","LiuJQ-MZL-2013-323-10","LiuJQ-MZL-2013-323-11","LiuJQ-MZL-2013-323-12","LiuJQ-MZL-2013-323-13","LiuJQ-MZL-2013-323-4","LiuJQ-MZL-2013-323-5","LiuJQ-MZL-2013-323-6","LiuJQ-MZL-2013-323-7","LiuJQ-MZL-2013-323-9","ZHY-25-10","ZHY-25-11","ZHY-25-12","ZHY-25-13","ZHY-25-14","ZHY-25-3","ZHY-25-4","ZHY-25-7","ZHY-25-8","ZHY-25-9","ZHY-26-1","ZHY-26-10","ZHY-26-11","ZHY-26-12","ZHY-26-13","ZHY-26-15","ZHY-26-2","ZHY-26-3","ZHY-26-4","ZHY-26-8","ZHY-31-1","ZHY-31-10","ZHY-31-11","ZHY-31-12","ZHY-31-2","ZHY-31-3","ZHY-31-4","ZHY-31-7","ZHY-31-8","ZHY-33-1","ZHY-33-10","ZHY-33-11","ZHY-33-12","ZHY-33-3","ZHY-33-6","ZHY-33-7","ZHY-33-8","ZHY-33-9","ZHY-34-1","ZHY-34-11","ZHY-34-12","ZHY-34-13","ZHY-34-14","ZHY-34-2","ZHY-34-4","ZHY-34-5","ZHY-34-7","ZHY-34-9","ZHY-35-1","ZHY-35-10","ZHY-35-2","ZHY-35-3","ZHY-35-4","ZHY-35-5","ZHY-35-6","ZHY-35-7","ZHY-35-8","ZHY-35-9","ZHY-37-10","ZHY-37-11","ZHY-37-12","ZHY-37-15","ZHY-37-2","ZHY-37-3","ZHY-37-4","ZHY-37-6","ZHY-37-8","ZHY-37-9","ZHY-41-1","ZHY-41-10","ZHY-41-11","ZHY-41-12","ZHY-41-13","ZHY-41-2","ZHY-41-4","ZHY-41-6","ZHY-41-7","ZHY-41-9","ZHY-44-1","ZHY-44-10","ZHY-44-2","ZHY-44-3","ZHY-44-4","ZHY-44-5","ZHY-44-6","ZHY-44-9")
length(myorder)

library(tidyverse)
p1<-Q2 %>% 
  mutate(V1=factor(V1,
                   levels = myorder)) %>% 
  pivot_longer(-V1) %>% 
  mutate(name=factor(name,levels = c("V3","V2"))) %>% 
  ggplot(aes(x=V1,y=value,fill=name))+
  geom_bar(stat='identity',width=1,show.legend = FALSE)+
  scale_fill_manual(values = c("V3"="#e9e9e9",
                               "V2"="#e04d72"))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())+
  scale_y_continuous(minor_breaks=seq(0,1,0.1),
                     expand = c(0,0),
                     breaks=seq(0,1,0.25))+
  scale_x_discrete(breaks=NULL)+
  labs(x=NULL,y="k=2")


p2<-Q3 %>% 
  mutate(V1=factor(V1,
                   levels = myorder)) %>% 
  pivot_longer(-V1) %>% 
  #mutate(name=factor(name,levels = c("V3","V2"))) %>% 
  ggplot(aes(x=V1,y=value,fill=name))+
  geom_bar(stat='identity',width=1,show.legend = FALSE)+
  scale_fill_manual(values = c("V2"="#e9e9e9",
                               "V3"="#3280c3",
                               "V4"="#e04d72"))+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x = element_blank())+
  scale_y_continuous(minor_breaks=seq(0,1,0.1),
                     expand = c(0,0),
                     breaks=seq(0,1,0.25))+
  scale_x_discrete(breaks=NULL)+
  labs(x=NULL,y="k=3")

p3<-Q2 %>% 
  mutate(V1=factor(V1,
                   levels = myorder)) %>%
  ggplot()+
  geom_ribbon(aes(x=V1,ymin=0.1,ymax=1),fill="#e04d72")+
  #geom_ribbon(aes(x=164:230,ymin=0.1,ymax=1),fill="#3280c3")+
  theme_bw()+
  theme(panel.grid = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        panel.border = element_blank(),
        axis.title = element_blank())+
  scale_y_continuous(minor_breaks=seq(0,1,0.1),
                     expand = c(0,0),
                     breaks=seq(0,1,0.25))+
  #scale_x_continuous(breaks=NULL)+
  annotate(geom="text",x=80,y=0,label="South",vjust=-0.5)+
  annotate(geom="text",x=190,y=0,label="North",vjust=-0.5)+
  annotate(geom = "ribbon",x=1:165,ymin=0.5,ymax=1,fill="#e04d72")+
  annotate(geom = "ribbon",x=166:230,ymin=0.5,ymax=1,fill="#3280c3")

library(patchwork)

p1/p2/p3+
  plot_layout(heights = c(4,4,1))

最终结果

image.png

示例数据和代码可以给公众号推文点赞,点击在看,最后留言获取

欢迎大家关注我的公众号

小明的数据分析笔记本

小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
相关文章
|
3月前
|
数据可视化 数据挖掘 图形学
R语言基础可视化:使用ggplot2构建精美图形的探索
【8月更文挑战第29天】 `ggplot2`是R语言中一个非常强大的图形构建工具,它基于图形语法提供了一种灵活且直观的方式来创建各种统计图形。通过掌握`ggplot2`的基本用法和美化技巧,你可以轻松地将复杂的数据转化为直观易懂的图形,从而更好地理解和展示你的数据分析结果。希望本文能够为你探索`ggplot2`的世界提供一些帮助和启发。
|
3月前
|
数据可视化
R语言自定义图形:ggplot2中的主题与标签设置
【8月更文挑战第30天】`ggplot2`作为R语言中功能强大的绘图包,其自定义能力让数据可视化变得更加灵活和多样。通过合理使用`theme()`函数和`labs()`函数,以及`geom_text()`和`geom_label()`等几何对象,我们可以轻松创建出既美观又富有表达力的图形。希望本文的介绍能够帮助你更好地掌握`ggplot2`中的主题与标签设置技巧。
|
6月前
|
存储 数据可视化 数据挖掘
R语言可视化:ggplot2冲积/桑基图sankey分析大学录取情况、泰坦尼克幸存者数据
R语言可视化:ggplot2冲积/桑基图sankey分析大学录取情况、泰坦尼克幸存者数据
|
6月前
|
算法 数据可视化
R语言社区检测算法可视化网络图:ggplot2绘制igraph对象分析物种相对丰度
R语言社区检测算法可视化网络图:ggplot2绘制igraph对象分析物种相对丰度
|
6月前
r语言ggplot2误差棒图快速指南
r语言ggplot2误差棒图快速指南
|
2月前
|
数据采集 机器学习/深度学习 数据可视化
R语言从数据到决策:R语言在商业分析中的实践
【9月更文挑战第1天】R语言在商业分析中的应用广泛而深入,从数据收集、预处理、分析到预测模型构建和决策支持,R语言都提供了强大的工具和功能。通过学习和掌握R语言在商业分析中的实践应用,我们可以更好地利用数据驱动企业决策,提升企业的竞争力和盈利能力。未来,随着大数据和人工智能技术的不断发展,R语言在商业分析领域的应用将更加广泛和深入,为企业带来更多的机遇和挑战。
|
19天前
|
数据挖掘 C语言 C++
R语言是一种强大的统计分析工具,提供了丰富的函数和包用于时间序列分析。
【10月更文挑战第21天】时间序列分析是一种重要的数据分析方法,广泛应用于经济学、金融学、气象学、生态学等领域。R语言是一种强大的统计分析工具,提供了丰富的函数和包用于时间序列分析。本文将介绍使用R语言进行时间序列分析的基本概念、方法和实例,帮助读者掌握R语言在时间序列分析中的应用。
40 3
|
6月前
|
数据可视化 数据挖掘 API
【R语言实战】聚类分析及可视化
【R语言实战】聚类分析及可视化
|
6月前
|
机器学习/深度学习 数据可视化
R语言逻辑回归logistic模型ROC曲线可视化分析2例:麻醉剂用量影响、汽车购买行为2
R语言逻辑回归logistic模型ROC曲线可视化分析2例:麻醉剂用量影响、汽车购买行为
|
2月前
|
数据采集 数据可视化 数据挖掘
R语言在金融数据分析中的深度应用:探索数据背后的市场智慧
【9月更文挑战第1天】R语言在金融数据分析中展现出了强大的功能和广泛的应用前景。通过丰富的数据处理函数、强大的统计分析功能和优秀的可视化效果,R语言能够帮助金融机构深入挖掘数据价值,洞察市场动态。未来,随着金融数据的不断积累和技术的不断进步,R语言在金融数据分析中的应用将更加广泛和深入。

热门文章

最新文章