Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copying and modifying a default theme

I would like to create a new theme for ggplot that is based on theme_bw().

I imagine the following steps are necessary (in pseudocode):

  1. Make a copy of theme_bw(): theme_new() <- theme_bw()
  2. Modify the copy: theme_update(axis.title.x = theme_text(family = base_family, size = base_size, vjust = 0.5))

Any advice on how to implement this will be very much appreciated!


Edit: @Andrie, I modified your answer for my needs:

theme_new <- theme_set(theme_bw()) theme_new <- theme_update(axis.title.x = theme_text(family = base_family, size = base_size, vjust = 0.5)) 

However, I get the following error:

ggplot(mtcars, aes(factor(cyl))) + geom_bar() 

Error in match(gparname, names(gpars)) : object 'base_size' not found


Edit: 31/10/2017, answer provided by @Andrie works just fine. R version 3.4.1, ggplot2_2.2.1

like image 894
donodarazao Avatar asked Sep 18 '11 06:09

donodarazao


2 Answers

Your code just needs a few small changes to work (mainly removing brackets and adding brackets at the right places)

theme_new <- theme_set(theme_bw())  theme_new <- theme_update(     panel.background = element_rect(fill="lightblue"))  ggplot(mtcars, aes(factor(cyl))) + geom_bar() 

enter image description here


Reference:

  • CRAN: extending-ggplot2
  • Tidyverse: themes
  • GitHub: New-theme-system
like image 77
Andrie Avatar answered Sep 25 '22 14:09

Andrie


the wiki suggests one way to do this using modifyList,

theme_new <- function (base_size = 12, base_family = "", ...){  modifyList (theme_bw (base_size = base_size, base_family = base_family),           list (axis.title.x = theme_text(family = base_family,                  size = base_size, vjust = 0.5))) } 
like image 36
baptiste Avatar answered Sep 22 '22 14:09

baptiste