R plots: Corrupted/missing characters for devices other than pdf

1.7k Views Asked by At

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

4

There are 4 best solutions below

3
user12728748 On

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

6
newmisaac On

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.

0
Francisco Valenzuela On

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
Bratubare On

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.