How to create a custom default theme with ggplot2 in R

When I try to apply a custom theme using ggplot2 , it gets an error, for example:

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

I think I should skip something basic here (my first attempt at creating custom themes). The theme was created based on 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:

x <- rnorm (10)

theme_set (theme_new ())

qplot (x)

Get the above error!

But:

theme_set (theme_bw ())

qplot (x)

Works great!

I am assuming that theme_update, described in https://stackoverflow.com/a/3/4203/... , does not match changing the default theme using theme_set (). If we look at a new topic in this vignette ( http://docs.ggplot2.org/dev/vignettes/themes.html ), I understand that you just need to specify all the parameters for the theme and use complete=TRUE to say this; OR use the %+replace% operator to add something to an old theme, such as theme_bw (). It didn’t work, though!

+8
r ggplot2 themes
source share
1 answer

A brief overview of http://docs.ggplot2.org/dev/vignettes/themes.html shows

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

...

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

So, you should comment on the specifications, including line , text , rect , as they have already been defined in the parent themes: theme_bw and 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() creates the following image with a bunch of warnings related to fonts. enter image description here

When almost any stories that I tried without any warnings were produced on another machine, I think it works! For example, the second set of graphs at http://www.cookbook-r.com/Graphs/Scatterplots_(ggplot2)/ is reproduced as enter image description here

+7
source share

All Articles