How to change the default theme in ggplot2?

Background

I would like to change the theme used by ggplot, but it's hard for me to understand the documentation and examples of theme_update() .

I use align_plots() to place the rectangle above the density graph, but I did not find that none of these codes caused an error and was left with a minimal example. I suspect the error is caused by using theme_blank() , but I'm not sure why this is happening or how I can fix it.

So here I provide a minimal reproducible example of the error I get:

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

But it breaks after updating the topic:

 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 an 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:

  • What causes this error?

  • Where can I get more information about using ?theme_update() ? I got this far in the ggplot documentation and cannot find the answer on the ggplot website , although the closest I received was 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

+8
r ggplot2
source share
2 answers

This can be considered an error in ggExtra :: align.plots (). This function calculates the size of various ggplot elements, such as the y-axis label and legend, and aligns the graphs accordingly so that the story panels are on top of each other. If you use the theme_blank () theme for some of these graphic elements, the function is confused because the main coffin (ggplot2 :. ZeroGrob) is not quite like other craps.

Although this can be fixed (*), I think you would be better off looking at other options:

  • use the facetting dummy variable so that ggplot2 automatically aligns the two panels.

  • use gridExtra :: grid.arrange () or simple grid viewports to have two graphs on top of each other; since you have removed elements that can offset the position of the chart, there should be no problem.

(*): now fixed, try

  source("http://ggextra.googlecode.com/svn/trunk/R/align.r") 
+1
source share

I do not know why this works, but it is. Just insert the line theme_set(newtheme) before calling align.plots.

+7
source share

All Articles