围绕轴标签绘制彩色框

ℕʘʘ*_*ḆḽḘ 2 r ggplot2

考虑这个简单的例子

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()
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

我想提出的X标签(goodneutralbad)以不同的颜色框。例如,good(在x轴和y轴上)将被一个小绿色框包围,依此类推。

我可以在里面做ggplot2吗?

Max*_*lon 6

像这样?

tibble(x = as.factor(c('good', 'neutral', 'bad')),
y = as.factor(c('bad', 'neutral', 'bad'))) %>%
ggplot(aes(x = x, y = y)) + 
  geom_point() + 
  theme(axis.text.x = element_text(color = c('red', 'blue', 'green')))
Run Code Online (Sandbox Code Playgroud)

您的情节:

你的情节

编辑

另一种漂亮的贫民窟解决方案,使用 grid

tibble(x = as.factor(c('good', 'neutral', 'bad')),
y = as.factor(c('bad', 'neutral', 'bad'))) %>%
ggplot(aes(x = x, y = y)) + 
  geom_point() + 
  theme(axis.text.x = element_text(color = c('red', 'blue', 'green')))
Run Code Online (Sandbox Code Playgroud)

贫民窟的方式