2

I forked the ggthemes git repo to make my own custom theme. I've figured out how to do almost everything I need, but have one hang up.

I am specifically trying to set the default size for geom_line() in my ggtheme.

Where I'm at now, I have to do something like this:

economics %>%
    ggplot(aes(date, uempmed)) +
    geom_line(size = 1.75) +
    theme_mycustomtheme()

When I would prefer to just have to do this:

economics %>%
    ggplot(aes(date, uempmed)) +
    geom_line() +
    theme_mycustomtheme() # this would set the line size automatically

I have edited my mycustomtheme.R file like so:

theme(
    # Elements in this first block aren't used directly, but are inherited
    # by others
    line =               element_line(
      color = "black", size = 1.75,
      linetype = 1, lineend = "butt"
    )

Note how the size is now set to 1.75. But it doesn't seem to make a difference when I call the theme in practice.

I would appreciate any pointers as to what I may be doing wrong. Thanks!

Adhi R.
  • 177
  • 10
  • Pretty sure theme doesn't control that - the "line" argument in theme controls the various other .line arguments within it, not lines created by geom_line() – iod Nov 21 '18 at 18:20
  • I would think that one would need to have `theme_mycustomtheme <- theme(...)` in the .R file. Otherwise loading it wouldn't have any effect. And you may need to invoke `?theme_update` to get more information. – IRTFM Nov 21 '18 at 18:22

1 Answers1

11

themes don't affect lines in geoms, only lines in axes, gridlines, etc. But, you can change the default appearance of geoms using update_geom_defaults().

#specify geom to update, and list attibutes you want to change appearance of
update_geom_defaults("line", list(size = 1.75))

#now your graph will plot with the line size you defined as default
economics %>%
  ggplot(aes(date, uempmed)) +
  geom_line() 

If you add update_geom_defaults("line", list(size = 1.75)) to the file where you store your custom theme, your geom defaults will also update when you source() your mycustomtheme.r file, and you'll get the linetype you want. Note that setting defaults this way only changes the exact geom specified (line), and does not affect line elements in other geoms (boxplot borders, error bars, etc.), so you will need to define geom defaults for each individual geom you plan to use.

Jan Boyer
  • 1,540
  • 2
  • 14
  • 22
  • Ah interesting, thanks! Confusing how `line` and `rect` don't refer to geoms. The end user is installing my fork of `ggthemes` though, not `source()`ing it. Is there a way to have that update either when someone calls `library(ggthemes)` or `theme_mycustomtheme()`? Basically I want to make it as easy as possible for the end-user to use and a way of baking in `update_geom_defaults()` would be really ace! – Adhi R. Nov 21 '18 at 22:38
  • 1
    The way ggplot2 works, the aesthetics for geoms are separate from themes. Themes only handle the way the non-data/background aspects of the plot are rendered. Also, there's no reason to fork ggthemes to create a new theme. I suggest creating a new package with the theme for your users to install. – jrnold Nov 22 '18 at 19:19