1
votes

I like to write a function, which globally sets specific theme options and scale aesthetics for ggplot objects, comparable to the ggthemr-package.

I can modify theme options with following code-snippet:

sjtheme <- theme_grey() +
  theme(axis.text.x = element_text(angle=axis.angle.x, size=rel(axis.size.x), colour=axis.color.x), 
        axis.text.y = element_text(angle=axis.angle.y, size=rel(axis.size.y), colour=axis.color.y), 
        axis.title = element_text(size=rel(axis.title.size), colour=axis.title.color),
        axis.ticks = element_line(colour=axis.ticks.color),
        axis.ticks.length = unit(axis.ticks.length, "cm"),
        plot.title = element_text(size=rel(title.size), colour=title.color),
        plot.background = element_rect(colour=plot.bordercol, fill=plot.backgroundcol),
        panel.background = element_rect(colour=panel.bordercol, fill=panel.backcol),
        panel.grid.minor = element_line(colour=panel.minor.gridcol),
        panel.grid.major = element_line(colour=panel.major.gridcol))
theme_set(sjtheme)

geom defaults can be changed e.g. with

update_geom_defaults('boxplot', list(fill = geom.colors, alpha = geom.alpha, outlier.colour = geom.colors))

now this both works well, so whenever I create a ggplot-object, my theme and geom options are applied.

But how do I do this with scales, so bar/dot/line colors get a new default as well?

There is a solution in the ggthemr-package: https://github.com/cttobin/ggthemr/blob/master/R/theme_scales.R

However, this approach modifies the global environment, which would violate CRAN submission policies. All suggestions I found via search eninge on the web don't work, like "set_scale_defaults" (which has been removed in newer ggplot versions) or this posting or this posting.

So, is there a possibility to change scale defaults that don't change the user's global environment?

1

1 Answers

2
votes

you can simply redefine the relevant scale,

scale_colour_discrete <- function(...) 
  scale_colour_brewer(..., palette="Set1")