使用ggplot时,无法在r中标记多面板图形

时间:2021-11-11 14:56:25

Sorry for the possibly simple question. I'm a programmer, though I rarely deal with graphics, and after tearing my hair out for hours with this problem, it's time to get some help. I'm creating a multi-panel plot in r using ggplot, but I cannot find a way to display figure labels, outside of the figure, when using ggplot.

抱歉可能是简单的问题。我是一名程序员,虽然我很少处理图形,并且在用这个问题撕掉头发数小时后,是时候得到一些帮助。我正在使用ggplot在r中创建一个多面板图,但是在使用ggplot时,我无法找到在图之外显示图形标签的方法。

Here is what I want my code to do:

这是我想要我的代码做的事情:

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)

How would I get those "a" and "b" labels, in the location that they are in for the figure created by the above code, into this type of code:

如何将这些“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)

Thank you in advance for your help!

预先感谢您的帮助!

2 个解决方案

#1


16  

You could use ggtitle and theme:

你可以使用ggtitle和主题:

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)

使用ggplot时,无法在r中标记多面板图形

#2


3  

Two (less than ideal) options:

两个(不太理想)的选择:

#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)

For the grid.text option, if you delve into the ggplot object you can probably avoid having to tinker to get those values right manually.

对于grid.text选项,如果您深入研究ggplot对象,您可以避免修改以手动获取这些值。

#1


16  

You could use ggtitle and theme:

你可以使用ggtitle和主题:

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)

使用ggplot时,无法在r中标记多面板图形

#2


3  

Two (less than ideal) options:

两个(不太理想)的选择:

#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)

For the grid.text option, if you delve into the ggplot object you can probably avoid having to tinker to get those values right manually.

对于grid.text选项,如果您深入研究ggplot对象,您可以避免修改以手动获取这些值。