用于此问题的数据和库:
library(tidyverse)
library(reshape2)
library(cowplot)
data("diamonds")
temp1_m <- temp2_m <- melt(diamonds[1:3])
temp1_m[3] <- temp2_m[3] <- NULL
colnames(temp1_m) <- colnames(temp2_m) <- c('Var1', 'Var2', 'value')
我正在尝试使用该库合并两个geom_tile
图。cowplot
使用以下方法创建两个单独的图:
figMT <- ggplot(temp2_m, aes(Var1, Var2))+
geom_tile(aes(fill = value), colour = 'white')+
scale_fill_gradient(low = 'green', high = 'red', name = 'log fold change',
guide = guide_legend(title.vjust = 1, reverse = T))+
ggtitle('Mutant clusters')+
scale_x_discrete(expand = c(0, 0))+
scale_y_discrete(limits = rev(levels(temp2_m$Var2)))+
xlab('')+
ylab('')+
coord_equal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
axis.text = element_text(size=12),plot.margin=grid::unit(c(0,0,0,0), "mm"))
和
figWT <- ggplot(temp1_m, aes(Var1, Var2))+
geom_tile(aes(fill = value), colour = 'white')+
scale_fill_gradient(low = 'green', high = 'red', name = 'log fold change',
guide = guide_legend(title.vjust = 1, reverse = T))+
ggtitle('WT clusters')+
scale_x_discrete(expand = c(0, 0))+
scale_y_discrete(limits = rev(levels(temp1_m$Var2)))+
xlab('')+
ylab('Cluster')+
coord_equal()+
theme(axis.text.x = element_text(angle = 45, hjust = 1),
axis.text = element_text(size=12),plot.margin=grid::unit(c(0,0,0,0), "mm"))
然后我先用cowplot
以下方式制作标题:
title <- ggdraw()+
draw_label("Heatmaps over the average fold change of the clusters", fontface='bold')
然后将标题和两个情节结合起来:
p <- plot_grid(figWT+ theme(plot.margin = unit(c(0, -10, 0, 0), "cm")),
figMT+ theme(plot.margin = unit(c(0, 0, 0, -5.1), "cm")),
labels=c('A', 'B'), hjust = c(-24, -.5))
plot_grid(title, p, nrow=2, rel_heights=c(0.1, 1))
在将它们靠得更近后,我减少了两个热图之间的大量空白。但它在左右边距上创建了很多我无法删除的空格。也就是说,当我保存图片时:
ggsave('ex1.pdf', scale = 2)
有什么建议么?