0

Data and libraries used for this question:

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')

I'm trying to combine two geom_tile plots using the cowplot library. The two individual plots are created using:

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"))

and

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"))

I then use cowplot to first make a title with:

title <- ggdraw()+
  draw_label("Heatmaps over the average fold change of the clusters", fontface='bold')

and then to combine the title and the two plots with:

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))

I reduce a lot of whitespace between the two heatmaps after moving them closer together. But it creates a lot of whitespaces on the left and right margin which I don't manage to remove. That is, when I save the picture with:

ggsave('ex1.pdf', scale = 2)

Any suggestions?

Baraliuh
  • 593
  • 3
  • 12

1 Answers1

1

By adding coord_equal(), you're setting a fixed-aspect-ratio coordinate system, which means you need to make the aspect ratio of the final output file match the aspect ratios of your underlying plots. By not specifying a width and a height in your ggsave() line you're essentially guaranteed to not get the right output. Also, if you find yourself setting large negative margins you know for sure you're doing something wrong.

Without knowing exactly what the intended output is, this seems reasonable to me:

p <- plot_grid(figWT, figMT, labels=c('A', 'B'))
plot_grid(title, p, nrow=2, rel_heights=c(0.1, 1))
ggsave("ex1.png", width = 8, height = 4.5, dpi = 300)

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104