ggplot2: geom_text() with facet_grid()?

前端 未结 2 2067
花落未央
花落未央 2020-12-08 10:57

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?

相关标签:
2条回答
  • 2020-12-08 11:34

    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.

    0 讨论(0)
  • 2020-12-08 11:50

    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:

    0 讨论(0)
提交回复
热议问题