如何在R中使用ggplot2创建默认自定义主题

时间:2022-07-22 14:56:08

When I try to apply a custom made theme using ggplot2 it get an error like:

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

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

I think I must miss something basic here (my first try on creating custom themes). The theme was created based on theme_bw():

我想我必须错过一些基本的东西(我第一次尝试创建自定义主题)。主题是基于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)
        )
    }

Then try it out:

然后尝试一下:

x <- rnorm(10)

x < - rnorm(10)

theme_set(theme_new())

qplot(x)

Get the above error!

得到以上错误!

However:

theme_set(theme_bw())

qplot(x)

Works fine!

I guess that the theme_update described in this * post is not the same as changing the default theme with theme_set(). If we look at the new theme guidelines in this the vignette (http://docs.ggplot2.org/dev/vignettes/themes.html) my understanding is that one EITHER need to specify all theme parameters and use the complete=TRUE to tell this; OR use the %+replace%operator to add something to an old theme, like theme_bw(). Dont get it though!

我想这个*帖子中描述的theme_update与使用theme_set()更改默认主题不同。如果我们在这个小插图(http://docs.ggplot2.org/dev/vignettes/themes.html)中查看新的主题指南,我的理解是,一个EITHER需要指定所有主题参数并使用complete = TRUE来告诉这个;或者使用%+ replace%运算符向旧主题添加内容,例如theme_bw()。不要得到它!

1 个解决方案

#1


7  

Brief glimpse over http://docs.ggplot2.org/dev/vignettes/themes.html reveals

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

Therefore, when using the %+replace% operator to create a new theme function, you need to be very careful about replacing theme elements at the top of the inheritance hierarchy such as text, line and rect.

因此,在使用%+ replace%运算符创建新的主题函数时,需要非常小心地替换继承层次结构顶部的主题元素,如text,line和rect。

...

Notice that the theme elements replaced in theme_bw primarily have NULL properties in theme_grey() since most of the default properties in the latter are defined in elements rect, line and text and passed down to their child elements. The %+replace% operator is used to set non-NULL properties in the selected elements specified in theme() with all undeclared properties set to NULL.

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

So, you should comment out the specifications including line, text, rect since they were already defined in the parent themes: theme_bw and theme_grey.

因此,您应该注释掉规范,包括line,text,rect,因为它们已经在父主题中定义:theme_bw和theme_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() produces the following image with bunch of warnings related to the fonts. 如何在R中使用ggplot2创建默认自定义主题

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

When on different machine, it produced virtually any plots I tried without any warnings, so I guess it works! For instance, the second set of plots in http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/ is reproduced as如何在R中使用ggplot2创建默认自定义主题

在不同的机器上,它几乎产生了我试过的没有任何警告的情节,所以我猜它有效!例如,http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/中的第二组图表被复制为

#1


7  

Brief glimpse over http://docs.ggplot2.org/dev/vignettes/themes.html reveals

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

Therefore, when using the %+replace% operator to create a new theme function, you need to be very careful about replacing theme elements at the top of the inheritance hierarchy such as text, line and rect.

因此,在使用%+ replace%运算符创建新的主题函数时,需要非常小心地替换继承层次结构顶部的主题元素,如text,line和rect。

...

Notice that the theme elements replaced in theme_bw primarily have NULL properties in theme_grey() since most of the default properties in the latter are defined in elements rect, line and text and passed down to their child elements. The %+replace% operator is used to set non-NULL properties in the selected elements specified in theme() with all undeclared properties set to NULL.

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

So, you should comment out the specifications including line, text, rect since they were already defined in the parent themes: theme_bw and theme_grey.

因此,您应该注释掉规范,包括line,text,rect,因为它们已经在父主题中定义:theme_bw和theme_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() produces the following image with bunch of warnings related to the fonts. 如何在R中使用ggplot2创建默认自定义主题

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

When on different machine, it produced virtually any plots I tried without any warnings, so I guess it works! For instance, the second set of plots in http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/ is reproduced as如何在R中使用ggplot2创建默认自定义主题

在不同的机器上,它几乎产生了我试过的没有任何警告的情节,所以我猜它有效!例如,http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/中的第二组图表被复制为