wilkelab / ggtext

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

Umlauts and other special characters add spacing to labels #19

Closed pasipasi123 closed 3 years ago

pasipasi123 commented 4 years ago

My native language has a lot of umlauts so I came across this bug. Using the provided example (rendering a reprex is also impossible for some reason):

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:11pt'>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)
  )

image

clauswilke commented 4 years ago

I can't reproduce this. Are you using Windows? Can you try a different graphics device (e.g., pdf)?

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:11pt'>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 2020-02-10 by the reprex package (v0.3.0)

clauswilke commented 4 years ago

Also, do the umlauts in the title and caption render correctly if you're not using element_markdown()?

pasipasi123 commented 4 years ago

Yes, Windows, and the behavior is the same on two different Windows machines. I copied the image file a posted above from Rstudio's Plots window. If I save the plot to a file, pdf or png, the text displays just fine!

image

clauswilke commented 4 years ago

Ok, that's what I thought. The Windows graphics device is known to have issues.

Could you run the following two examples and post the output from RStudio's plot windows?

library(ggplot2)

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 = "úúúúú",
      virginica = "äääää",
      versicolor = "aaaaa")
  ) +
  theme_minimal()


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 = "aaaaa",
      virginica = "aaaaa",
      versicolor = "aaaaa")
  ) +
  theme_minimal()

Created on 2020-02-11 by the reprex package (v0.3.0)

clauswilke commented 4 years ago

Specifically, is the spacing to the right of the legend the same in both cases?

clauswilke commented 3 years ago

Closing this, as it is a graphics device issue.