wilkelab / ggtext

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

Font-size with pt < 10 does not work #7

Closed krassowski closed 4 years ago

krassowski commented 4 years ago

First, many thanks for this awesome package - no more dreadful struggles with plotmath!

Not sure if this is intended, or just a bug but setting font-size lower than 10pt results in an error, e.g.: Error: The string '9pt' does not represent a valid CSS unit.. I think that it would be nice to allow values less than 10 points. I've just installed from GitHub master with devtools, R 3.6.1.

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</i>",
      virginica = "<i style='color:#009E73'>I. virginica</i>",
      versicolor = "<i style='color:#D55E00'>I. versicolor</i>")
  ) +
  labs(
    title = "**Fisher's *Iris* dataset**  
    <span style='font-size:9pt'>Sepal width vs. sepal length for three *Iris*
    species</span>",
    x = "Sepal length (cm)", y = "Sepal width (cm)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_markdown(lineheight = 1.1),
    legend.text = element_markdown(size = 11)
  )
#> Warning in e1[n] <- e2[n]: number of items to replace is not a multiple of
#> replacement length

#> Warning in e1[n] <- e2[n]: number of items to replace is not a multiple of
#> replacement length
#> Error: The string '9pt' does not represent a valid CSS unit.

Created on 2019-11-26 by the reprex package (v0.3.0)
krassowski commented 4 years ago

The same happens with 9px (10px works perfectly)

krassowski commented 4 years ago

I guess that the regular expression in clauswilke/gridtext/parse-css.R should be more like:

Hope this helps!

(not 100% sure how to define a non-capturing group here and if this is valid syntax in R regexp)

clauswilke commented 4 years ago

Thanks for reporting. I'll fix this. You're correct the regular expression is wrong. In the mean time, you can write 9.0pt.

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</i>",
      virginica = "<i style='color:#009E73'>I. virginica</i>",
      versicolor = "<i style='color:#D55E00'>I. versicolor</i>")
  ) +
  labs(
    title = "**Fisher's *Iris* dataset**  
    <span style='font-size:9.0pt'>Sepal width vs. sepal length for three *Iris*
    species</span>",
    x = "Sepal length (cm)", y = "Sepal width (cm)"
  ) +
  theme_minimal() +
  theme(
    plot.title = element_markdown(lineheight = 1.1),
    legend.text = element_markdown(size = 11)
  )

Created on 2019-11-26 by the reprex package (v0.3.0)

clauswilke commented 4 years ago

This should work now. Please install the latest version of gridtext and test.

krassowski commented 4 years ago

Works indeed - thank you!