2017-07-27 214 views
0

從ggplot 2.2.0開始,標題默認是左對齊的。爲了讓他們再次爲中心已經在這個崗位已經解釋:使用theme_bw在ggplot2中繪製標題

Center Plot title in ggplot2

這在我的情況下完美的作品爲好,但不是如果我用theme_bw。

dat <- data.frame(
    time = factor(c("Lunch","Dinner"), levels=c("Lunch","Dinner")), 
    total_bill = c(14.89, 17.23) 
) 

ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) + 
    geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") + 
    guides(fill=FALSE) + 
    xlab("Time of day") + ylab("Total bill") + 
    ggtitle("Average bill for 2 people")+ 
    theme(plot.title = element_text(hjust = 0.5))+ 
    theme_bw() 

我試圖傳遞的主題參數theme_bw()

theme_bw(plot.title = element_text(hjust = 0.5)) 

但did`t工作無論是。

任何想法?幫助深表感謝

回答

4

你只需要反轉theme_bwtheme

ggplot(data=dat, aes(x=time, y=total_bill, fill=time)) + 
    geom_bar(colour="black", fill="#DD8888", width=.8, stat="identity") + 
    guides(fill=FALSE) + 
    xlab("Time of day") + ylab("Total bill") + 
    ggtitle("Average bill for 2 people") + 
    theme_bw() + 
    theme(plot.title = element_text(hjust = 0.5)) 

enter image description here

+0

是的,就是這樣。實際上很容易,但沒有看到它。謝謝! – HannesZ

+1

Hi @HannesZ如果這解決了你的問題,請考慮通過點擊複選標記來接受它。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – AntoineBic

+0

對不起,我以爲我已經做到了。我接受得太快:) – HannesZ

0

我一直有同樣的問題,但它似乎是所有需要快速switcheroo!

+1

你應該發佈'快速切換',以便它實際上可以作爲答案;) – mhz

+0

我也有'theme'和'theme_bw'錯誤的方式。 – Steve