Remi.b Remi.b - 2 months ago 13
R Question

ggplot2: Add name of variable used for facet_grid

require(ggplot2)
ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(vs+gear ~ cyl+am)


enter image description here

I would like to add the name of the 4 variables used for facet_grid on this graph. I suppose the best way to do so would be to add the name of the variables in the corners with a small arrow pointing to the row or column. I was thinking to use
annotation_custom
and
textGrob
for this purpose but failed to get anything printed on the graph.

Answer

Something like this?

ggplot(mtcars, aes(mpg, wt)) + geom_point() + facet_grid(vs+gear ~ cyl+am,labeller = labeller(.rows = label_both, .cols = label_both))

You can also use syntax like so:

labeller = label_bquote("Gear"==.(gear))
Comments