在ggplot heatmap中添加群集的图例

时间:2017-07-30 10:11:28

标签: r ggplot2 heatmap

我有ggplot热图,其中行由kmeans(15个集群)聚集。

MotifPresence.kmeans = kmeans(MotifPresence, 15)
class.promoter = MotifPresence.kmeans$cluster
MotifPresence.kmeans.ordered = MotifPresence[order(class.promoter), MPbindist.hclust$order]
x=colnames(MotifPresence)[MPbindist.hclust$order]
y=rownames(MotifPresence)
data <- expand.grid(X=x, Y=y)

data$Z <- as.vector(t((MotifPresence.kmeans.ordered)))

ggplot(data, aes(X, Y, z= Z)) + geom_tile(aes(fill = Z)) + 
theme(axis.text.x = element_text(angle = 45, hjust = 1), 
    axis.text.y = element_blank(), axis.ticks.y=element_blank())+
scale_fill_gradient(name = "Score", low = "white", high = "darkgreen") +
ylab("") +xlab("") 

如何在此热图中添加一些标记以显示此群集之间的界限?可能是水平线或每个簇有不同颜色的附加列?

heatmap with clusters

1 个答案:

答案 0 :(得分:0)

找到答案:

class.size = MotifPresence.kmeans$size
levels = cumsum(class.size)+0.5
my.lines<-data.frame(x=c(.5), y=c(levels), 
                 xend=c(17.5), yend=c(levels))
ggplot(data, aes(X, Y, z= Z)) + geom_tile(aes(fill = Z)) + 
theme(axis.text.x = element_text(angle = 45, hjust = 1), 
    axis.text.y = element_blank(), axis.ticks.y=element_blank())+
scale_fill_gradient(name = "Score", low = "white", high = "darkgreen") +
ylab("") +xlab("") + 
geom_segment(data=my.lines, aes(x,y,xend=xend, yend=yend), size=0.1, inherit.aes=F)

enter image description here