ggplot主题格式可以保存为对象吗? [英] Can ggplot theme formatting be saved as an object?

查看:179
本文介绍了ggplot主题格式可以保存为对象吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

TL; DR:如何保存绘图轴的文本和大小等等,以使对象缩短我的代码?

说例如我想绘制具有可能不同几何图形的不同数据,但使用相同的轴文本大小和标题。



在代码中看起来像这样

  ggplot( data = df,aes(x = x,y = y)+ geom_line()+ 
ylab(我的y轴)+
xlab(my x axis)+
opts (title =my title)+
theme(axis.text = element_text(size = 20),
axis.title = element_text(size = 14,face =bold))


ggplot(data = new_df,aes(x = whatever,y = something)+ geom_anythingelse()+
ylab(我的y轴)+
xlab(我的x轴)+
opts(title =my title)+
theme(axis.text = element_text(size = 20),
axis.title = element_text(size = 14 ,face =bold))

#...

或者我可以保存

  my_theme < -  ylab(my y axis)+ 
xlab(my x (title =my title)+
主题(axis.text = element_text(size = 20),
axis.title = element_text(size = 14,face =bold))



作为自己的对象添加到ggplot当我喜欢。 ggplot的灵活性足以满足我的需求吗?

  ggplot(data = df,aes(x = x,y = y) + geom_point()+ 
my_theme

这个问题是否违反了ggplot的对象命名原则建立在?

解决方案

您可以制作一个没有任何问题的主题对象,例如:

  mytheme <-theme(panel.background = element_rect(color =green))

如果这是您的标准主题类型,则更容易

  old_theme< -  theme_update(panel.background = element_rect(color =green))

写:

  ggplot(...)+ mytheme 

,而在后者中,因为您的自定义主题现在是标准主题,所以只需输入:

  ggplot(...)


TL;DR: How do I save the plotting axis text and sizes and et cetera an object to make my code shorter?

Say for example I wanted to plot different data with potentially different geoms but use the same axis text sizing and titles.

It would look like this in made up code

ggplot(data = df, aes(x = x, y = y) + geom_line() +
    ylab("my y axis") +
    xlab("my x axis") +
    opts(title = "my title") +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=14,face="bold"))


ggplot(data = new_df, aes(x = whatever, y = something) + geom_anythingelse() +
    ylab("my y axis") +
    xlab("my x axis") +
    opts(title = "my title") +
    theme(axis.text=element_text(size=20),
    axis.title=element_text(size=14,face="bold"))

#...

How or can I save

my_theme <-   ylab("my y axis") +
              xlab("my x axis") +
              opts(title = "my title") +
              theme(axis.text=element_text(size=20),
              axis.title=element_text(size=14,face="bold"))

as its own object to add to ggplot when I like. Is ggplot flexible enough to accommodate my need here?

ggplot(data = df, aes(x = x, y = y) + geom_point() +
my_theme

Does this question violate the object naming philosophy that ggplot was built on?

解决方案

You can make a theme object without any problems, e.g:

mytheme<-theme(panel.background=element_rect(colour="green"))

It is even easier, if this is your standard theme to type

old_theme<- theme_update(panel.background=element_rect(colour="green"))

In the former case you write:

ggplot(...)+mytheme

while in the latter, because your custom theme is now the standard theme, it is only necessary to type:

ggplot(...)

这篇关于ggplot主题格式可以保存为对象吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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