:)
我有一个问题,个人搜索找不到任何答案。我想制作一个带有分类变量的热图(有点像这样:类似于热图的图,但要分类的变量),我想在左侧添加一个系统进化树(像这样的树:如何创建一个树)。具有固定外部层次结构集群的热图)。理想的是改编第二个,因为它看起来更漂亮!;)
这是我的数据:
一个新的格式化的系统发育树,有3种,可以这样说:
((1,2),3);
数据框:
x<-c("species 1","species 2","species 3")
y<-c("A","A","C")
z<-c("A","B","A")
df<- data.frame(x,y,z)
(以A,B和C为分类变量,例如,在我的情况下,存在/不存在/重复的基因)。
你知道怎么做吗?
提前谢谢了!
编辑:我希望能够选择热图中每个类别的颜色,而不是经典的渐变。假设A =绿色,B =黄色,C =红色
我实际上是自己弄清楚的。对于那些感兴趣的人,这是我的脚本:
#load packages
library("ape")
library(gplots)
#retrieve tree in newick format with three species
mytree <- read.tree("sometreewith3species.tre")
mytree_brlen <- compute.brlen(mytree, method="Grafen") #so that branches have all same length
#turn the phylo tree to a dendrogram object
hc <- as.hclust(mytree_brlen) #Compulsory step as as.dendrogram doesn't have a method for phylo objects.
dend <- as.dendrogram(hc)
plot(dend, horiz=TRUE) #check dendrogram face
#create a matrix with values of each category for each species
a<-mytree_brlen$tip
b<-c("gene1","gene2")
list<-list(a,b)
values<-c(1,2,1,1,3,2) #some values for the categories (1=A, 2=B, 3=C)
mat <- matrix(values,nrow=3, dimnames=list) #Some random data to plot
#plot the hetmap
heatmap.2(mat, Rowv=dend, Colv=NA, dendrogram='row',col =
colorRampPalette(c("red","green","yellow"))(3),
sepwidth=c(0.01,0.02),sepcolor="black",colsep=1:ncol(mat),rowsep=1:nrow(mat),
key=FALSE,trace="none",
cexRow=2,cexCol=2,srtCol=45,
margins=c(10,10),
main="Gene presence, absence and duplication in three species")
#legend of heatmap
par(lend=2) # square line ends for the color legend
legend("topright", # location of the legend on the heatmap plot
legend = c("gene absence", "1 copy of the gene", "2 copies"), # category labels
col = c("red", "green", "yellow"), # color key
lty= 1, # line style
lwd = 15 # line width
)
这是结果图:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句