其他
多个热图,多种配色,还要分开画图例?
I've been having great success with ggtree now and have recommended it to several colleages here in Oxford.
Is there any way to seperate the legend in heatmaps? Eg I have plotted a tree in a circular style with outer rings given extra data - I would like a seperate key for each ring - is this possible?
这是一个用户问题,画多个热图很容易,不断调用gheatmap
函数就是了。
下面是一个简单的例子:
library("ggplot2")
library("ggtree")
nwk <- system.file("extdata", "sample.nwk", package="treeio")
tree <- read.tree(nwk)
circ <- ggtree(tree, layout = "circular")
df <- data.frame(first=c("a", "b", "a", "c", "d", "d", "a", "b", "e", "e", "f", "c", "f"),
second= c("z", "z", "z", "z", "y", "y", "y", "y", "x", "x", "x", "a", "a"))
rownames(df) <- tree$tip.label
p1 <- gheatmap(circ, df[, "first", drop=F], offset=.8, width=.1,
colnames_angle=90, colnames_offset_y = .25)
p2 <- gheatmap(p1, df[, "second", drop=F], offset=5, width=.1,
colnames_angle=90, colnames_offset_y = .25)
要为不同的热图应用不同的配色方案,也是比较容易的,用scale_fill_manual
手工指定就行了。但是不同的热图的图例是混在一起的。
require(RColorBrewer)
col <- c(brewer.pal(5, "Dark2"), brewer.pal(4, "Pastel1"))
names(col) = c(letters[1:6], letters[24:26])
pp <- p2 + scale_fill_manual(values=col)
print(pp)
ggplot2
可以做到,就是比较麻烦而已,需要我们去覆盖aes
,这里我将介绍一个简单的方法,大家更容易理解。就是我们每一个热图单独画,然后把图例给抽取出来,最后用cowplot
来拼图。
p1x <- p1 + scale_fill_manual(values=col)
p2x <- gheatmap(circ, df[, "second", drop=F], offset=5, width=.1) +
scale_fill_manual(values=col)
require(cowplot)
leg1 <- get_legend(p1x)
leg2 <- get_legend(p2x)
pp <- pp + theme(legend.position="none")
plot_grid(pp, leg1, leg2, ncol=3, rel_widths=c(1, .1, .1))
往期精彩