Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't label multi-panel figure in r while using ggplot

Tags:

r

ggplot2

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.

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:

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!

like image 323
user2221184 Avatar asked Mar 28 '13 19:03

user2221184


1 Answers

You could use ggtitle and 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)

enter image description here

like image 135
Matthew Plourde Avatar answered Sep 21 '22 15:09

Matthew Plourde