2017-06-18 63 views
1

我想为我的所有图表添加一个默认标题,这样我就不必为我制作的所有图表输入它。有没有办法给主题添加默认文本标签?在ggplot中添加默认标签

这是我想要做的。我使用我自己的主题(本例中为theme_bw)。每次制作图表时,我都想避免输入标题。有没有办法在theme_bw()内添加+ labs(caption ="Default")

或者我可以创建两个+ labs(caption ="Default")+ theme_bw()一个新的对象,可以被称为+ labs_and_theme

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

enter image description here

+0

有关ggplot函数式编程的优秀教程:https://rpubs.com/hadley/97970 –

回答

4

你可以做

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() 

参见here

+0

非常感谢。 –