Rendering plot in R with mono-spaced family font does not display characters anymore

Viewed 568

Update 2020-10-09: the issue does not depend on {ggplot2}, I have provided a more minimal example with some more details

Until some weeks ago, I had no problem to render and correctly see the output of a plot with the modification of characters in monospace. Since some days ago, they did not display anymore.

I have difficulty figuring out how to debug this issue. Can someone suggest some tests or something other to understand the source and maybe solve the issue?

Here a reprex from my local system:

library(showtext)
#> Loading required package: sysfonts
#> Loading required package: showtextdb


plot(1:10)
text(5, 8, "Some text in serif", family = "serif")
text(5, 5, "Some text in mono-spaced", family = "mono")
text(5, 2, "Some text in sans-serif", family = "sans")

showtext_auto() 

plot(1:10)
text(5, 8, "Some text in serif", family = "serif")
text(5, 5, "Some text in mono-spaced", family = "mono")
text(5, 2, "Some text in sans-serif", family = "sans")

Created on 2020-10-09 by the reprex package (v0.3.0)

devtools::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value                       
#>  version  R version 4.0.2 (2020-06-22)
#>  os       Ubuntu 20.04.1 LTS          
#>  system   x86_64, linux-gnu           
#>  ui       X11                         
#>  language (EN)                        
#>  collate  en_US.UTF-8                 
#>  ctype    en_US.UTF-8                 
#>  tz       Europe/Rome                 
#>  date     2020-10-09                  
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version     date       lib source                         
#>  assertthat    0.2.1       2019-03-21 [1] CRAN (R 4.0.2)                 
#>  backports     1.1.10      2020-09-15 [1] CRAN (R 4.0.2)                 
#>  callr         3.4.4       2020-09-07 [1] CRAN (R 4.0.2)                 
#>  cli           2.0.2       2020-02-28 [1] CRAN (R 4.0.2)                 
#>  crayon        1.3.4       2017-09-16 [1] CRAN (R 4.0.2)                 
#>  curl          4.3         2019-12-02 [1] RSPM (R 4.0.2)                 
#>  desc          1.2.0       2018-05-01 [1] CRAN (R 4.0.2)                 
#>  devtools      2.3.2       2020-09-18 [1] CRAN (R 4.0.2)                 
#>  digest        0.6.25      2020-02-23 [1] CRAN (R 4.0.2)                 
#>  ellipsis      0.3.1       2020-05-15 [1] CRAN (R 4.0.2)                 
#>  evaluate      0.14        2019-05-28 [1] CRAN (R 4.0.2)                 
#>  fansi         0.4.1       2020-01-08 [1] CRAN (R 4.0.2)                 
#>  fs            1.5.0       2020-07-31 [1] CRAN (R 4.0.2)                 
#>  glue          1.4.2       2020-08-27 [1] CRAN (R 4.0.2)                 
#>  highr         0.8         2019-03-20 [1] CRAN (R 4.0.2)                 
#>  htmltools     0.5.0       2020-06-16 [1] CRAN (R 4.0.2)                 
#>  httr          1.4.2       2020-07-20 [1] CRAN (R 4.0.2)                 
#>  knitr         1.30        2020-09-22 [1] CRAN (R 4.0.2)                 
#>  magrittr      1.5         2014-11-22 [1] CRAN (R 4.0.2)                 
#>  memoise       1.1.0       2017-04-21 [1] CRAN (R 4.0.2)                 
#>  mime          0.9         2020-02-04 [1] CRAN (R 4.0.2)                 
#>  pkgbuild      1.1.0       2020-07-13 [1] CRAN (R 4.0.2)                 
#>  pkgload       1.1.0       2020-05-29 [1] CRAN (R 4.0.2)                 
#>  prettyunits   1.1.1       2020-01-24 [1] CRAN (R 4.0.2)                 
#>  processx      3.4.4       2020-09-03 [1] CRAN (R 4.0.2)                 
#>  ps            1.3.4       2020-08-11 [1] CRAN (R 4.0.2)                 
#>  R6            2.4.1       2019-11-12 [1] CRAN (R 4.0.2)                 
#>  remotes       2.2.0       2020-07-21 [1] CRAN (R 4.0.2)                 
#>  rlang         0.4.7       2020-07-09 [1] CRAN (R 4.0.2)                 
#>  rmarkdown     2.4         2020-09-30 [1] CRAN (R 4.0.2)                 
#>  rprojroot     1.3-2       2018-01-03 [1] CRAN (R 4.0.2)                 
#>  sessioninfo   1.1.1       2018-11-05 [1] CRAN (R 4.0.2)                 
#>  showtext    * 0.9         2020-08-13 [1] RSPM (R 4.0.2)                 
#>  showtextdb  * 3.0         2020-06-04 [1] RSPM (R 4.0.2)                 
#>  stringi       1.5.3       2020-09-09 [1] CRAN (R 4.0.2)                 
#>  stringr       1.4.0       2019-02-10 [1] CRAN (R 4.0.2)                 
#>  sysfonts    * 0.8.1       2020-05-08 [1] RSPM (R 4.0.2)                 
#>  testthat      2.99.0.9000 2020-10-01 [1] Github (r-lib/testthat@3b0b970)
#>  usethis       1.6.3       2020-09-17 [1] CRAN (R 4.0.2)                 
#>  withr         2.3.0       2020-09-22 [1] CRAN (R 4.0.2)                 
#>  xfun          0.18        2020-09-29 [1] CRAN (R 4.0.2)                 
#>  xml2          1.3.2       2020-04-23 [1] CRAN (R 4.0.2)                 
#>  yaml          2.2.1       2020-02-01 [1] CRAN (R 4.0.2)                 
#> 
#> [1] /home/cl/R/x86_64-pc-linux-gnu-library/4.0
#> [2] /usr/local/lib/R/site-library
#> [3] /usr/lib/R/site-library
#> [4] /usr/lib/R/library

