wilkelab / ggtext

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

Markdown text is not displayed if the string begins with a percent symbol ("%") #67

Closed goaltergeist closed 3 years ago

goaltergeist commented 3 years ago

Hello, thank you for this amazing package. I am encountering a behavior where element_markdown() doesn't render ggplot2 labels if the label string begins with a percent symbol, i.e. %.

A plot with working x axis label and title, formatted in Markdown:

library(tidyverse)
library(ggtext)
mtcars %>%
    ggplot(aes(mpg, cyl/8, color = am)) +
    geom_point() +
    labs(title = "A working _ggtext_ **markdown** title.") +
    scale_x_continuous(name = "Percent of Maximum **Cylinders** (8)") +
    theme(plot.title = element_markdown(), axis.title.x = element_markdown())

image

The same plot, but this x axis label doesn't print as it begins with %:

library(tidyverse)
library(ggtext)
mtcars %>%
    ggplot(aes(mpg, cyl/8, color = am)) +
    geom_point() +
    labs(title = "A working _ggtext_ **markdown** title.") +
    scale_x_continuous(name = "% of Maximum **Cylinders** (8)") +
    theme(plot.title = element_markdown(), axis.title.x = element_markdown())

image

If axis.title.x is set to element_text(), the label beginning with % is displayed:

library(tidyverse)
library(ggtext)
mtcars %>%
    ggplot(aes(mpg, cyl/8, color = am)) +
    geom_point() +
    labs(title = "A working _ggtext_ **markdown** title.") +
    scale_x_continuous(name = "% of Maximum **Cylinders** (8)") +
    theme(plot.title = element_markdown(), axis.title.x = element_text())

image

Lastly, escaping the percent symbol does not work because the \ is escaped rather than the %:

library(tidyverse)
library(ggtext)
mtcars %>%
    ggplot(aes(mpg, cyl/8, color = am)) +
    geom_point() +
    labs(title = "A working _ggtext_ **markdown** title.") +
    scale_x_continuous(name = "\\% of Maximum **Cylinders** (8)") +
    theme(plot.title = element_markdown(), axis.title.x = element_markdown())

image

Is there any way for a label string to begin with %?

clauswilke commented 3 years ago

Use the html entity, %.

library(tidyverse)
library(ggtext)
mtcars %>%
  ggplot(aes(mpg, cyl/8, color = am)) +
  geom_point() +
  labs(title = "A working _ggtext_ **markdown** title.") +
  scale_x_continuous(name = "% of Maximum **Cylinders** (8)") +
  theme(plot.title = element_markdown(), axis.title.x = element_markdown())

Created on 2021-07-12 by the reprex package (v1.0.0)