9

很抱歉这个可能很简单的问题。我是一名程序员,虽然我很少处理图形,在为这个问题苦恼了几个小时之后,是时候寻求帮助了。我正在使用 ggplot 在 r 中创建一个多面板图,但是在使用 ggplot 时,我找不到在图形之外显示图形标签的方法。

这是我希望我的代码执行的操作:

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)

预先感谢您的帮助!

4

2 回答 2

16

你可以使用ggtitleand theme

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)

在此处输入图像描述

于 2013-03-28T19:49:19.520 回答
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 对象,您可能可以避免不得不手动修改这些值。

于 2013-03-28T19:50:50.087 回答