Search code examples
rggplot2character-encodingexpressioncairo

Using expression and non-ASCII character in R ggplot axis


I am trying to plot a chart using ggplotwith superscript and non-ASCII characters. While expression with superscript works with simple characters as shown below:

relative part of ggplot script

      labs(
    y = "těžba, C [t]",
    x = "rok") +  
  scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6, accuracy = 1),
                     sec.axis = sec_axis(~.*4.2, name = expression("objem [m"^"3" * "]"),
                     breaks = seq(0,4e7,5e6), labels = function(b) { paste0(round(b/1000000, 0), " M")}))  +
  theme_bw() + theme(panel.grid.major = element_blank(), 
                     panel.grid.minor = element_blank())

ggplot

I get error when trying to use expression with this characters: ěž while on the lext y axis without expression() there is no problem.

scale_y_continuous(labels = unit_format(unit = "M", scale = 1e-6, accuracy = 1),
                   sec.axis = sec_axis(~.*4.2, name = expression("těžba, objem [m"^"3" * "]"),
                   breaks = seq(0,4e7,5e6), labels = function(b) { paste0(round(b/1000000, 0), " M")}))

Error in grid.Call.graphics(C_text, as.graphicsAnnot(x$label), x$x, x$y, : Metric information not available for this family/device

I couldn't find no workaround. Any help aprreciated.


EDIT

The problem occurs when trying to export image using png or cairo-png devices with different error messages. The default device without antialias is working.

- Session info -----------------------------------------------------------
 setting  value                       
 version  R version 4.0.3 (2020-10-10)
 os       Windows 10 x64              
 system   x86_64, mingw32             
 ui       RStudio                     
 language (EN)                        
 collate  Czech_Czechia.1250          
 ctype    Czech_Czechia.1250          
 tz       Europe/Prague               
 date     2021-01-06      

Solution

  • To expand on my comment, here a reproducible example - showing how I tried to reproduce it - which I cannot.

    It may be that your packages are not up to date or simply that you need to restart a new session :)

    library(ggplot2)
    ggplot(mtcars, aes(mpg, disp)) +
    labs( y = expression("těžba, objem [m"^"3" * "]"))
    

    Created on 2021-01-06 by the reprex package (v0.3.0)

    devtools::session_info()
    #> ─ Session info ───────────────────────────────────────────────────────────────
    #>  setting  value                       
    #>  version  R version 4.0.3 (2020-10-10)
    #>  os       macOS Catalina 10.15.7      
    #>  system   x86_64, darwin17.0          
    #>  ui       X11                         
    #>  language (EN)                        
    #>  collate  en_GB.UTF-8                 
    #>  ctype    en_GB.UTF-8                 
    #>  tz       Europe/London               
    #>  date     2021-01-06                  
    #> 
    #> ─ Packages ───────────────────────────────────────────────────────────────────
    #>  package     * version date       lib source        
    
    #>  ggplot2     * 3.3.2   2020-06-19 [1] CRAN (R 4.0.0)