rggplot2ggtext

HTML symbol is not rendering in the ggplot


I am trying to render HTML syntax using ggtext package as below

library(ggplot2)
library(ggtext)
df <- data.frame(
  label = c(
    "Some text **in bold.**",
    "Linebreaks<br>Linebreaks<br>Linebreaks",
    "*x*<sup>2</sup> + 5*x* + *C*<sub>*i*</sub>",
    "Some <span style='color:blue'>blue text **in bold.**</span><br>And *italics text.*<br>
      And some <span style='font-size:18pt; color:black'>large</span> text."
  ),
  x = c(.2, .1, .5, .9),
  y = c(.8, .4, .1, .5),
  hjust = c(0.5, 0, 0, 1),
  vjust = c(0.5, 1, 0, 0.5),
  angle = c(0, 0, 45, -45),
  color = c("black", "blue", "black", "red"),
  fill = c("cornsilk", "white", "lightblue1", "white")
)

ggplot(df) +
  aes(
    x, y, label = label, angle = angle, color = color,
    hjust = hjust, vjust = vjust
  ) +
  geom_point(color = "black", size = 2) +
  scale_color_identity() +
  xlim(0, 1) + ylim(0, 1) + 
  annotate(
                    geom = 'richtext', angle = 90, vjust = .5, hjust = .5, color = Plot_Text_Color, label.colour = NA, fill = NA, 
                    label = "<span style = 'font-size: 20px'>HTML Sign : &#10045;</span>",
                    x = 0.50,
                    y = 0.50) 

As you can see that the HTML symbol &#10045; is not showing in the plot. Is there any way to correct that?


Solution

  • As I mentioned in the comment you can (probably) fix that by using ragg as the graphics device:

    library(ggplot2)
    library(ggtext)
    
    df <- data.frame(
      label = c(
        "Some text **in bold.**",
        "Linebreaks<br>Linebreaks<br>Linebreaks",
        "*x*<sup>2</sup> + 5*x* + *C*<sub>*i*</sub>",
        "Some <span style='color:blue'>blue text **in bold.**</span><br>And *italics text.*<br>
          And some <span style='font-size:18pt; color:black'>large</span> text."
      ),
      x = c(.2, .1, .5, .9),
      y = c(.8, .4, .1, .5),
      hjust = c(0.5, 0, 0, 1),
      vjust = c(0.5, 1, 0, 0.5),
      angle = c(0, 0, 45, -45),
      color = c("black", "blue", "black", "red"),
      fill = c("cornsilk", "white", "lightblue1", "white")
    )
    
    Plot_Text_Color <- "black"
    
    p <- ggplot(df) +
      aes(
        x, y,
        label = label, angle = angle, color = color,
        hjust = hjust, vjust = vjust
      ) +
      geom_point(color = "black", size = 2) +
      scale_color_identity() +
      xlim(0, 1) +
      ylim(0, 1) +
      annotate(
        geom = "richtext", angle = 90, vjust = .5, hjust = .5,
        color = Plot_Text_Color, label.colour = NA, fill = NA,
        label = "<span style = 'font-size: 20px'>HTML Sign : &#10045;</span>",
        x = 0.50,
        y = 0.50
      )
    
    file <- tempfile(fileext = ".png")
    ragg::agg_png(file)
    p
    dev.off()
    #> quartz_off_screen 
    #>                 2
    knitr::include_graphics(file)
    

    Whereas when using the default graphics device the symbol gets displayed as a "box":

    file<- tempfile(fileext = ".png")
    png(file)
    p
    dev.off()
    #> quartz_off_screen 
    #>                 2
    knitr::include_graphics(file)