Disable cowplot default for ggplots

旧时模样 提交于 2019-12-21 07:34:44

问题


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"))

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?


回答1:


Just call theme_set(theme_gray()) beforehand:

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

Then, if you want to go crazy with themes, you could install ggthemes and simply replace theme_gray() with any theme you choose, or roll your own. Also see this vignette for styling individual elements.




回答2:


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"))

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



来源:https://stackoverflow.com/questions/33438265/disable-cowplot-default-for-ggplots

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!