The cowplot package is a simple add-on to ggplot. It provides variousfeatures that help with creating publication-quality figures, such as aset of themes, functions to align plots and arrange them into complexcompound figures, and functions that make it easy to annotate plots andor mix plots with images. The package was originally written forinternal use in my lab, to provide my students and postdocs with thetools to make high-quality figures for their publications. I have alsoused the package extensively in my bookFundamentals of DataVisualization. This introductory vignette provides a brief glance atthe key features of the package.
For more complete documentation, readallvignettes and/or thereferencedocumentation.
When I first wrote the cowplot package, its primary purpose was toprovide a simple and clean theme, similar to ggplot2’stheme_classic(). Therefore, I wrote the package toautomatically load the theme. However, as the package grew infunctionality, this choice was increasingly problematic, and thereforeas of version 1.0 cowplot does not alter the default ggplot2 themeanymore.
Importantly, the cowplot package now provides a set of complementarythemes with different features. I now believe that there isn’t onesingle theme that works for all figures, and therefore I recommend thatyou always explicitly set a theme for every plot you make.
library(ggplot2)library(cowplot)# default ggplot2 themeggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point()# classic cowplot themeggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point() + theme_cowplot(12)# minimal grid themeggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + geom_point() + theme_minimal_grid(12)# minimal horizontal grid themeggplot(iris, aes(Sepal.Length, fill = Species)) + geom_density(alpha = 0.5) + scale_y_continuous(expand = expansion(mult = c(0, 0.05))) + theme_minimal_hgrid(12)However, if you have existing code that depends on the old cowplotbehavior, you can calltheme_set(theme_cowplot()) at thetop of your script to change the theme globally.
The cowplot package provides the functionplot_grid() toarrange plots into a grid and label them.
p1 <- ggplot(mtcars, aes(disp, mpg)) + geom_point()p2 <- ggplot(mtcars, aes(qsec, mpg)) + geom_point()plot_grid(p1, p2, labels = c('A', 'B'), label_size = 12)Whileplot_grid() was originally written for ggplot2, italso supports other plotting frameworks, such as base graphics.
p3 <- ~plot(mtcars$qsec, mtcars$mpg)plot_grid(p1, p3, labels = c('A', 'B'), label_size = 12)Theplot_grid() is built on top of a generic drawinglayer that allows us to capture plots as images and then draw with themor on top of them.
p <- ggplot(mtcars, aes(disp, mpg)) + geom_point(size = 1.5, color = "blue") + theme_cowplot(12)logo_file <- system.file("extdata", "logo.png", package = "cowplot")ggdraw(p) + draw_image(logo_file, x = 1, y = 1, hjust = 1, vjust = 1, width = 0.13, height = 0.2)Here,ggdraw() takes a snapshot of the plot and placesit at full size into a new drawing canvas. The functiondraw_image() then draws an image on top of the plot.
To create a watermark, we can reverse the order by first setting upan empty drawing canvas, then drawing the image, and then drawing theplot on top.
ggdraw() + draw_image(logo_file, scale = 0.5) + draw_plot(p)