wilkelab / ggtext

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

ggtext translates dash or minus symbol into unsupported tag #80

Open Matthionine opened 2 years ago

Matthionine commented 2 years ago

Hello,

I encountered some unexpected behavior while using ggtext. Whenever a text passed on to element_markdown() consists only of one or more dash or minus symbols ("-" or "−") it gets interpreted as a <h2> tag and triggers an error as that tag is not supported.

Using &minus; or other html codes that result in a dash or minus symbol do not cause that problem.

Translating a dash or minus into a <h2> tag does not seem intuitive, and I would think that using "−" in a label of a plot is more likely intended than using that as a shortcut for a tag.

See the example below. the first two plots printing either a dash and a minus by their respective html codes or avoiding element_markdown() works without problems. However, as soon as "-" or "−" is used, it produces an error.

library(tidyverse)
library(ggtext)

df <- tibble(x = c("&#45;", "&minus;", "+"),
       y = 1:3)

# works
df %>%
  ggplot(aes(x = x, y = y)) +
  geom_col() +
  theme(axis.text.x = element_markdown())

# now add a row that only has a minus sign "-"
df <- df %>%
  add_row(x = "-", y = 4)

# works
df %>%
  ggplot(aes(x = x, y = y)) +
  geom_col()

# does not work
df %>%
  ggplot(aes(x = x, y = y)) +
  geom_col() +
  theme(axis.text.x = element_markdown())

tidyverse: 1.3.1 ggtext: 0.1.1 (same issue with the development version from github installed via remotes) r: 4.1.3 observed on Windows and Manjaro.