4

I am having a problem where some graphics devices print missing glyph boxes instead of characters. Actually, the only device I have tried so far that renders characters is PDF. Since I recently updated R and rebuilt a bunch of packages, I suspect that may have something to do with it. Here is a screenshot comparing output with four devices, jpeg, pdf, svg, and png.

Although I first encountered the problem in Rstudio with the rcorr package, the issue occurs when I run as an Rscript from the command line and with a basic boxplot.

require(corrplot)
M<-cor(mtcars)
corrplot(M, method="circle")
dev.off()
pdf("test2.pdf")
corrplot(M, method="circle")
dev.off()
png("test2.png")
corrplot(M, method="circle")
dev.off()
jpeg("test2.jpeg")
corrplot(M, method="circle")
dev.off()
svg("test2.svg")
corrplot(M, method="circle")
dev.off()

pdf("test3.pdf")
boxplot(M, method="circle")
dev.off()
png("test3.png")
boxplot(M, method="circle")
dev.off()
jpeg("test3.jpeg")
boxplot(M, method="circle")
dev.off()
svg("test3.svg")
boxplot(M, method="circle")
dev.off()


Session info:


> sessionInfo()
R version 3.6.2 (2019-12-12)
Platform: x86_64-generic-linux-gnu (64-bit)
Running under: Clear Linux OS

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblas_nehalemp-r0.3.7.so

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

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

other attached packages:
[1] showtext_0.7     showtextdb_2.0   sysfonts_0.8     cairoDevice_2.28 corrplot_0.84   

loaded via a namespace (and not attached):
[1] compiler_3.6.2 tools_3.6.2   

outputs:

comparison of graphics devices output problem is not limited to rcorr

Stonecraft
  • 860
  • 1
  • 12
  • 30

4 Answers4

2

There may be an issue between R and a font on your system. Check which font R is using for Helvetica family. I had the same problem and was able to fix it by removing a font package (adobe-base-14-fonts from Arch Linux's AUR).

Another solution that worked for me was to use the Cairo_png function from cairoDevice. The problem with that was that it doesn't fix the Plots tab in RStudio.

You could maybe edit/add ~/.config/fontconfig/fonts.conf to include an alias section, here's an example:

<?xml version="1.0"?>
<!DOCTYPE fontconfig SYSTEM "fonts.dtd">
<fontconfig>
<alias binding="same">
  <family>Helvetica</family>
  <prefer>
    <family>Nimbus Sans</family>
  </prefer>
</alias>
</fontconfig>

Use Nimbus Sans, Arial or any other metric-compatible font. This may affect other programs that are trying to use Helvetica on your system, but you can at least see if it's really the problem.

newmisaac
  • 21
  • 2
  • 1
    How do I know which font R is using? Is that specific to each graphic device or global? – Stonecraft Jan 25 '20 at 08:19
  • Cairo_png does work for me, but I want to be able to see what I'm doing in the plots pane. – Stonecraft Jan 25 '20 at 08:58
  • 1
    Try loading `Cairo` and running `CairoFontMatch(fontpattern="Helvetica")`. I'm not sure if that is always the same font that the PNG device uses. – newmisaac Jan 27 '20 at 18:15
  • That outputs `1. family: "Helvetica", style: "Regular", file: "/usr/share/fonts/X11/75dpi/helvR12-ISO8859-1.pcf.gz"` ... now how do I know which font file the problem devices are using? – Stonecraft Jan 28 '20 at 06:41
  • That looks like a Xorg font, I wouldn't try to uninstall it. – newmisaac Jan 28 '20 at 15:47
  • @Stonecraft, does your system follow fontconfig configuration? I don't really know if it's a generic linux thing or if it's distro-specific. I can replicate the problem if I use `helvR12-ISO8859-1.pcf.gz` or fix it if I use something else in the `fonts.conf` file (I've edited the answer above to include an example). – newmisaac Feb 04 '20 at 02:32
0

Have you run BiocManager::valid() to make sure all packages are up to date? That may fix incompatibilities.

user12728748
  • 8,106
  • 2
  • 9
  • 14
0

I was having the same issue in R-studio and some threads on stack overflow suggested installing helvetica, missing font libraries, or to change font config files etc.

What worked for me was to simply go pick one of the fonts from R studio Tools -Global Options - Appearance. In this case I chose ubuntu mono Then in one of my R studio scripts I just ran this line (which I found in a diff. thread), and all my plots started working, I didn't even have to add this to other scripts:

par(family ="Ubuntu Mono")

And I didn't have to change anything else.

*(I'm using Ubuntu 20.4 and R Studio 1.1.456 through an Anaconda environment)

0

I ran into a similar problem after the upgrade to R 4.1. The following worked for me:

  1. Install the ‘ragg’ package and its dependencies
  2. In the Tools menu, open Global Options
  3. Select the “Graphics” tab in the General Section
  4. Select the ‘AGG’ backend

You may need to restart R sessions for this to become active

Cheers.