Align 2 heatmap with cowplot

I 'd like to concat 2 plot area as like following image.


Using plot_grid(), I could not align plot areas like following though I passed arguments align='h'.

How can I align 2 plots in this situation? Any comments will be appreciated!!

library(dplyr)
library(ggplot2)
library(tidyr)
library(cowplot)
library(tibble)

## Dataset.
mtcars %>%
    select(1: 7) %>%
    cor() %>%
    round(., 1)-> m

label <- colnames(m)

## heatmap
m %>%
    as.data.frame() %>%
    tibble() %>%
    mutate(id=label) %>%
    gather(key=y, value=value, -id) %>%
    rename(x=id) %>%
    ggplot(aes(x=ordered(x,levels=label), y=ordered(y,levels=rev(label)), fill=value)) +
    geom_tile() +
    geom_text(aes(label=value), size=4) +
    scale_fill_viridis_c() +
    coord_fixed() -> g


## another value
data.frame(value=apply(m*100, 1, mean)) %>%
    rownames_to_column() %>%
    rename(y=rowname) %>%
    mutate(another_value=round(value, 1)) %>%
    ggplot(aes(x=1,  y=ordered(y,levels=rev(label)), fill=another_value)) +
    geom_tile() + 
    geom_text(aes(label=another_value), size=4) +
    theme(axis.title.x=element_blank(),
          axis.text.x=element_blank()) + 
 scale_fill_distiller(palette = 'Spectral') -> gg


## concat 
plot_grid(g, gg, align='h')


specify also an aspect ratio for the second plot and use patchwork
library(dplyr)
library(ggplot2)
library(tidyr)
library(cowplot)
library(tibble)
library(patchwork)

Dataset.

mtcars %>%
select(1: 7) %>%
cor() %>%
round(., 1)-> m

label <- colnames(m)

heatmap

m %>%
as.data.frame() %>%
tibble() %>%
mutate(id=label) %>%
gather(key=y, value=value, -id) %>%
rename(x=id) %>%
ggplot(aes(x=ordered(x,levels=label), y=ordered(y,levels=rev(label)), fill=value)) +
geom_tile() +
geom_text(aes(label=value), size=4) +
scale_fill_viridis_c() +
coord_fixed() -> g

another value

data.frame(value=apply(m*100, 1, mean)) %>%
rownames_to_column() %>%
rename(y=rowname) %>%
mutate(another_value=round(value, 1)) %>%
ggplot(aes(x=1, y=ordered(y,levels=rev(label)), fill=another_value)) +
geom_tile() +
geom_text(aes(label=another_value), size=4) +
theme(axis.title.x=element_blank(),
axis.text.x=element_blank()) +
scale_fill_distiller(palette = 'Spectral')+
theme(aspect.ratio = 8)-> gg

g + gg

How incredible solution! Thanks a lot, you made my day!

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.