Matt L.
Matt L.

Reputation: 2954

Symbol issue in ggplot title using ggtext

When using unicode characters with the new ggtext package, which expands text formatting options, I get the wrong characters in the title and legend labels.

note: requires ggtext from github (not on CRAN yet). to install, run: devtools::install_github("clauswilke/ggtext")

This issue appears to be a system issue on my computer, as the package developer (Claus Wilke) could not reproduce the same issue on his.

If someone can guide me- why would it be printing the wrong symbol in the plot (titles and legend labels)? also does the same thing if using "μ" instead of the "\u03bc" syntax.

Here is system info:

> sessionInfo()
R version 3.6.1 (2019-07-05)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 17763)

Matrix products: default

locale:
[1] LC_COLLATE=English_United States.1252  LC_CTYPE=English_United States.1252    LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C                           LC_TIME=English_United States.1252    

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base   
library(ggplot2)
library(ggtext)

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) +
  geom_point(size = 3) +
  scale_color_manual(
    name = NULL,
    values = c(setosa = "#0072B2", virginica = "#009E73", versicolor = "#D55E00"),
    labels = c(
      setosa = "<i style='color:#0072B2'>I. setosa  \u03bc </i>",
      virginica = "<i style='color:#009E73'>I. virginica  \u03bc </i>",
      versicolor = "<i style='color:#D55E00'>I. versicolor  \u03bc </i>")
  ) +
  labs(
    title = "**Fisher's *Iris* dataset  (test unicode symbol: \u03bc)**  
    <span style='font-size:11'>Sepal width vs. sepal length for three *Iris*
    species  \u03bc </span>",
    x = "Sepal length (cm)\n (test unicode symbol: \u03bc)", 
    y = "Sepal width (cm)\n (test unicode symbol: \u03bc)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_markdown(lineheight = 1.1),
    legend.text = element_markdown(size = 11)
  )

Created on 2019-08-09 by the reprex package (v0.3.0)

Upvotes: 2

Views: 1163

Answers (2)

Matt L.
Matt L.

Reputation: 2954

Claus isolated this to an issue in Rcpp, and has worked around it using an updated in gridtext. It only affects Windows systems, apparently. To fix this, update to the new version on github:

devtools::install_github("clauswilke/gridtext")

Upvotes: 0

Claus Wilke
Claus Wilke

Reputation: 17790

I cannot reproduce the issue, but the following reprex should help pinpoint the source of the problem. It mimics the processing pipeline that is executed inside ggtext.

library(grid)

x <- c(.2, .4, .6, .8)
y <- c(.8, .6, .4, .2)

# original input
text <- c("special char:  \u03bc ")

# convert markdown to html
text2 <- markdown::markdownToHTML(text = text, options = c("use_xhtml", "fragment_only"))

# parse html
doctree <- xml2::read_html(text2)
text3 <- xml2::as_list(doctree)$html$body$p[[1]]

# break text nodes into individual words that get rendered
text4 <- stringr::str_split(stringr::str_squish(text3), "[[:space:]]+")[[1]][3]

# the final rendering is done via grid::textGrob
grid.newpage()
grid.draw(textGrob(c(text, text2, text3, text4), x, y))

Created on 2019-08-10 by the reprex package (v0.3.0)

Upvotes: 2

Related Questions