[英]How to make a default custom theme with ggplot2 in R
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())
theme_set(theme_new())
qplot(x)
qplot(x)
Get the above error! 得到以上错误!
However: 然而:
theme_set(theme_bw())
theme_set(theme_bw())
qplot(x)
qplot(x)
Works fine! 工作正常!
I guess that the theme_update described in this stackoverflow post is not the same as changing the default theme with theme_set(). 我猜这个stackoverflow帖子中描述的theme_update与使用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; 如果我们在此插图中查看新的主题准则( http://docs.ggplot2.org/dev/vignettes/themes.html ),我的理解是,一个人需要指定所有主题参数并使用
complete=TRUE
来告诉这个 OR use the %+replace%
operator to add something to an old theme, like theme_bw(). 或者使用
%+replace%
运算符向旧主题中添加内容,例如theme_bw()。 Dont get it though! 但是不要得到它!
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%运算符创建新的主题函数时,需要非常小心地替换继承层次结构顶部的主题元素,例如文本,行和矩形。
...
...
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.
请注意,替换为theme_bw的主题元素在theme_grey()中主要具有NULL属性,因为后者中的大多数默认属性都在rect,line和text元素中定义,并向下传递到其子元素。 The %+replace% operator is used to set non-NULL properties in the selected elements specified in theme() with all undeclared properties set to NULL.
%+ 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. 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
例如, http://www.cookbook-r.com/Graphs/Scatterplots_ (ggplot2)/中的第二组图被复制为
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.