ggplot2:使用多个图的选项 [英] ggplot2: Use options for multiple plots

查看:96
本文介绍了ggplot2:使用多个图的选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

  + opts(panel.grid.major = theme_line(color =white)

给每个图表定义,但是当我决定改变背景颜色让我们说grey25时, d)必须单独修改每一个情节,这似乎是太多的工作了;)

因此,我想过要做一些像

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

然后定义每个图

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

然后,可以将其他选项(边距,字体,比例...)添加到opt1。
但是,这并不起作用(尝试打印pl_x时出现错误信息)任何人都知道如何完成我想要的喜欢做什么?



我也玩过theme_set和theme_update,但是没有得到我的plo除非我完全重新启动R,否则不会再工作了。

解决方案

您不必添加+号。

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

pl_x < - pl_x + opt

虽然这不起作用:

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

这样做:

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

感谢哈德利的例子,这也是有效的:

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

注意:由于版本0.9.2 opts 已被替换主题


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.

解决方案

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.

这篇关于ggplot2:使用多个图的选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