【问题标题】:How to make a default custom theme with ggplot2 in R如何在 R 中使用 ggplot2 制作默认自定义主题
【发布时间】:2014-12-10 12:23:38
【问题描述】:

当我尝试使用 ggplot2 应用自定义主题时,会出现如下错误:

Error in FUN("text"[[1L]], ...) : 
  Theme element 'text' has NULL property: family, face, size, hjust, vjust, angle, lineheight

我想我必须在这里错过一些基本的东西(我第一次尝试创建自定义主题)。 主题是基于theme_bw()创建的:

theme_new <- function(base_size = 12, base_family = "Helvetica"){
    theme_bw(base_size = base_size, base_family = base_family) %+replace%
    theme(
        line = element_line(colour="black"),
        text = element_text(colour="black"),
        axis.title = element_text(size = 14),
        axis.text = element_text(colour="black", size=8),
        strip.text = element_text(size=12),
        legend.key=element_rect(colour=NA, fill =NA),
        panel.grid = element_blank(),   
        panel.border = element_rect(fill = NA, colour = "black", size=1),
        panel.background = element_rect(fill = "white", colour = "black"), 
        strip.background = element_rect(fill = NA)
        )
    }

那就试试吧:

x

theme_set(theme_new())

qplot(x)

得到上面的错误!

但是:

theme_set(theme_bw())

qplot(x)

工作正常!

我猜this stackoverflow post 中描述的theme_update 与使用theme_set() 更改默认主题不同。 如果我们看一下这个小插图 (http://docs.ggplot2.org/dev/vignettes/themes.html) 中的新主题指南,我的理解是,要么需要指定所有主题参数并使用 complete=TRUE 来说明这一点;或者使用%+replace%operator 向旧主题添加一些东西,比如theme_bw()。 还是不明白!

【问题讨论】:

    标签: r ggplot2 themes


    【解决方案1】:

    http://docs.ggplot2.org/dev/vignettes/themes.html 的简要介绍

    因此,在使用 %+replace% 运算符创建新的主题函数时,需要非常小心地替换位于继承层次结构顶部的主题元素,例如文本、线条和矩形。

    ...

    请注意,在 theme_bw 中替换的主题元素主要在 theme_grey() 中具有 NULL 属性,因为后者中的大多数默认属性在元素 rect、line 和 text 中定义并传递给它们的子元素。 %+replace% 运算符用于在 theme() 中指定的选定元素中设置非 NULL 属性,并将所有未声明的属性设置为 NULL。

    因此,您应该注释掉包括 linetextrect 在内的规范,因为它们已经在父主题中定义:theme_bwtheme_grey

    theme_new <- function(base_size = 12, base_family = "Helvetica"){
      theme_bw(base_size = base_size, base_family = base_family) %+replace%
        theme(
          #line = element_line(colour="black"),
          #text = element_text(colour="black"),
          axis.title = element_text(size = 14),
          #axis.text = element_text(colour="black", size=8),
          #strip.text = element_text(size=12),
          legend.key=element_rect(colour=NA, fill =NA),
          panel.grid = element_blank(),   
          panel.border = element_rect(fill = NA, colour = "black", size=1),
          panel.background = element_rect(fill = "white", colour = "black"), 
          strip.background = element_rect(fill = NA)
          )
    }
    

    qplot(x) + theme_new() 生成以下图像,其中包含与字体相关的大量警告。

    当在不同的机器上时,它几乎可以生成我尝试过的任何图而没有任何警告,所以我想它可以工作! 比如http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/中的第二组图复制为

    【讨论】:

    • 带有所有这些警告,这不是解决方案。无论如何,当你做另一个情节时它不起作用。
    • 我很想知道你不能用它制作什么特定的情节。看看答案的编辑版本。
    • 是的,它有助于删除带有“基本”设置的行(尽管并不完全清楚为什么会这样),我猜这些警告都与丢失的 windows 字体有关,可以很容易地修复。
    猜你喜欢
    • 2019-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多