wilkelab / ggtext

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

Missing spaces in labels of faceted plot using tex/tikz #60

Open aschersleben opened 3 years ago

aschersleben commented 3 years ago

Hi, I'm using the ggtext package in combination with a faceted plot. There seems to be a problem with spaces in the labels when saving the plots as tex/tikz image. I present a reprex to illustrate the problem and a workaround.

In the following reprex, I generate three different images: 1) faceted plot without use of ggtext 2) faceted plot using ggtext in the "normal" way (as I understand), produces only very small spaces between words, every word gets its own node in tex/tikz file 3) faceted plot using ggtext and replacing spaces by "latex spaces" (~), produces normal spaces between words

I'm using texi2pdf to compile the standalone tikz files and magick package to convert pdf to png. Nevertheless, the problem is already visible in the tex files, I present the corresponding lines as comments. The problem also arises when using pictex device instead of tikzDevice::tikz.

I also tried element_textbox() instead of element_markdown(), but got the same results.

library(tidyverse)
library(ggtext)
library(tikzDevice)
library(magick)
#> Linking to ImageMagick 6.9.11.57
#> Enabled features: cairo, freetype, fftw, ghostscript, heic, lcms, pango, raw, rsvg, webp
#> Disabled features: fontconfig, x11

wd <- 5
ht <- 2

# Generate faceted example plot 
p <- mtcars %>%
  mutate(
    lab0 = if_else(am == 0, "label one", "label two"),
    lab1 = if_else(am == 0, "<span style='font-size:20pt'>label one</span>", "label two"),
    lab2 = if_else(am == 0, "<span style='font-size:20pt'>label~one</span>", "label~two")
  ) %>%
  ggplot(aes(mpg, disp)) +
  geom_point()

# normal plot without ggtext
p0 <- p +
  facet_wrap(~ lab0)

ggsave(p0, filename = "p0.tex", width = wd, height = ht)
# output in p0.tex
# \put {label one}  [lB] <0.00pt,0.00pt> at ...

ggsave(p0, filename = "p0-tikz.tex", width = wd, height = ht, device = tikz, standAlone = TRUE)
# output in p0.tex
# \node[text=drawColor,anchor=base,inner sep=0pt, outer sep=0pt, scale=  0.88] at (...) {label one};
tools::texi2pdf("p0-tikz.tex")
image_read_pdf("p0-tikz.pdf") %>%
  image_convert("png") %>%
  image_resize("600x")

# plot with `ggtext` and normal spaces
p1 <- p +
  facet_wrap(~ lab1) +
  theme(strip.text = element_markdown())

ggsave(p1, filename = "p1.tex", width = wd, height = ht)
# output in p1.tex
# \put {label}  [lB] <0.00pt,0.00pt> at ...
# \put {one}  [lB] <0.00pt,0.00pt> at ...

ggsave(p1, filename = "p1-tikz.tex", width = wd, height = ht, device = tikz, standAlone = TRUE)
# output in p1-tikz.tex
# \node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  2.00] at (...) {label};
# \node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  2.00] at (...) {one};
tools::texi2pdf("p1-tikz.tex")
image_read_pdf("p1-tikz.pdf") %>%
  image_convert("png") %>%
  image_resize("600x")

# plot with `ggtext` and latex spaces (~)
p2 <- p +
  facet_wrap(~ lab2) +
  theme(strip.text = element_markdown())

ggsave(p2, filename = "p2.tex", width = wd, height = ht)
# output in p2.tex
# \put {label~one}  [lB] <0.00pt,0.00pt> at ...

ggsave(p2, filename = "p2-tikz.tex", width = wd, height = ht, device = tikz, standAlone = TRUE)
# output in p2-tikz.tex
# \node[text=drawColor,anchor=base west,inner sep=0pt, outer sep=0pt, scale=  2.00] at (...) {label~one};
tools::texi2pdf("p2-tikz.tex")
image_read_pdf("p2-tikz.pdf") %>%
  image_convert("png") %>%
  image_resize("600x")

Created on 2021-03-10 by the reprex package (v1.0.0)