TBP
TBP

Reputation: 747

Text labels for ggplot2 facet_wrap

I am having trouble putting letter labels on a facet_wrapped ggplot2 graph. It should be simple and I have done it before... although obviously I am not doing something right at the moment. The letters are just plotting atop one another in every plot when there should be a different letter in each plot. Has something changed recently or am I overlooking the obvious?

library(ggplot2)
library(reshape2)
posdat<-data.frame(  x=c(rep(10,4)),
                     y=c(rep(0.4,4)),     
                   lab=c("A","B","C","D"),
                    stringsAsFactors = FALSE)
sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
sp+geom_text(data=posdat, aes(x=10, y=0.4, label=lab))+
facet_wrap( ~ day, ncol=2)

Upvotes: 3

Views: 1228

Answers (1)

Rorschach
Rorschach

Reputation: 32416

If you add a day identifier to your posdat data, then the text can be split amongst the facets.

posdat$day <- unique(tips$day)

sp <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
sp+geom_text(data=posdat, aes(x=10, y=0.4, label=lab))+
  facet_wrap( ~ day, ncol=2)

enter image description here

Upvotes: 3

Related Questions