I have a matrix of experimental results from multiple parameters. Each parameter has been tested across different values and the number of values tested is different for each parameter. That is, not an even grid to plot.
What I am aiming for and almost achieved is a grid of graphs, with each row being a parameter and each column a value for that parameter. Each cell of the grid contains a bar plot of two results for that parameter-value combination. I have built each row as a single graph using facet_graph
in ggplot2
and then combined them into one figure with patchwork
.
library(dplyr)
library(readxl) # reads single sheets from .xlsx file
library(grid)
library(gridExtra)
library(ggplot2)
library(ggthemes) # extra ggplot themes
library(patchwork) # multiple plots with spacers in one pane
# sample matrix
mtr <- data.frame(
"parameter" = c(rep("M",12), rep("a",10), rep("mu",10), rep("i",8), rep("N",6), rep("mut",4), rep("rpl",4)),
"value" = c(rep(c(1,"N","N/10","N/20","N/4","N/5"),2),
rep(c(0,0.05,0.1,0.15,0.2),2),
rep(c(0.01,0.03,0.05,0.07,0.1),2),
rep(c(0.1,0.2,0.3,0.5),2),
rep(c(100,200,50),2),
rep(c("pc","pl"),2),
rep(c("F", "W"),2)
),
"ep" = c(c(rep("ESS",6), rep("ES",6)),
c(rep("ESS",5), rep("ES",5)),
c(rep("ESS",5), rep("ES",5)),
c(rep("ESS",4), rep("ES",4)),
c(rep("ESS",3), rep("ES",3)),
c(rep("ESS",2), rep("ES",2)),
c(rep("ESS",2), rep("ES",2))
),
"permill" = runif(27, 0, 0.45),
"SD" = runif(27, 0, 0.016)
)
mtr$value = as.factor(mtr$value)
# graphical parameters
colcode <- c("ESS" = "white", "ES" = "red")
set_theme <- theme_bw()
# individual ggplot graphs
a.tr <- filter(mtr,parameter == "a")
agg.tr <- ggplot(a.tr,aes(x = ep, y = permill, fill = ep)) +
geom_col() +
scale_fill_manual("brewer", values = colcode) +
geom_errorbar(aes(ymin = permill - SD, ymax = permill + SD), width = .15, size = 0.70) +
coord_cartesian(ylim = c(0,0.450)) +
facet_wrap( ~ value, nrow = 1) +
labs(x = "", y = "", tag = "a") +
set_theme +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), legend.position = "none")
N.tr <- filter(mtr, parameter == "N")
Ngg.tr <- ggplot(N.tr,aes(x = ep, y = permill, fill = ep)) +
geom_col() +
scale_fill_manual("brewer", values = colcode) +
geom_errorbar(aes(ymin = permill - SD, ymax = permill + SD), width = .15, size = 0.70) +
coord_cartesian(ylim = c(0,0.450)) +
facet_wrap( ~ value, nrow = 1) +
set_theme +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), legend.position = "none") +
labs(x = "", y = "", tag = "N")
M.tr <- filter(mtr, parameter == "M")
Mgg.tr <- ggplot(M.tr,aes(x = ep, y = permill, fill = ep)) +
geom_col() +
scale_fill_manual("brewer", values = colcode) +
geom_errorbar(aes(ymin = permill - SD, ymax = permill + SD), width = .15, size = 0.70) +
coord_cartesian(ylim = c(0,0.450)) +
facet_wrap( ~ value, nrow = 1) +
set_theme +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), legend.position = "none") +
labs(x = "", y = "", tag = "M")
i.tr <- filter(mtr, parameter == "i")
igg.tr <- ggplot(i.tr,aes(x = ep, y = permill, fill = ep)) +
geom_col() +
scale_fill_manual("brewer", values = colcode) +
geom_errorbar(aes(ymin = permill - SD, ymax = permill + SD), width = .15, size = 0.70) +
coord_cartesian(ylim = c(0,0.450)) +
facet_wrap( ~ value, nrow = 1) +
set_theme +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), legend.position = "none") +
labs(x = "", y = "", tag = "i")
mu.tr <- filter(mtr, parameter == "mu")
mugg.tr <- ggplot(mu.tr,aes(x = ep, y = permill, fill = ep)) +
geom_col() +
scale_fill_manual("brewer", values = colcode) +
geom_errorbar(aes(ymin = permill - SD, ymax = permill + SD), width = .15, size = 0.70) +
coord_cartesian(ylim = c(0,0.450)) +
facet_wrap( ~ value, nrow = 1) +
set_theme +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), legend.position = "none") +
labs(x = "", y = "", tag = "mu")
mut.tr <- filter(mtr, parameter == "mut")
mutgg.tr <- ggplot(mut.tr,aes(x = ep, y = permill, fill = ep)) +
geom_col() +
scale_fill_manual("brewer", values = colcode) +
geom_errorbar(aes(ymin = permill - SD, ymax = permill + SD), width = .15, size = 0.70) +
coord_cartesian(ylim = c(0,0.450)) +
facet_wrap( ~ value, nrow = 1) +
set_theme +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), legend.position = "none") +
labs(x = "", y = "", tag = "mut")
rpl.tr <- filter(mtr, parameter == "rpl")
rplgg.tr <- ggplot(rpl.tr,aes(x = ep, y = permill, fill = ep)) +
geom_col() +
scale_fill_manual("brewer", values = colcode) +
geom_errorbar(aes(ymin = permill - SD, ymax = permill + SD), width = .15, size = 0.70) +
coord_cartesian(ylim = c(0,0.450)) +
facet_wrap( ~ value, nrow = 1) +
set_theme +
theme(axis.text.x=element_blank(),axis.ticks.x=element_blank(), legend.position = "none") +
labs(x = "", y = "", tag = "rpl")
# layout of graph positioning
layout <- "
AAAAAAAAAAAA
BBBBBBBBBB##
CCCCCCCCCC##
DDDDDDDD####
EEEEEE######
FFFF##GGGG##
"
# patchwork package allows to "add" the plots and positions them acocrding to the layout
Mgg.tr + agg.tr + mugg.tr + igg.tr + Ngg.tr + mutgg.tr + rplgg.tr + plot_layout(design = layout)
I have two problems:
- How to fix graph dimensions so that all cells in the grid (parameterX-valueZ combination) are exactly the same size. The x and y axis are identical for all graphs and I would like to make them comparable at a first glance.
- How to write one axis label for the whole graph?
For 2. I have tried
library(ggpubr) # annotates fig
cmp.tr <- Mgg.tr + agg.tr + mugg.tr + igg.tr + Ngg.tr + mutgg.tr + rplgg.tr + plot_layout(design = layout)
annotate_figure(cmp.tr, left = textGrob("proportion of simulations reaching solution type", rot = 90))
but the axis title is applied to the last graph of the grid only.