Consider this simple example
library(tidyverse)
tibble(x = as.factor(c('good', 'neutral', 'bad')),
y = as.factor(c('bad', 'neutral', 'bad'))) %>%
ggplot(aes(x = x, y = y)) + geom_point()
I would like to put the x labels (good
, neutral
, bad
) in different colored boxes. For instance, good
(on both the x and y axis) would be surrounded on a small green box, and so on.
Can I do that in ggplot2
?
Like this?
Your Plot:
EDIT
An alternate pretty Ghetto solution using
grid
One approach could be this:
With
geom_rect()
you can add colored backgrounds:Another approach
Create a vector of colors and pass them into
axis.text.x()
option oftheme()
.Solution using
geom_label
outside the plot area:Another solution
You should create
geom_rect
with borders, but without fill and plot them outside the plot area (usingcoord_cartesian
):