ggplot2:使用多个绘图的选项
我想创建 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不必添加 + 号。
虽然这不起作用:
这确实有效:
并且感谢 Hadley 的示例,这也有效:
注意:自版本 0.9.2
opts
以来,替换为主题
。You don't have to add the + sign.
Although this doesn't work:
This does:
And thanks to Hadley's example, this works too:
Note: Since version 0.9.2
opts
has been replace bytheme
.