开发者

How can I change the default theme in ggplot2?

开发者 https://www.devze.com 2023-02-19 23:13 出处:网络
Background I would like to change the theme that ggplot uses, but I am having difficulty understanding the theme_update() documentation and examples.

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开发者_StackOverflow中文版 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


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")
0

精彩评论

暂无评论...
验证码 换一张
取 消