Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable cowplot default for ggplots

The ggplot2 add-on package, cowplot, has a nice function for plotting multiple plots called plot_grid(). Here's plot_grid() in action:

library(ggplot2); library(cowplot)

plot_a <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
plot_b <- ggplot(mtcars, aes(mpg, disp)) + geom_point()

plot_grid(plot_a, plot_b, labels = c("A", "B"))

enter image description here

But note how loading cowplot changes the default style of plots. How can I load the cowplot package so that I can use the plot_grid() function but also disable the default plot styles that cowplot enforces?

like image 233
luciano Avatar asked Oct 30 '15 14:10

luciano


1 Answers

As mentioned in the comments, once you've installed the cowplot package you can load the plot_grid() function with the :: operator (see What are the double colons (::) in R?) and cowplot won't change any ggplot2 defaults.

> plot_a <- ggplot(mtcars, aes(mpg, wt)) + geom_point()
> plot_b <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
> plot_grid(plot_a, plot_b, labels = c("A", "B"))
Error in plot_grid(plot_a, plot_b, labels = c("A", "B")) : 
  could not find function "plot_grid"

> cowplot::plot_grid(plot_a, plot_b, labels = c("A", "B"))

enter image description here

The issue comes when you load the entire cowplot package with library() or require().

like image 134
Andrew Haynes Avatar answered Oct 05 '22 11:10

Andrew Haynes