2010-10-05 194 views
28

我嘗試使用+opts(subtitle="text")添加小標題,但沒有顯示出來。主標題確實有效(+opts(title="text"))。如何添加小標題並更改R中ggplot圖的字體大小?

我還想爲軸(標籤和座標)使用較大的字體,但我不知道該怎麼做。

+0

對於第一個問題,有一個更好的答案在這裏:http://stackoverflow.com/questions/11724311/how-to-add-a-ggplot2 -subtitle-with-different-size-and-color – naught101 2014-08-29 02:51:41

+0

將問題標記爲重複的,因爲有一個更新的問題對我來說看起來很奇怪。那麼爲什麼這被標記爲重複? – Jaap 2014-08-29 08:35:07

回答

45

theme_get()會告訴你,你可以在opts()使用 「隱藏」 選項,張貼0.91這是theme()

電流:

theme(axis.text.x=element_text(size=X)) 
theme(axis.text.y=element_text(size=X)) 

前0.91:

opts(axis.text.x=theme_text(size=X)) 
opts(axis.text.y=theme_text(size=X)) 

將尺寸更改爲您所需的尺寸。

WRT標題,您可以使用 「\ n」 來剩餘的文本移動到新行:

電流:

labs(title="text \n more text") 

前0.91:

opts(title="text \n more text") 

ggplot2沒有「字幕」功能。但是,您可以使用任何標籤中的\ n項來刪除一行。

+1

+1太棒了! ''theme_get()' – Legend 2011-10-18 23:14:12

+3

'theme_text'現在不再使用'element_text'代替。我會說更多,但我正在尋找如何自己使用'element_text'。幫助只是一個存根。 – geneorama 2012-09-13 18:14:59

+0

嘗試更新你的軟件包。該文檔已得到改進,我也更新了我的答案。 – 2012-09-13 18:40:39

3

更新:ggplot版本2.2.0可以做字幕,如例如,在this blog post

例子:

library(ggplot2) 
packageVersion("ggplot2") ## 2.2.0 
d <- data.frame(x=1:5,y=1:5) 
ggplot(d,aes(x,y))+ 
    labs(title="abc",subtitle="def")+ 
    ## default left-aligned: moved them to center alignment 
    theme(plot.title=element_text(hjust=0.5), 
      plot.subtitle=element_text(hjust=0.5)) 

enter image description here