Remi.b
Remi.b

Reputation: 18219

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.

Upvotes: 8

Views: 3257

Answers (1)

Dan Slone
Dan Slone

Reputation: 563

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))

Upvotes: 7

Related Questions