Prompted by Jake Tolbert’s request for comparison of ggthemes and ggthemr.

library('ggplot2')
library('ggthemes')
library('ggthemr')

Save ggplot2’s original theme settings:

theme_gg <- theme_get()

1 A simple plot:

p <- ggplot(mtcars, aes(factor(cyl), fill = factor(gear))) +
  geom_bar(alpha = 2/3) +
  facet_grid(.~am) +
  labs(x = 'Cylinders', fill = 'Gears')

p +
  labs(title = 'ggplot2 default')

2 ggplot2 & ggthemes

theme_minimal(), plus some theme elements, and a scale from ggthemesscale_fill_few():

p + 
  theme_minimal(base_family = 'serif') +
  theme(axis.ticks = element_line(colour = 'grey90')
        , strip.background = element_rect(fill = 'grey90', colour = 'grey90')
        , panel.border = element_rect(colour = 'grey90', fill = NA)) +
  scale_fill_few('medium') +
  labs(title = 'theme_minimal() + theme() elements + scale_fill_few()')

2.1 What’s in scale_fill_few?

scale_fill_few
## function (palette = "light", ...) 
## {
##     discrete_scale("fill", "few", few_pal(palette), ...)
## }
## <environment: namespace:ggthemes>

light by default, a discrete scale with scale_name “few”, with a palette selected from few_pal. Help indicates one way to get the 7 values used for fill:

few_pal('medium')(7)
## [1] "#F15A60" "#7AC36A" "#5A9BD4" "#FAA75B" "#9E67AB" "#CE7058" "#D77FB4"

The function shows another way:

few_pal
## function (palette = "medium") 
## {
##     values <- ggthemes_data$few[[palette]]
##     n <- length(values)
##     manual_pal(unname(values[2:n]))
## }
## <environment: namespace:ggthemes>

The first element of 8 is a ‘gray’, for background fills I believe:

ggthemes_data$few[['medium']]
##      gray       red     green      blue    orange    purple    maroon 
## "#737373" "#F15A60" "#7AC36A" "#5A9BD4" "#FAA75B" "#9E67AB" "#CE7058" 
##   magenta 
## "#D77FB4"

I did try extending the named characters in ggthemes_data$few[['medium']] by 1 for a particular plot:

str(ggthemes_data$few[['medium']])
##  Named chr [1:8] "#737373" "#F15A60" "#7AC36A" "#5A9BD4" ...
##  - attr(*, "names")= chr [1:8] "gray" "red" "green" "blue" ...

However, I didn’t have time to get it to work so used values from few_pal('medium')(7) in a scale_fill_manual, adding the extra colour (‘bisque1’ #FFE4C4).

scale_fill_manual(drop = FALSE, values = c('#F15A60','#7AC36A','#5A9BD4'
                                             ,'#FAA75B','#9E67AB','#CE7058'
                                             ,'#D77FB4','#FFE4C4'))

Requiring so many colours suggests too many categories might be involved for a reader to make immediate sense of, so I wouldn’t encourage it.

3 ggthemr

ggthemr('dust')
p +
  labs(title = 'ggthemr(\'dust\')')

3.1 Global Environment has changed

scale_colour_ and scale_fill_ functions have been created in the Global Environment:

ls()
## [1] "p"                       "scale_colour_continuous"
## [3] "scale_colour_discrete"   "scale_colour_gradient"  
## [5] "scale_fill_continuous"   "scale_fill_discrete"    
## [7] "scale_fill_gradient"     "theme_gg"

e.g. scale_fill_discrete() now returns the function in the Global Environment:

scale_fill_discrete()
## discrete_scale(aesthetics = "fill", scale_name = "ggthemr", palette = discrete_colours(colours))

It exists in two places:

getAnywhere('scale_fill_discrete')
## 2 differing objects matching 'scale_fill_discrete' were found
## in the following places
##   .GlobalEnv
##   package:ggplot2
##   namespace:ggplot2
## Use [] to view one of them
getAnywhere('scale_fill_discrete')[1]
## function (...) 
## discrete_scale("fill", "ggthemr", discrete_colours(colours), 
##     ...)
## <environment: 0x000000000ad58128>
getAnywhere('scale_fill_discrete')[2]
## function (..., h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, 
##     direction = 1, na.value = "grey50") 
## {
##     discrete_scale("fill", "hue", hue_pal(h, c, l, h.start, direction), 
##         na.value = na.value, ...)
## }
## <environment: namespace:ggplot2>

I’d not sure why the ggthemes approach wouldn’t work instead, but I haven’t built a package, so may well be missing something. I suppose the idea is the normal scale_ functions can remain in use and code, with no need to use package specific functions like scale_fill_few.

3.2 Reset to ggplot2 defaults:

ggthemr_reset()

scale_colour_ and scale_ functions have been removed from the Global Environment:

ls()
## [1] "p"        "theme_gg"

scale_ functions are now only in ggplot2’s namespace:

getAnywhere('scale_fill_discrete')
## A single object matching 'scale_fill_discrete' was found
## It was found in the following places
##   package:ggplot2
##   namespace:ggplot2
## with value
## 
## function (..., h = c(0, 360) + 15, c = 100, l = 65, h.start = 0, 
##     direction = 1, na.value = "grey50") 
## {
##     discrete_scale("fill", "hue", hue_pal(h, c, l, h.start, direction), 
##         na.value = na.value, ...)
## }
## <environment: namespace:ggplot2>

The original plot:

p +
  labs(title = 'ggthemr_reset()')

4 Using theme_set and theme_update

Apply theme_minimal() again, but using theme_set and theme_update to make them persistent settings:

theme_set(theme_minimal(base_family = 'serif'))

theme_update(axis.ticks = element_line(colour = 'grey90')
             , strip.background = element_rect(fill = 'grey90', colour = 'grey90')
             , panel.border = element_rect(colour = 'grey90', fill = NA))
p + 
  scale_fill_few('medium') +
  labs(title = 'theme_set() & theme_update(), after ggthemr_reset()')

Restore original theme:

theme_set(theme_gg)
p +
  labs(title = 'theme_set() back to the original settings')