2017-06-18 70 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

非常感謝。 –