hatmatrix
hatmatrix

Reputation: 44862

ggplot2: geom_text() with facet_grid()?

I just want to add annotation to each panel of figures generated by ggplot2; just simple labels like (a), (b), (c), etc. in each corner. Is there a simple way to do this?

Upvotes: 26

Views: 24658

Answers (3)

emr2
emr2

Reputation: 1722

I found this approach that maybe could be a solution for the problem: https://stackoverflow.com/a/46499601/13997761

Upvotes: 1

hrbrmstr
hrbrmstr

Reputation: 78792

From: https://groups.google.com/forum/?fromgroups=#!topic/ggplot2/RL8M7Ut5EpU you can use the following:

library(ggplot2) 
x <-runif(9, 0, 125) 
data <- as.data.frame(x) 
data$y <- runif(9, 0, 125) 
data$yy <- factor(c("a","b","c")) 

ggplot(data, aes(x, y)) + 
    geom_point(shape = 2) + 
    facet_grid(~yy) + 
    geom_text(aes(x, y, label=lab),
        data=data.frame(x=60, y=Inf, lab=c("this","is","the way"),
             yy=letters[1:3]), vjust=1)

which should give you this:

Upvotes: 37

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

Basically, you create a data.frame with the text which contains a column with the text, and a column with the variables you use for facet_grid. You can then simply add a geom_text with that data.frame. See the documentation of geom_text for more details on text placement and such.

Upvotes: 8

Related Questions