Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I change the default theme in ggplot2?

Tags:

r

ggplot2

Background

I would like to change the theme that ggplot uses, but I am having difficulty understanding the theme_update() documentation and examples.

I am using align_plots() to place a boxplot over a density plot, but I found none of that code was causing the error, and was left with the minimal example. I suspect that the error is caused by the use of theme_blank(), but I am not sure why it occurs or how I can fix it.

So, here I provide a minimal reproducible example of an error that I am getting:

library(ggExtra)
align.plots(qplot(1))

But it breaks after I update the theme:

newtheme <- theme_update(axis.text.y = theme_blank(),
                         axis.line = theme_blank(),
                         axis.title.x = theme_blank(), 
                         axis.title.y = theme_blank(),
                         axis.ticks.x = theme_blank(),
                         panel.grid.major = theme_blank(),
                         panel.grid.minor = theme_blank(),
                         panel.border = theme_blank(),
                         axis.color.y = 'white')
align.plots(qplot(1))

This gives the error:

Error in UseMethod("validGrob") : 
  no applicable method for 'validGrob' applied to an object of class "NULL"
In addition: Warning message:
In editThisGrob(grob, specs) : Slot 'vp' not found

Questions:

  1. What causes this error?

  2. Where can I get more information on using ?theme_update()? I got this far on the ggplot documentation, and can't find the answer at the ggplot website, although the closest that I got was the polishing.r script

note:

I get the same error with a solution based on Hadley's suggestion for another question.

opt <- opts(...)
align.plots(qplot(1) + opt)

where ... is the contents of theme_update() above

like image 880
David LeBauer Avatar asked Mar 28 '11 01:03

David LeBauer


People also ask

What is the default Ggplot theme?

3.1 Default theme. 3.2 Black and white theme. 3.3 Light theme. 3.4 Dark theme.

What is the default theme in R?

The default RStudio theme is 'Modern' the default editor theme is TextMate.

What are the themes in ggplot2?

Change the default ggplot theme by using the list of the standard themes available in ggplot2 R package. Our selection of best ggplot themes for professional publications or presentations, include: theme_classic(), theme_minimal() and theme_bw(). Another famous theme is the dark theme: theme_dark().

What does Theme () do in R?

The theme() allows a precise control of graphical parameters such as axis text, ticks and labels, or legend texts and labels, etc.


2 Answers

I don't know why this works, but it does. Just insert the line theme_set(newtheme) before you call align.plots.

like image 70
Ramnath Avatar answered Sep 30 '22 01:09

Ramnath


It could be considered a bug in ggExtra::align.plots(). This function computes the size of different elements of a ggplot, such as y-axis label and legend, and aligns the plots accordingly to have the plot panels on top of each other. If you set your theme to use theme_blank() for some of those graphical elements, the function gets confused as the underlying grob (ggplot2:::.zeroGrob) isn't quite like other grobs.

While it might be fixable (*), I think you'd be better off considering other options:

  • use a dummy facetting variable to have ggplot2 automatically align the two panels

  • use gridExtra::grid.arrange() or plain grid viewports to have the two plots on top of each other; since you removed the elements that may offset the plot positions, there should be no problem.

(*): now fixed, try

   source("http://ggextra.googlecode.com/svn/trunk/R/align.r")
like image 22
baptiste Avatar answered Sep 30 '22 02:09

baptiste