Reputation: 185
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!
Upvotes: 2
Views: 3083
Reputation: 1580
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.
Upvotes: 11