1

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?

Community
  • 1
  • 1
Daniel
  • 7,252
  • 6
  • 26
  • 38

1 Answers1

2

you can simply redefine the relevant scale,

scale_colour_discrete <- function(...) 
  scale_colour_brewer(..., palette="Set1")
baptiste
  • 75,767
  • 19
  • 198
  • 294
  • In some cases your solution works, in other cases it doesn't. However, I haven't figured out a pattern when and why? When I overwrite the scale_fill..., a plot with dodged bars does not apply the modified theme. However, if I manually add a `+ scale_fill...` to the ggplot-object, I get the warning: scale_fill exist, will be overwritten. So, why is my redefined scale not applied? Hm... It would be great if geom colors could be set via theme options as well, since this seems to work perfectly. I guess, I will keep a "bar color" parameter for my plotting-function. – Daniel Sep 25 '14 at 08:48
  • it's hard to tell without an example... Regarding your point on theme vs geom defaults, I fully agree, and suggested something along those lines a few times in the past. It's not going to happen unfortunately, and ggplot2 is not being developed anymore. – baptiste Sep 25 '14 at 10:38
  • True, an example would be helpful. However, all this stuff is integrated in my package, but I could commit the latest code to GitHub, so everbody can check the source - I thought, this might be too much work, so I tried to ask this question in a "generic" way. BTW, where did you read that ggplot2 is not being developed anymore? – Daniel Sep 25 '14 at 12:46
  • [see announcement last march](https://groups.google.com/forum/#!topic/ggplot2/SSxt8B8QLfo) – baptiste Sep 26 '14 at 11:16