gridExtra : grid.arrange

library(reshape2)
library(ggplot2)
library(gridExtra)
library(grid)
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggplot2':
## 
##     ggsave
library(MASS)

insul = qplot(whiteside$Insul)
temp = qplot(whiteside$Temp, binwidth = 1)
grid.arrange(insul, temp)

set.seed(777)

v = sample(seq(1:100), size = 1000, replace = TRUE)
m = matrix(v, ncol = 4)
df = data.frame(m)
colnames(df) = c("Val", "X", "Y", "z")
df.melt = melt(df, id.vars = c("Val"))
 
histogram = qplot(v, geom = "histogram", binwidth = 1)
heatmap = qplot(x = Val, y = variable, data = df.melt, fill = value, geom = "tile")
lines = qplot(x = Val, y = Y, data = df, geom = "line")
scatterplot = qplot(Val, value, data = df.melt)
 
grid.arrange(histogram, heatmap, lines, scatterplot)

heatmap with barplots

set.seed(888)
x.values = seq(1:10)
y.values = LETTERS[seq(1:10)]
v = sample(seq(1:10), size = 100, replace = TRUE)
m = matrix(v, nrow = 10, ncol = 10)
df = data.frame(m)
colnames(df) = y.values
df$x.values = x.values
df
##     A  B  C  D E  F  G  H I  J x.values
## 1   1  5  7  2 5  5 10  1 6  3        1
## 2   4  1 10  2 2  3  1  3 9  7        2
## 3   1  1 10  9 4  5  2  8 6  1        3
## 4   7  5  8  1 6  8  2  9 1  1        4
## 5   8  8  9  5 3 10  6 10 2 10        5
## 6   2 10  6  3 9  9  6  9 2  3        6
## 7   4  8  7  1 8  6  9  3 1  6        7
## 8   3  4  2 10 4 10  3  2 6  5        8
## 9   1  9  4  4 5  7  5  9 4  7        9
## 10 10  8  9  4 1  6  5  3 3  4       10
df.melted = melt(df, id.vars = c("x.values"))
head(df.melted)
##   x.values variable value
## 1        1        A     1
## 2        2        A     4
## 3        3        A     1
## 4        4        A     7
## 5        5        A     8
## 6        6        A     2
hm <- ggplot(data = df.melted, aes(x = factor(x.values), y = variable, fill = value)) + geom_tile() + scale_x_discrete(breaks = unique(df.melted$x.values), labels = unique(df.melted$x.values))

hm

Extract heatmap legend
hm.clean1 <- hm + theme(axis.title.y = element_blank(), axis.text.y = element_blank(), axis.ticks.y = element_blank(), axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(), legend.position="none")
hm.clean1

hm.clean2 <- hm + theme(legend.position="none")
hm.clean2

Prepare y axis barplot
df.melted.y.avg <- aggregate(df.melted$value, by = list(df.melted$variable), FUN = mean)
colnames(df.melted.y.avg) <- c("YCategory", "ValueAvg")
head(df.melted.y.avg)
##   YCategory ValueAvg
## 1         A      4.1
## 2         B      5.9
## 3         C      7.2
## 4         D      4.1
## 5         E      4.7
## 6         F      6.9
bp.y <- ggplot(data = df.melted.y.avg, aes(x = YCategory, y = ValueAvg)) + 
    geom_bar(stat = "identity", aes(fill = ValueAvg)) + coord_flip() + theme_gray() +
    theme(axis.title.x = element_blank(), axis.text.x = element_blank(),
          axis.ticks.x = element_blank(), axis.text.y = element_text(size = 15), 
          axis.title.y = element_text(size = 20, margin = margin(0,10,0,0), angle = -90),
          legend.position="none") +
    scale_fill_distiller(name = "Value", palette = "Reds", direction = 1) + 
    labs(x = "Y Category")

bp.y

combine all plots
grid.arrange(hm, bp.y, nrow = 1, ncol = 2)

grid.arrange(hm.clean2, bp.y, nrow = 1, ncol = 2)

grid.arrange(hm.clean1, bp.y, nrow = 1, ncol = 2)

bp.y <- ggplot(data = df.melted.y.avg, aes(x = YCategory, y = ValueAvg)) + 
    geom_bar(stat = "identity", aes(fill = ValueAvg)) + coord_flip() + theme_gray() +
    theme(axis.title.x = element_blank(), axis.text.x = element_blank(),
          axis.ticks.x = element_blank(), axis.text.y = element_blank(), 
          axis.title.y = element_blank(),
          legend.position="none") +
    scale_fill_distiller(name = "Value", palette = "Reds", direction = 1) + 
    labs(x = "")


grid.arrange(hm.clean1, bp.y, nrow = 1, ncol = 2)

hm.clean3 <- hm + theme(axis.title.x = element_blank(), axis.text.x = element_blank(), axis.ticks.x = element_blank(), legend.position="none")

grid.arrange(hm.clean3, bp.y, nrow = 1, ncol = 2)