ggplot2:使用多个绘图的选项

发布于 2024-09-25 07:04:48 字数 573 浏览 6 评论 0原文

我想创建 10 个具有不同数据但光学外观相同的图。例如,我想更改每个图的网格线的颜色。这可以通过添加

+ opts(panel.grid.major = theme_line(colour = "white")

到每个绘图定义来完成。然而,当我现在决定将背景颜色更改为“grey25”时,我必须单独修改每个图。这看起来工作量太大了。 ;)

所以,我考虑做一些类似的事情

opt1 <- '+ opts(panel.grid.major = theme_line(colour = "white")'

,然后定义每个图,

pl_x <- pl_x + opt1
pl_y <- pl_y + opt1
...

然后可以将其他选项(边距、字体、比例……)添加到 opt1 中。 但是,这不起作用(尝试打印 pl_x 时出现错误消息)。也许有人知道如何完成我想做的事情?

我还尝试了 theme_set 和 theme_update,但这导致我的绘图不再工作,除非我完全重新启动 R。

I would like to create 10 plots that have different data, but the same optical appearance. As an example, I’d like to change the colour of the gridline for each plot. This could be done by adding

+ opts(panel.grid.major = theme_line(colour = "white")

to each plot definition. However, when I now decide to change background colour to let’s say “grey25”, I’d have to modify each plot individually. This seems like way too much work. ;)

So, I thought about doing something like

opt1 <- '+ opts(panel.grid.major = theme_line(colour = "white")'

and then define each plot like

pl_x <- pl_x + opt1
pl_y <- pl_y + opt1
...

Other options (margins, fonts, scales,…) could then be added to opt1.
However, that doesn’t work (error message when trying to print pl_x). Anybody maybe knows how to accomplish what I’d like to do?

I also played around with theme_set and theme_update, but that resulted in none off my plots working anymore unless I completely restarted R.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一梦等七年七年为一梦 2024-10-02 07:04:48

您不必添加 + 号。

opt <- opts(panel.grid.major = theme_line(colour = "white"))

pl_x <- pl_x + opt

虽然这不起作用:

opt <- opts(...) + scale_y_continuous(..)

这确实有效:

opt <- opts(...)
syc <- scale_y_continuous(...)
pl_x <- pl_x + opt + syc

并且感谢 Hadley 的示例,这也有效:

opt <- list(opts(...),scale_y_continuous(...))

注意:自版本 0.9.2 opts 以来,替换主题

You don't have to add the + sign.

opt <- opts(panel.grid.major = theme_line(colour = "white"))

pl_x <- pl_x + opt

Although this doesn't work:

opt <- opts(...) + scale_y_continuous(..)

This does:

opt <- opts(...)
syc <- scale_y_continuous(...)
pl_x <- pl_x + opt + syc

And thanks to Hadley's example, this works too:

opt <- list(opts(...),scale_y_continuous(...))

Note: Since version 0.9.2 opts has been replace by theme.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文