8

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

Community
  • 1
  • 1
David LeBauer
  • 31,011
  • 31
  • 115
  • 189

2 Answers2

7

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

Ramnath
  • 54,439
  • 16
  • 125
  • 152
  • 1
    thanks. I tried this before; it makes a plot, but the plot is in the default `theme_grey()` rather than with the `theme_blank()` options that are in `newtheme` – David LeBauer Mar 28 '11 at 06:20
  • @DavidLeBauer Thank you a lot! It got changed somehow and I can restore it like that `theme_set(theme_grey())` – Anton K Feb 08 '16 at 08:00
2

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")
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • Hey, Baptiste! The new align.r appears to be behind a password wall. Any advice? – Atticus29 Jun 19 '12 at 16:56
  • this package is deprecated; I'd suggest you use [facetting instead](https://github.com/hadley/ggplot2/wiki/Align-two-plots-on-a-page) – baptiste Jun 19 '12 at 19:39