2013-10-27 57 views
16

這裏標題位置是代碼:如何調整GGPLOT2

require(ggplot2) 
require(grid) 
# pdf("a.pdf") 
png('a.png') 
a <- qplot(date, unemploy, data = economics, geom = "line") + opts(title='A') 
b <- qplot(uempmed, unemploy, data = economics) + geom_smooth(se = F) + opts(title='B') 
c <- qplot(uempmed, unemploy, data = economics, geom="path") + opts(title='C') 
grid.newpage() 
pushViewport(viewport(layout = grid.layout(2, 2))) 
vplayout <- function(x, y) viewport(layout.pos.row = x, layout.pos.col = y) 
print(a, vp = vplayout(1, 1:2)) 
print(b, vp = vplayout(2, 1)) 
print(c, vp = vplayout(2, 2)) 
dev.off() 

而且結果:

enter image description here

雖然這裏是我想有,即位置附近的標題是什麼y軸的頂部:

enter image description here

回答

23

你什麼正在尋找的是theme(plot.title = element_text(hjust = 0))。例如,使用GGPLOT2的最新版本,並theme代替opts我們

a <- qplot(date, unemploy, data = economics, geom = "line") + ggtitle("A") + 
    theme(plot.title = element_text(hjust = 0)) 

或者,opts

a <- qplot(date, unemploy, data = economics, geom = "line") + 
    opts(title = "A", plot.title = element_text(hjust = 0)) 

enter image description here

+1

住雖然它看起來像他們還沒有更新的GGPLOT2一段時間,所以也許使用'opts'而不是'theme' –

+0

@Julius我會如何調整標題,使其在劇情內?我嘗試了'vjust',但它不能與'hjust'結合使用。我正在尋找將標題移到劇情內右上角。 – Meli

+1

@Meli,我不認爲你的問題有類似的解決方案;你應該考慮簡單地將標題插入到文本中,如http://stackoverflow.com/questions/14928711/position-ggplot-title-at-the-top-right-of-the-plot – Julius