wilkelab / ggtext

Improved text rendering support for ggplot2
https://wilkelab.org/ggtext/
GNU General Public License v2.0
655 stars 37 forks source link

Unable to change the background color of texts in geom_textbox() #99

Open moomoofarm1 opened 1 year ago

moomoofarm1 commented 1 year ago

I want to change the background color of texts using the HTML tag . But the generated plot does not seem to support the tag here. The text "Fuel economy" should have a background color with the color code "#F7D1CD". But the background color of the text "Fuel economy" in the output plot is now with the color code "#37FAB6".

library(ggplot2)
library(ggtext)
colorSent <- "#37FAB6"
sent <- "<span style = 'background-color:#F7D1CD'>Fuel economy </span> vs. engine displacement"

df <- data.frame(
        label = sent,
        x = c(0.5),
        y = c(0.5),
        hjust = c(0.5), vjust = c(0.5),
        fill = c(colorSent)
)

p <- ggplot2::ggplot() + ggplot2::theme_void() +
    ggplot2::aes(
        x, y,
        label = label,
        fill = fill,
        hjust = hjust, vjust = vjust
    ) + ggtext::geom_textbox(data=df) +
    ggplot2::scale_discrete_identity(aesthetics = c("fill" )) +
    ggplot2::xlim(0, 1) + ggplot2::ylim(0, 1) + 
    ggplot2::theme(plot.background = ggplot2::element_rect(fill="#FCCB58"))  

p
clauswilke commented 1 year ago

Yes, this is not possible, sorry. Only a very limited number of html features are supported.