I made a bar plot with ggplot2 that contains a data table within its plot area. How can I colorize text elements within this data table?
I know how to colorize text with "geom_richtext", but how is it done in a data table ?
The example below draws the bar plot, a data table within the area, and text (where colors can be applied). But how can they be applied to table elements (e.g. to the words within table cells)?
library(ggplot2)
library(ggtext)
library(ggpmisc)
library(ggpp)
data = data.frame(
colors = c('black, red, black again', 'green, yello, now blue'),
x = c(1, 2),
y = c(5, 10))
gg = ggplot(data, aes(x = x, y = y)) +
geom_bar(stat = 'identity', fill = 'blue')
gg = gg +
annotate(geom = 'table', x = 1, y = 7, label = list(data), parse = F)
mytext = glue::glue("black, <span style = \'color: red;\'>red</span>, black again ....")
gg = gg +
geom_richtext(aes(x = 1.5, y = 3, label = mytext), label.color = NA)
gg