- 跟着Nature Methods学画图:R语言ggplot2+g
- 跟着Nature Communications 学画图~ggpl
- 跟着Nature Methods学画图:R语言画热图(pheat
- 跟着Nature Communications 学画图~ggpl
- R语言基础绘图函数散点图~跟着Nature Communicat
- 跟着Nature Communications 学画图~ggpl
- 跟着Nature Communications学画图~Figur
- 跟着Nature Methods学画图:R语言ggplot2画小
- 跟着Nature Methods学画图:R语言ggplot2散点
- 跟着Nature Methods学画图:R语言ggplot2画气
今天继续昨天推文的内容,今天的内容介绍如何在气泡图和左侧和上方添加聚类树图,今天的内容主要参考
https://mp.weixin.qq.com/s/XVl2MoOsT7pB1wNJmltoVw
这篇论文是在简书 土豆学生信 分享的内容看到的。简书的链接是 https://www.jianshu.com/p/bbf9cb13b41a
论文是
![](https://img.haomeiwen.com/i6857799/0623d9325418f379.png)
论文对应的代码是公开的 https://github.com/ajwilk/2020_Wilk_COVID
![](https://img.haomeiwen.com/i6857799/1c06722a4334d394.png)
今天重复的内容是论文中的figure2f
![](https://img.haomeiwen.com/i6857799/d7a44218c9c7d7b1.png)
按照论文提供的代码得到了画图用到的数据,部分数据如下
![](https://img.haomeiwen.com/i6857799/5bd36088f1832b1b.png)
但是用他提供的画图代码没有能够画出图来。因为他用到了一个
dot_plot()
函数,没有找到这个函数是怎么来的。既然已经拿到了数据,就用ggplot2自己来画吧
读入数据做气泡图,
data.final<-read.csv("NM/figure2f.csv",header=T,check.names=F)
head(data.final)
library(ggplot2)
ggplot(data.final,aes(x=features.plot,y=id))+
geom_point(aes(size=`Percent expressed`,
color=`Average expression`))+
theme_bw()+
theme(panel.grid = element_blank(),
axis.text.x=element_text(angle=90,hjust = 1,vjust=0.5))+
scale_color_gradient(low="lightgrey",high="blue")+
labs(x=NULL,y=NULL)+
guides(size=guide_legend(order=3))
![](https://img.haomeiwen.com/i6857799/31a5e7f401c667b0.png)
以y轴为变量,做层次聚类,并使用ggtree展示层次聚类结果
聚类用到的是平均表达量那一列
df<-data.final[,c(1,2,4)]
首先是长格式数据转换为宽格式
df1<-reshape2::dcast(df,id~features.plot,value.var = "Average expression")
rownames(df1)<-df1$id
df1.1<-df1[,2:22]
![](https://img.haomeiwen.com/i6857799/a7e92fc12f9725d3.png)
层次聚类,ggtree展示结果
df1.1.clust<-hclust(dist(df1.1))
df2.1.clust<-hclust(dist(df2.1))
library(ggtree)
p2<-ggtree(df1.1.clust)
p2+
geom_tiplab()+
xlim(NA,7)
![](https://img.haomeiwen.com/i6857799/2d8fb42b87fa5621.png)
使用aplot包拼图
library(ggplot2)
p1<-ggplot(data.final,aes(x=features.plot,y=id))+
geom_point(aes(size=`Percent expressed`,
color=`Average expression`))+
theme_bw()+
theme(panel.grid = element_blank(),
axis.text.x=element_text(angle=90,hjust = 1,vjust=0.5))+
scale_color_gradient(low="lightgrey",high="blue")+
labs(x=NULL,y=NULL)+
guides(size=guide_legend(order=3))
library(aplot)
p1%>%
insert_left(p2,width = 0.2)
![](https://img.haomeiwen.com/i6857799/0c6b4070e779e3c3.png)
接下来就是在上方叠加聚类树,一样的操作
df2<-reshape2::dcast(df,features.plot~id,value.var = "Average expression")
rownames(df2)<-df2$features.plot
df2.1<-df2[,2:15]
df2.1.clust<-hclust(dist(df2.1))
p3<-ggtree(df2.1.clust)+
#geom_tiplab(angle=90)+
#theme_tree2()+
layout_dendrogram()
p3
p1%>%
insert_left(p2,width = 0.2)%>%
insert_top(p3,height = 0.2)
这里多了一个知识点是ggtree作图默认开口树的方向是向右,如果需要把开口改成向下,需要加上layout_dendrogram()
函数
最终的结果如下
![](https://img.haomeiwen.com/i6857799/4b08e4245815cc03.png)
这里和论文中的图有些不一致,可能是聚类算法的原因;ggtree有一个默认的从上到下排序,比如左侧的树现在第一个是H6,第二个是H5,如果想把H5放到第一个也是可以实现的,可以参考之前的推文 R语言ggtree按照指定的节点旋转树
欢迎大家关注我的公众号
小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
网友评论