我在尝试使用两个树形图来调整热图时遇到了麻烦。现在它看起来像这样(我使用虚线只是为了显示情节'边界):
我的目标是在plot.margin
中不使用任何魔术常量来获得这样的东西(这个特定的图像是使用它们创建的),所以我可以使用一个脚本来获取数百个数据集的树形图热图,而不是只有一个:
(注意顶部的树形图和热图之间的自由空间以及热图的标签和图的下边缘)。
我认为主要问题是我不知道如何正确计算热图的宽度和高度以及轴的空间。文本。
完全可重复的示例代码:
library(ggplot2)
library(ggdendro)
library(reshape2)
library(gridExtra)
n_samples <- 20
n_modules <- 11
my_data <- dget("http://pastebin.com/raw/bKSXY5kP")
base_size <- 9
longest_sample_name <- max(nchar(my_data$names))
heatmap_height <- round((10 / 27.1) * n_samples, digits=3)
heatmap_width <- round((10 / 27.1) * n_modules, digits=3)
title_width <- round((10 / 27.1) * 0.25 * longest_sample_name, digits=3)
heatmap <-
ggplot(my_data, aes(Module, Sample)) +
geom_tile(aes(fill = value), colour = "black", size=0.5) +
scale_fill_gradientn(colours=c("blue", "blue", "white", "red", "red"), space="Lab") +
theme_grey(base_size=base_size, base_family = "mono") +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
coord_fixed() +
theme(
panel.border = element_rect(linetype = "dashed", colour = "black", fill=NA),
plot.margin=unit(c(0, 0, 0, 0), "in"),
legend.position ="none",
axis.title = element_blank(),
axis.text.y = element_text(size=base_size * 1.2, colour="black"),
axis.text.x = element_text(size=base_size * 1.2, colour="black"))
x <- matrix(my_data$value, nrow = n_samples, ncol = n_modules + 1)
dd.col <- as.dendrogram(hclust(dist(t(x))))
dd.row <- as.dendrogram(hclust(dist(x)))
ddata_x <- dendro_data(dd.row)
ddata_y <- dendro_data(dd.col)
theme_dendro <- theme(
panel.border = element_rect(linetype = "dashed", colour = "black", fill=NA),
plot.margin=unit(c(0, 0, 0, 0), "in"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank())
dendro_horizontal <-
ggplot(segment(ddata_x)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
theme_dendro +
coord_flip()
dendro_vertical <-
ggplot(segment(ddata_y)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
theme_dendro
layout_matrix <- rbind(c(NA, 1, NA),
c(2, 2, 3))
widths <- c(title_width, heatmap_width, heatmap_width * 2 / 3)
heights <- c(heatmap_height / 2, heatmap_height)
grobs <- arrangeGrob(grobs = list(dendro_vertical, heatmap, dendro_horizontal),
layout_matrix = layout_matrix,
widths = widths,
heights = heights)
ggsave("heatmap_with_dendro.png",
plot = grobs,
width = sum(widths),
height = sum(heights),
units = "in")
这是我计算grobs&#39;大小(&#39;魔术&#39;数字是从以前的编码员继承的,没有敢触摸它们):
longest_sample_name <- max(nchar(my_data$names))
heatmap_height <- round((10 / 27.1) * n_samples, digits=3)
heatmap_width <- round((10 / 27.1) * n_modules, digits=3)
title_width <- round((10 / 27.1) * 0.25 * longest_sample_name, digits=3)
我如何创建热图:
heatmap <-
ggplot(my_data, aes(Module, Sample)) +
geom_tile(aes(fill = value), colour = "black", size=0.5) +
scale_fill_gradientn(colours=c("blue", "blue", "white", "red", "red"), space="Lab") +
theme_grey(base_size=base_size, base_family = "mono") +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
coord_fixed() +
theme(
panel.border = element_rect(linetype = "dashed", colour = "black", fill=NA),
plot.margin=unit(c(0, 0, 0, 0), "in"),
legend.position ="none",
axis.title = element_blank(),
axis.text.y = element_text(size=base_size * 1.2, colour="black"),
axis.text.x = element_text(size=base_size * 1.2, colour="black"))
和树状图:
x <- matrix(my_data$value, nrow = n_samples, ncol = n_modules + 1)
dd.col <- as.dendrogram(hclust(dist(t(x))))
dd.row <- as.dendrogram(hclust(dist(x)))
ddata_x <- dendro_data(dd.row)
ddata_y <- dendro_data(dd.col)
theme_dendro <- theme(
panel.border = element_rect(linetype = "dashed", colour = "black", fill=NA),
plot.margin=unit(c(0, 0, 0, 0), "in"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
panel.background = element_blank(),
axis.title = element_blank(),
axis.text = element_blank(),
axis.ticks = element_blank(),
axis.line = element_blank())
dendro_horizontal <-
ggplot(segment(ddata_x)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
theme_dendro +
coord_flip()
dendro_vertical <-
ggplot(segment(ddata_y)) +
geom_segment(aes(x=x, y=y, xend=xend, yend=yend)) +
scale_x_discrete(expand=c(0,0)) +
scale_y_discrete(expand=c(0,0)) +
theme_dendro
我如何使用gridExtra
安排它们:
layout_matrix <- rbind(c(NA, 1, NA),
c(2, 2, 3))
widths <- c(title_width, heatmap_width, heatmap_width * 2 / 3)
heights <- c(heatmap_height / 2, heatmap_height)
grobs <- arrangeGrob(grobs = list(dendro_vertical, heatmap, dendro_horizontal),
layout_matrix = layout_matrix,
widths = widths,
heights = heights)
最终保存图片:
ggsave("heatmap_with_dendro.png",
plot = grobs,
width = sum(widths),
height = sum(heights),
units = "in")