2013-03-28 34 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对象你可以避免不得不修补手动获取这些值。