Quick example on how to combine two individual ggplot2 graphs. This allows more flexibility compared to facetting over different variables. Facetting over multiple variables with different scales (e.g. concentration for one parameter, temperature for the other) has the problem that the axis-labels can not be formatted independently. Facetting is supposed to show multiple facets of the same metric. Using it for parameters that are measured on completely different scales (like rainfall and temperature) was not the reason why facetting was put into ggplot. It works fine for quick overview plots, but good luck getting axis-labels where they should be.
Combining individual graphs into one object allows much more fexibility, even though it is more work on first glance.
# assemble to graphs into one object for printing
# using ggplot2 and gridExtra
# this allows to combine completely independent graphs into one
# has full fexibility to format each plot independently
# open iris data
data(iris)
# load libraries
library(ggplot2)
library(gridExtra)
## Loading required package: grid
df <- iris
# create first plot
p <- ggplot(df, aes(x = Sepal.Length, y = Sepal.Width))
p <- p + geom_point(aes(colour = Species))
p <- p + theme_bw()
p
scatter_plot <- p
# create second plot
p <- ggplot(df, aes(x = Species, y = Petal.Width))
p <- p + geom_boxplot()
p <- p + theme_bw()
p
box_plot <- p
# arrange both plots
grid.arrange(scatter_plot, box_plot)
# remove x-axis elements from one of the plots
# so that they look as they share the same x-axis
box_plot <- box_plot + theme(
axis.text.x = element_blank(),
axis.title.x = element_blank(),
axis.ticks.x = element_blank()
)
box_plotA <- box_plot
box_plotA
# the second plot
p <- ggplot(df, aes(x = Species, y = Petal.Length))
p <- p + geom_boxplot()
p <- p + theme_bw()
p
box_plotB <- p
# box_plotB
# arrange both plots
grid.arrange(box_plotA, box_plotB)
# check ?grid.arrange for layout-options.
# Problem with the current graph:
# The left side of the plot areas are not aligned due to decimals on the y-axis
# solution: change display format of the decimals in one of the plots
box_plotC <- box_plotB + scale_y_continuous(labels = function(x) format(x, nsmall = 1))
grid.arrange(box_plotA, box_plotC)
# remove margins between the plots
# see plot.margin
# plot.margin margin around entire plot (unit with the sizes of the top, right, bottom, and left margins)
# units can be given in "lines" or something more specific like "cm"...
box_plotA <- box_plotA + theme(plot.margin = unit(c(1, 1, 0, 1), "lines"))
box_plotD <- box_plotB + theme(plot.margin = unit(c(0, 1, 1, 1), "lines"))
grid.arrange(box_plotA, box_plotD)
# Alternative, as suggested by NH-N:
# no worries about decimals and margins.
grid.draw(rbind(ggplotGrob(box_plotA), ggplotGrob(box_plotB),
size="first"))
# to export the plot
# old fashioned device-specific way
pdf(file = "Combined_plots.pdf", width = 7, height = 9)
print(grid.arrange(box_plotA, box_plotB)) # or print(grid.draw(rbind(...
dev.off() # don't forget the essential dev.off!!