为ggplot2中的多个主题元素分配相同的值 [英] Assign the same value to multiple theme elements in ggplot2

查看:170
本文介绍了为ggplot2中的多个主题元素分配相同的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我觉得这是一个相当简单的问题,但我在任何地方都找不到它。

是否可以在 ggplot2 中将相同的值分配给多个主题元素?例如,我可以编写我的主题声明,如下所示:

pre $主题(axis.text = element_text ,
axis.text.x = element_text(angle = 45,hjust = 1),
line = element_line(color =gray25),
strip.text = element_text(face = ),
legend.title = element_text(color =gray25),
legend.text = element_text(color =gray25),
plot.title = element_text(color = gray25,face =bold,vjust = 2))

但这似乎是高度重复的。是不是有像联合会分配给多个元素相同的值?

 主题(axis.text& legend.title& legend.text& plot.title = element_text(color =gray25),
标签为粗体
axis.text.x = element_text(angle = 45,hjust = 1),
strip.text = element_text(face = ),
plot.title = element_text(face =bold,vjust = 2))


解决方案

在,进行更改并保存,只需添加 + theme_bw_custom (或任何你命名它)到你的绘图代码的结尾。



另见 ggthemes ,这可能是您创建自己的主题时的起点


I feel like this is quite an easy question, but I couldn't find it asked anywhere. Nor could I find anything in the docs.

Is it possible in ggplot2 to assign the same value to multiple theme elements? For instance, I can write my theme declaration as follows:

 theme(axis.text = element_text(colour = "gray25"), 
        axis.text.x = element_text(angle = 45, hjust = 1),
        line = element_line(colour = "gray25"),
        strip.text = element_text(face = "bold"),
        legend.title = element_text(colour = "gray25"),
        legend.text = element_text(colour = "gray25"),
        plot.title = element_text(colour = "gray25", face="bold",vjust=2))

But this seems highly repetetive. Isn't there something like a conjunction to assign the same value to multiple elements? Something like this for instance.

   theme(axis.text & legend.title & legend.text & plot.title = element_text(colour = "gray25"),
        labels bold 
        axis.text.x = element_text(angle = 45, hjust = 1),
        strip.text = element_text(face = "bold"),
        plot.title = element_text(face="bold",vjust=2))

解决方案

In ?theme, you find that "Theme elements can inherit properties from other theme elements". For all individual theme elements, you find which element they inherit from. Thus, you can try to change as many properties as possible in the 'top level' in order to avoid redundancy. You can read more in the official theme vignette, where you also a find visualization of the inheritance between elements:

If you have a theme which you wish to use repeatedly (e.g. for specific journals or publishers), you may customize and save your own themes. For example, you may start from the default theme, make your changes, save it, and use it just by adding + theme_bw_custom (or whatever you name it) to the end of your plotting code.

See also and ggthemes, which may serve as a starting point when creating your own themes.

这篇关于为ggplot2中的多个主题元素分配相同的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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