2013-03-28 186 views
8

對不起,可能是一個簡單的問題。我是一名程序員,雖然我很少處理圖形,並且在解決這個問題之後把頭髮撕碎幾個小時,現在是時候得到一些幫助。我使用ggplot在r中創建了一個多面板圖,但是當使用ggplot時,我無法找到在圖外部顯示圖標的方法。使用ggplot時無法在r中標記多面板圖形

這是我希望我的代碼做:

par(mfrow = c(1, 2), pty = "s", las = 1, mgp = c(2, 0.4, 0), tcl = -0.3) 
qqnorm(rnorm(100), main = "") 
mtext("a", side = 3, line = 1, adj = 0, cex = 1.1) 
qqnorm(rnorm(100), main = "") 
mtext("b", side = 3, line = 1, adj = 0, cex = 1.1) 

我將如何讓這些「A」和「B」的標籤,因爲它們是在由上面的代碼創建的圖中的位置,進入這種類型的代碼:

df = data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30)) 
p = ggplot(df) + geom_point(aes(x = gp, y = y)) 
p2 = ggplot(df) + geom_point(aes(x = y, y = gp)) 
grid.arrange(p, p2, ncol = 2) 

預先感謝您的幫助!

回答

15

你可以使用ggtitletheme

df = data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30)) 
p = ggplot(df) + geom_point(aes(x = gp, y = y)) + ggtitle('a') + theme(plot.title=element_text(hjust=0)) 
p2 = ggplot(df) + geom_point(aes(x = y, y = gp)) + ggtitle('b') + theme(plot.title=element_text(hjust=0)) 
grid.arrange(p, p2, ncol = 2) 

enter image description here

+0

這比我的構思要好。 – joran

+0

完美的作品!謝謝您的幫助!! – user2221184

+0

@ user2221184很好,沒有問題。請記住點擊旁邊的複選標記以接受答案。 –

3

兩個(低於理想)選項:

#Use faceting, similar to Matthew's ggtitle option 
df = data.frame(gp = factor(rep(letters[1:3], each = 10)), y = rnorm(30)) 
df$lab1 <- 'a' 
df$lab2 <- 'b' 
p = ggplot(df) + geom_point(aes(x = gp, y = y)) + facet_wrap(~lab1) 
p2 = ggplot(df) + geom_point(aes(x = y, y = gp)) + facet_wrap(~lab2) 
j <- theme(strip.text = element_text(hjust = 0.05)) 
grid.arrange(p + j, p2 + j, ncol = 2) 


#Use grid.text 
grid.text(letters[1:2],x = c(0.09,0.59),y = 0.99) 

對於grid.text選項,如果你深入到ggplot對象你可以避免不得不修補手動獲取這些值。