Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a default custom theme with ggplot2 in R

Tags:

r

ggplot2

themes

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

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_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)

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 stackoverflow 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!

like image 573
user3375672 Avatar asked Dec 10 '14 12:12

user3375672


1 Answers

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

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.

...

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.

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.

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. enter image description here

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 asenter image description here

like image 93
Khashaa Avatar answered Oct 04 '22 19:10

Khashaa