1

I would like to add a default caption to all my charts so that I do not have to type it in for all the charts I make. Is there a way to add default text labels to a theme?

Here's what I'd like to do. I'm using my own theme (theme_bw in this example). I would like to avoid typing the caption every time I make a chart. Is there a way to add + labs(caption ="Default") inside theme_bw()?

Or can I create a new object with both + labs(caption ="Default") and + theme_bw() that could be called as + labs_and_theme

ggplot(diamonds[1:20,], aes(x=carat, y=price)) + 
  geom_point() +
  labs(caption ="Default") +
  theme_bw()

enter image description here

1
  • Excellent tutorial on functional programming with ggplot: rpubs.com/hadley/97970 Commented Jun 18, 2017 at 16:16

1 Answer 1

4

You can do

library(ggplot2)
labs_and_theme <- list(
  labs(caption ="Default"),
  theme_bw() 
)
ggplot(diamonds[1:20,], aes(x=carat, y=price)) + 
  labs_and_theme + 
  geom_point() 

See also here.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.