简体   繁体   中英

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?

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:

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM