I am trying to plot three different plots together to form a larger figure. The problem is that plot.margin(c(0,0,0,0), "cm") doesn't remove the whitespace as expected. The figures are close together as expected if I remove the coord_fixed.
Example dataset, code to generate dataset and code used producing the example.
Code for dataset.
library(tidyverse)
library(cowplot)
n_pat <- 25
patient <- 1:n_pat
censoring <- ceil(rexp(n_pat, 1/30))
tumour_shrink <- (rbeta(n_pat, 2, 2) - 0.5) * 100
n_params <- 15
parameters <- paste("Parameter", 1:n_params)
response <- sample(c("PR", "NE", "CR", "PD", "SD"), size=n_pat,
replace = T)
missing_combination <- sample(c(TRUE, FALSE), size=n_pat, replace=T)
changes <- matrix(runif(n_pat * n_params, 1, 100), nrow=n_pat, ncol=n_cytokines)
changes[sample(1:dim(changes)[1], 4, replace = FALSE), sample(1:dim(changes)[2], 5, replace = F)] <- NA
df <- data.frame(patient, censoring, tumour_shrink, changes, missing_combination)
colnames(df) <- c("patient", "censoring", "tumour_shrink", cytokines, "missing_combination")
Code for plots:
p1 <- df %>%
mutate(color = case_when(
response == "PR" ~ "lightgreen",
response == "NE" ~ "white",
response == "CR" ~ "darkgreen",
response == "PD" ~ "red",
response == "SD" ~ "yellow"
)) %>%
arrange(tumour_shrink) %>%
mutate(patient = factor(patient, levels=patient)) %>%
ggplot(aes(x=patient, y=1, fill=color)) +
geom_raster() +
coord_fixed() +
geom_tile(color="black", size=1) +
geom_text(aes(label=response), size=3) +
theme(axis.text = element_blank(),
axis.ticks = element_blank(),
legend.position = "none",
axis.ticks.length = unit(0, "mm"),
axis.title.y = element_text(angle = 0, vjust=0.57, size = 12),
plot.margin = unit(c(0, 0, 0, 0), "cm")) +
scale_fill_identity() +
labs(y="Best ov. resp", x=NULL)
p2 <- df %>%
arrange(tumour_shrink) %>%
mutate(patient = factor(patient, levels=patient)) %>%
mutate(color = ifelse(missing_combination, "white", "gray")) %>%
ggplot(aes(x=patient, y=1, fill=color)) +
geom_raster() +
geom_tile(color="black", size=1) +
geom_text(aes(label=censoring), size=3) +
scale_fill_identity() +
labs(y="Censoring", x=NULL) +
coord_fixed() +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.ticks.length = unit(0, "mm"),
legend.position = "none",
axis.title.y = element_text(angle = 0, vjust=0.57, size=12),
plot.margin = unit(c(0, 0, 0, 0), "pt"))
p3 <- df %>%
arrange(tumour_shrink) %>%
mutate(patient = factor(patient, levels=patient)) %>%
ggplot(aes(x=factor(patient), y=1, fill=tumour_shrink)) +
geom_raster(alpha=0.8) +
geom_tile(color="black", size=1) +
coord_fixed() +
geom_text(aes(label=formatC(tumour_shrink, 0, format="f")),
size=3) +
theme(axis.text = element_blank(),
axis.title = element_blank(),
axis.ticks = element_blank(),
axis.ticks.length = unit(0, "mm"),
axis.title.y = element_text(angle = 0, vjust=0.57, size=12),
plot.margin = unit(c(0, 0, 0, 0), "cm"),
legend.position = "none") +
scale_fill_gradient(low="green", high="red") +
labs(y="Tumour shrink", x=NULL)
plot_grid(p1, p2, p3, ncol=1, align="v", axis="lr")
A simple solution is provided by the patchwork
package:
library(patchwork)
wrap_plots(p1, p2, p3, ncol=1)