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
What causes this error?
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
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
3.1 Default theme. 3.2 Black and white theme. 3.3 Light theme. 3.4 Dark theme.
The default RStudio theme is 'Modern' the default editor theme is TextMate.
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().
The theme() allows a precise control of graphical parameters such as axis text, ticks and labels, or legend texts and labels, etc.
I don't know why this works, but it does. Just insert the line theme_set(newtheme)
before you call align.plots.
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")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With