我如何更改ggplot2中的默认主题? [英] How can I change the default theme in ggplot2?
问题描述
背景
我想改变ggplot使用的主题,但我很难理解 theme_update()
文档和示例。
我使用 align_plots()
在密度图上放置箱形图,但没有找到任何代码导致了错误,并留下了最小的例子。我怀疑这个错误是由于使用 theme_blank()
造成的,但我不确定它为什么会发生,或者我可以如何修复它。
所以,在这里我提供了一个最小的可重现的错误示例:
library (ggExtra)
align.plots(qplot(1))
更新主题:
newtheme< - theme_update(axis.text.y = theme_blank(),
轴)。 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))
这给出了错误:
UseMethod(validGrob)中的错误:
没有适用于' validGrob'应用于类NULL的对象
另外:警告消息:
在editThisGrob(grob,specs)中:没有找到插槽'vp'
问题:
-
我在ggplot文档中得到了这一点,并且在 ggplot网站上找不到答案,尽管我得到的最接近的是 polishing.r 脚本
注意:
基于<一个href =https://stackoverflow.com/questions/3824412/ggplot2-use-options-for-multiple-plots/3824489#3824489>哈德利的建议另一个问题。
opt< - opts(...)
align.plots(qplot(1)+ opt)
其中...
是theme_update( )
以上解决方案它可能被认为是ggExtra :: align.plots()中的一个bug。此函数计算ggplot的不同元素的大小,如y轴标签和图例,并相应地对齐图表以使绘图面板位于彼此之上。如果您将主题设置为对某些图形元素使用theme_blank(),则该函数会变得混淆,因为基础grob(ggplot2 :::。zeroGrob)与其他grobs不太一样。
虽然它可能是可修复的(*),但我认为考虑其他选项会更好:
- 彼此重叠;因为您删除了可能会抵消剧情位置的元素,所以应该没有问题。 修正,尝试
使用虚拟刻面变量使ggplot2自动对齐两个面板使用gridExtra :: grid.arrange()或普通的网格视口来使用这两个面板来获得这两个面板
来源(http://ggextra.googlecode.com/svn/trunk/R/align.r )
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 oftheme_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:
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
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 oftheme_update()
above解决方案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")
这篇关于我如何更改ggplot2中的默认主题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!