gpt4 book ai didi

r - 如何更改 ggplot2 中的默认主题?

转载 作者:行者123 更新时间:2023-12-01 20:13:38 28 4
gpt4 key购买 nike

背景

我想更改 ggplot 使用的主题,但我很难理解 theme_update() 文档和示例。

我正在使用 align_plots() 在密度图上放置箱线图,但我发现这些代码都没有导致错误,并且只剩下最小的示例。我怀疑该错误是由使用 theme_blank() 引起的,但我不确定为什么会发生或如何修复它。

因此,在这里我提供了我遇到的错误的最小可重现示例:

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

但是在我更新主题后它就中断了:

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

这给出了错误:

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

问题:

  1. 什么原因导致此错误?

  2. 在哪里可以获得有关使用 ?theme_update() 的更多信息?我在 ggplot 文档中了解了这么多,但在 ggplot website 中找不到答案。 ,尽管我得到的最接近的是 polishing.r脚本

注意:

我使用基于 Hadley's suggestion 的解决方案遇到了相同的错误另一个问题。

opt <- opts(...)
align.plots(qplot(1) + opt)

其中...是上面theme_update()的内容

最佳答案

我不知道为什么会这样,但确实如此。只需在调用align.plots之前插入行theme_set(newtheme)即可。

关于r - 如何更改 ggplot2 中的默认主题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5453840/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com