Add default label in ggplot

2019-07-24 15:35发布

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

标签: r ggplot2
1条回答
家丑人穷心不美
2楼-- · 2019-07-24 16:14

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.

查看更多
登录 后发表回答