Search code examples
rplotjupyterjupyter-labcairo

All texts in Jupyter R plot in get scrambled if Cairo package is installed


When I plot in a Jupyter + R session, whether it is a Notebook or R console, base R or ggplot2 plots, texts all get scrambled like this:

R plot with scrambled texts

sessionInfo:

R version 4.3.3 (2024-02-29)
Platform: x86_64-conda-linux-gnu (64-bit)
Running under: Debian GNU/Linux 12 (bookworm)

Matrix products: default
BLAS/LAPACK: /home/adam/opt/mambaforge/envs/R433new/lib/libopenblasp-r0.3.27.so;  LAPACK version 3.12.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

time zone: US/Central
tzcode source: system (glibc)

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] digest_0.6.37     IRdisplay_1.1     utf8_1.2.4        base64enc_0.1-3  
 [5] fastmap_1.2.0     glue_1.7.0        htmltools_0.5.8.1 repr_1.1.7       
 [9] lifecycle_1.0.4   cli_3.6.3         fansi_1.0.6       vctrs_0.6.5      
[13] pbdZMQ_0.3-11     Cairo_1.6-3       compiler_4.3.3    tools_4.3.3      
[17] evaluate_0.24.0   pillar_1.9.0      crayon_1.5.3      rlang_1.1.4      
[21] jsonlite_1.8.8    IRkernel_1.3.2    uuid_1.2-1 

Notice that Cairo is loaded.

However, if I launch an R session in Linux terminal, or if set output to a pdf/png file, or if I uninstall Cairo package and start a new Jupyter session, all texts will be normal. Texts in plots remain normal even if I do library('Cairo') in a Linux terminal session. I am using R-4.3.3 in above code only because I thought the issue was with latest R, which turned out to be wrong. Any idea? Thank you very much!

I tried conda r-cairo, different versions of CRAN Cairo packages by install.packages('Cairo') and also different versions of R, none worked. Texts always get distorted as in above example.


Solution

  • As suggested by Simon Urbanek, this problem may be solved by changing the default font:

    CairoFonts(regular="sans:style=Regular")
    

    https://github.com/s-u/Cairo/issues/49