System Font

> X11Fonts()
$serif
[1] "-*-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$sans
[1] "-*-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$mono
[1] "-*-courier-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$Times
[1] "-adobe-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$Helvetica
[1] "-adobe-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$CyrTimes
[1] "-cronyx-times-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$CyrHelvetica
[1] "-cronyx-helvetica-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$Arial
[1] "-monotype-arial-%s-%s-*-*-%d-*-*-*-*-*-*-*"

$Mincho
[1] "-*-mincho-%s-%s-*-*-%d-*-*-*-*-*-*-*"

Thank you

2 Answers

I had exactly the same problem, also on Ubuntu 20.04 (after upgrading from 18.04).

What worked for me is to use family="monospace" (instead of family="mono").

For example:

plot(1:10)
text(5, 8, "Some text in serif", family = "serif")
text(5, 5, "Some text in mono-spaced", family = "monospace")  ## instead of "mono"!
text(5, 2, "Some text in sans-serif", family = "sans")

The result:

monospace

This issue was discussed on the R-help mailing list:

https://stat.ethz.ch/pipermail/r-help/2021-January/469955.html

To summarize:

20.04 uses a more recent version of Pango which does not support Type 1 fonts on Cairo-based graphics devices.

X11.options()$type

shows what kind of X11 device is being used. This is likely cairo. On the command line

fc-match Courier

is likely to point to a .pfb font, which is the problem.

Adding the following to ~/.config/fontconfig/fonts.conf should solve this issue:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
    <match>
        <test name="family">
            <string>Courier</string>
        </test>
        <edit mode="assign" name="file">
            <string>/usr/share/fonts/truetype/dejavu/    DejaVuSansMono.ttf</string>
        </edit>
    </match>
</fontconfig>

Substitute your favorite (ttf/otf) monospaced font above. Double-check that

fc-match Courier

now points to this font. Not sure if this is necessary, but I also refreshed the font cache:

fc-cache -r --verbose --really-force
Related