daqana / tikzDevice

A R package for producing graphics output as PGF/TikZ code for use in TeX documents.
https://daqana.github.io/tikzDevice
132 stars 26 forks source link

unable to use labels = percent_format() in scale #151

Closed fzesch closed 7 years ago

fzesch commented 7 years ago

I want to plot with ggplot and percent_format() from scales like

ggplot(df, aes(Weight.kg)) +
   geom_histogram(aes(y = ..count../(sum(..count..))), binwidth = 0.2,) + 
   scale_y_continuous(labels = percent_format())

But I receive the error

TeX was unable to calculate metrics for the following string or character:

0%

Common reasons for failure include:

  • The string contains a character which is special to LaTeX unless escaped properly, such as % or $.
  • The string makes use of LaTeX commands provided by a package and the tikzDevice was not told to load the package.

Which is of course a perfectly escaped error but nevertheless keeps me from getting my figure.

krlmlr commented 7 years ago

Thanks. The % leaks into the generated LaTeX code. You need to run tikz(sanitize = FALSE) (which prevents you from using LaTeX in your plots) or escape the percent characters yourself (with a custom formatter that wraps percent_format()).

fzesch commented 7 years ago

Thanks. Just for the sake of completeness: The correct command is tikz(sanitize = TRUE) if I want to get the right output. I did not know about this option, thanks for the help!