复制和修改默认主题 [英] Copying and modifying a default theme

查看:146
本文介绍了复制和修改默认主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为 ggplot 创建一个基于 theme_bw()的新主题。



我猜想以下步骤是必需的(伪代码):


  1. 复制theme_bw(): theme_new()< - theme_bw()

  2. 修改副本: theme_update .title.x = theme_text(family = base_family,size = base_size,vjust = 0.5))

任何关于如何实施的建议都将非常感谢!






编辑: @Andrie ,我修改了您的答案以满足我的需求:

  theme_new<  -  theme_set(theme_bw())
theme_new< - theme_update(axis.title.x = theme_text(family = base_family,size = base_size,vjust = 0.5))

然而,我得到以下错误:

  ggplot(mtcars,aes(factor(cyl)))+ geom_bar( )




错误in match(gparname,names(gpars)):object'base_size'not found






编辑: 31/10/2017,@Andrie提供的答案工作得很好。
R版本3.4.1,ggplot2_2.2.1

解决方案

wiki 建议了一种使用 modifyList 的方法,

  theme_new < -  function(base_size = 12,base_family =,...){
modifyList( theme_bw(base_size = base_size,base_family = base_family),
list(axis.title.x = theme_text(family = base_family,
size = base_size,vjust = 0.5)))
}


I would like to create a new theme for ggplot that is based on theme_bw().

I imagine the following steps are necessary (in pseudocode):

  1. Make a copy of theme_bw(): theme_new() <- theme_bw()
  2. Modify the copy: theme_update(axis.title.x = theme_text(family = base_family, size = base_size, vjust = 0.5))

Any advice on how to implement this will be very much appreciated!


Edit: @Andrie, I modified your answer for my needs:

theme_new <- theme_set(theme_bw())
theme_new <- theme_update(axis.title.x = theme_text(family = base_family, size = base_size, vjust = 0.5))

However, I get the following error:

ggplot(mtcars, aes(factor(cyl))) + geom_bar()

Error in match(gparname, names(gpars)) : object 'base_size' not found


Edit: 31/10/2017, answer provided by @Andrie works just fine. R version 3.4.1, ggplot2_2.2.1

解决方案

the wiki suggests one way to do this using modifyList,

theme_new <- function (base_size = 12, base_family = "", ...){
 modifyList (theme_bw (base_size = base_size, base_family = base_family),
          list (axis.title.x = theme_text(family = base_family, 
                size = base_size, vjust = 0.5)))
}

这篇关于复制和修改默认主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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