复制和修改默认主题

don*_*zao 30 plot themes r ggplot2

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

我想以下步骤是必要的(在伪代码中):

  1. 制作theme_bw()的副本: theme_new() <- theme_bw()
  2. 修改副本: theme_update(axis.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))
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误:

ggplot(mtcars, aes(factor(cyl))) + geom_bar()
Run Code Online (Sandbox Code Playgroud)

匹配错误(gparname,names(gpars)):找不到对象'base_size'


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

And*_*rie 25

您的代码只需要进行一些小的更改(主要是删除括号并在正确的位置添加括号)

theme_new <- theme_set(theme_bw())

theme_new <- theme_update(
    panel.background = element_rect(fill="lightblue"))

ggplot(mtcars, aes(factor(cyl))) + geom_bar()
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述


参考:


bap*_*ste 11

维基建议为此使用的一种方法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)))
}
Run Code Online (Sandbox Code Playgroud)

  • 注意:现在ggplot2 0.9中引入了新的主题系统,这是多余的. (3认同)
  • [链接描述新的主题系统并描述如何修改它.](https://github.com/wch/ggplot2/wiki/New-theme-system) (2认同)

cro*_*oss 6

对于较新的版本,基于此处的文章

txt <- element_text(size = 14, colour = "black", face = "plain")
bold_txt <- element_text(size = 14, colour = "black", face = "bold")

theme_whatever <- function(base_size = 14, base_family = "Palatino")
{
  theme_bw(base_size = base_size, base_family = base_family) +
  theme(
    legend.key = element_blank(), 
    strip.background = element_blank(), 

    text = txt, 
    plot.title = txt, 

    axis.title = txt, 
    axis.text = txt, 

    legend.title = bold_txt, 
    legend.text = txt ) 
}
Run Code Online (Sandbox Code Playgroud)

请注意,我使用txttxt_bold避免一遍又一遍地写同样的东西.


MYa*_*208 5

试试这个:

### Set up a blank theme
theme_none <- theme(
  panel.grid.major = element_blank(),
  panel.grid.minor = element_blank(),
  panel.background = element_blank(),
  axis.title.x = element_text(colour=NA),
  axis.title.y = element_blank(),
  axis.text.x = element_blank(),
  axis.text.y = element_blank(),
  axis.line = element_blank()
  #axis.ticks.length = element_blank()
)
Run Code Online (Sandbox Code Playgroud)