Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: geom_text() with facet_grid()?

Tags:

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?

like image 248
hatmatrix Avatar asked Apr 07 '13 20:04

hatmatrix


People also ask

What is the function of Facet_grid () in Ggplot ()?

facet_grid() forms a matrix of panels defined by row and column faceting variables. It is most useful when you have two discrete variables, and all combinations of the variables exist in the data.

What is the difference between Facet_wrap and Facet_grid?

While facet_grid shows the labels at the margins of the facet plot, facet_wrap creates a label for each plot panel.

What does Facet_wrap do in Ggplot?

facet_wrap() makes a long ribbon of panels (generated by any number of variables) and wraps it into 2d. This is useful if you have a single variable with many levels and want to arrange the plots in a more space efficient manner.


2 Answers

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:

like image 141
hrbrmstr Avatar answered Oct 06 '22 03:10

hrbrmstr


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.

like image 38
Paul Hiemstra Avatar answered Oct 06 '22 03:10

Paul Hiemstra