6

I have a faceted ggplot2 scatterplot, and would like to print summary statistics about the linear regression on each facet, as has been done here and here. Unlike those examples, I am using scales="free", and the ranges of the data in each facet are quite different, but I would like the summary statistics to show up in the same relative position in each facet (e.g. top right corner, or whatever). How can I specify to geom_text or annotate that the label should appear in the same position relative to the panel?

Where I am right now:

# Fake data
set.seed(2112)
x <- c(1:10, 6:15)
y <- x + c(runif(10), runif(10)*10)
l <- gl(2, 10)
d <- data.frame(x=x, y=y, l=l)

# Calculate a summary statistic (here, the r-squared) in a separate data frame
r_df <- ddply(d, .(l), summarise, rsq=round(summary(lm(y~x))$r.squared, 2))

# Use geom_text and a separate data frame to print the summary statistic
ggplot(d, aes(x=x, y=y)) +
  geom_text(data=r_df, aes(x=8, y=8, label=paste("rsq=", rsq)))+
  geom_point() + 
  facet_wrap(~l, scales="free")

enter image description here

I would like, instead, to have ggplot automatically position the text in the same relative position in each facet.

Community
  • 1
  • 1
Drew Steen
  • 16,045
  • 12
  • 62
  • 90

1 Answers1

17

If you want to place them relative to the corners, you can achieve that by specifying an x or y position of Inf or -Inf:

ggplot(d, aes(x=x, y=y)) +
  geom_text(data=r_df, aes(label=paste("rsq=", rsq)), 
            x=-Inf, y=Inf, hjust=-0.2, vjust=1.2)+
  geom_point() + 
  facet_wrap(~l, scales="free")

enter image description here

I also adjusted hjust and vjust so the label was not in the exact corner of the graph by pushed away from it a bit.

Brian Diggs
  • 57,757
  • 13
  • 166
  • 188
  • I'm guessing a more general solution involves delving into grid graphics? – Drew Steen Aug 26 '13 at 20:06
  • @DrewSteen Delving into grid would be needed for the more general solution because all specifications inside the plot area in ggplot are specified in data space and for the same relative position you want to specify them in display space. – Brian Diggs Aug 26 '13 at 20:08
  • You could also change the facet labels to include this information as seen in [the R Cookbook](http://www.cookbook-r.com/Graphs/Facets_(ggplot2)/#modifying-facet-label-text) – Tyler Rinker Aug 26 '13 at 20:55
  • @DrewSteen where do you want them, that +/- Inf doesn't already cover? – baptiste Aug 26 '13 at 21:07
  • @baptiste: Ideally, I'd like to be able to place them at any arbitrary position within the facet window (e.g., 32% of the way from left to right, 67% of the way from bottom to top, or whatever). – Drew Steen Aug 27 '13 at 03:03
  • 3
    @DrewSteen `p + annotation_custom(grob=textGrob("hi", x=0.32, y=1-0.67))` – baptiste Aug 27 '13 at 10:55
  • @baptiste I forgot about `annotation_custom` and its specification in terms of relative coordinates within the plot window. – Brian Diggs Aug 27 '13 at 15:20
  • i often wish I could forget about it, it's rather buggy – baptiste Aug 27 '13 at 15:23
  • @baptiste Was just playing with it and could not figure out how to put a different annotation in each facet. Is that a functionality that it has? – Brian Diggs Aug 27 '13 at 15:27