16

I recently installed the cowplot package. However, after doing this I noticed that my ggplots are missing their background and grid lines of theme_grey()!

enter image description here

The code to create each of the above plots is:

result_df %>%
    ggplot(aes_string(x = 'p', y = 'r')) +
    # theme_grey() + # uncomment this line to produce plot on right
    geom_point(aes(group = c), size = 0.5) +
    geom_line(aes(group = c), size = 0.2, linetype = 'dotted') +
    theme(axis.text.x=element_text(angle = 90, hjust = 1, vjust = 0.5)) +
    facet_grid(b ~ e, scales = "free_y") +
    scale_x_continuous(breaks = seq(0, 10, 2))

Without explicitly calling + theme_grey(), I get the plot on the left.

What is happening here? I thought that theme_grey() is the default. How do I see what my default theme is?

here is a snippet of my sessionInfo():

R version 3.3.2 (2016-10-31)
Platform: x86_64-w64-mingw32/x64 (64-bit)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggthemes_3.3.0    cowplot_0.7.0     RPostgreSQL_0.4-1 DBI_0.5-1         knitr_1.15.1      dirmult_0.1.3-4   dplyr_0.5.0      
 [8] purrr_0.2.2       readr_1.0.0       tidyr_0.6.0       tibble_1.2        ggplot2_2.2.0     tidyverse_1.0.0  

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.8      magrittr_1.5     munsell_0.4.3    colorspace_1.3-1 R6_2.2.0         stringr_1.1.0    plyr_1.8.4       tools_3.3.2     
 [9] grid_3.3.2       gtable_0.2.0     lazyeval_0.2.0   assertthat_0.1   crayon_1.3.2     reshape2_1.4.2   rsconnect_0.6    testthat_1.0.2  
[17] labeling_0.3     stringi_1.1.2    scales_0.4.1    
Alex
  • 15,186
  • 15
  • 73
  • 127
  • 9
    cowplot thinks it's a good idea to change the default theme when attached – baptiste Dec 12 '16 at 07:53
  • thanks, very helpful. So, how do I see what the default theme is and change it back? :) – Alex Dec 12 '16 at 07:56
  • 1
    set_theme(theme_grey())? – Robin Gertenbach Dec 12 '16 at 08:00
  • 4
    Don't attach cowplot. – Sandy Muspratt Dec 12 '16 at 08:11
  • 1
    The problem is that detaching does not fix it either. – Alex Dec 12 '16 at 08:12
  • 4
    Start a new session. And when you need to use cowplot, use `cowplot::` – Sandy Muspratt Dec 12 '16 at 08:13
  • that works, thanks, but is a bit troublesome. – Alex Dec 12 '16 at 08:13
  • 3
    [See here](http://stackoverflow.com/questions/38281423/exact-positioning-of-multiple-plots-in-ggplot2-with-grid-arrange/38281884#38281884) – Sandy Muspratt Dec 12 '16 at 08:20
  • 5
    This behaviour is described in the [cowplot vignette](https://cran.r-project.org/web/packages/cowplot/vignettes/introduction.html). In the second set of code, the `theme` is changed only by loading `cowplot`. And a bit further down: "Note that if you ever want to use the default ggplot2 theme while using the cowplot package, simply add `theme_gray()` to your plot or call `theme_set(theme_gray())`" – Henrik Dec 12 '16 at 08:55
  • Related: [here](http://stackoverflow.com/questions/36409672/plots-in-ggplot2-appearing-without-grey-background) and [here](http://stackoverflow.com/questions/33438265/disable-cowplot-default-for-ggplots). – Henrik Dec 12 '16 at 09:00
  • Also covered here: http://stackoverflow.com/a/38281884/1457051 – hrbrmstr Dec 12 '16 at 10:24

1 Answers1

25

Note: this is longer an issue in current releases of cowplot, where the default theme is not changed. Original answer below:


You can use theme_get() to see the current "default" theme.

You can use theme_set() to change the "default" theme.

Theme settings do not carry over sessions.

Usually, your default will be theme_grey, but cowplot feels it's necessary to change that into theme_cowplot. I really wish it didn't.

You can either use :: notation to completely avoid this, or you can load the package as:

library(cowplot)
theme_set(theme_grey())
Axeman
  • 32,068
  • 8
  • 81
  • 94
  • 4
    I hear you, and I'm actually thinking about separating all the plot_grid and ggdraw code out into a separate package that wouldn't touch the theme. cowplot was originally about the theme, and it's evolved to do more and more things since. – Claus Wilke Nov 29 '17 at 21:00
  • It makes sense! Just not for us outsiders that use the package for other things. :) – Axeman Nov 29 '17 at 21:23
  • 3
    Actually, lately I'm running into more and more cases where I wish myself the theme hadn't changed. Like, I'm demonstrating something, but I have cowplot loaded, so the plot looks different than expected. Moving all the drawing code into a separate package is the right way to go. In this way, cowplot doesn't change behavior and people can simply start loading the other package when they don't want the theme. One day. – Claus Wilke Nov 29 '17 at 21:27
  • 1
    Cool. Hit me up if you need help with anything. Cheers for the awesome functions! – Axeman Nov 29 '17 at 21:30
  • Its curious that loading cowplot after storing ggplot objects with specific themes does override the theme in the stored objects! e.g. panel_border is overwritten when calling plot_grid. – nouse Jul 02 '18 at 11:31