rplotunicodeaxis-labelsperspective

Why some unicode subscripts appear as rectangles in axes labels of persp3D in R?


I'd like to plot this persp3D with a z-axis label (zlab) that includes "art" and "2" as subscript.
With persp3D, "xlab, ylab, zlab titles for the axes must be character strings; expressions are not accepted"; therefore, I used unicodes, as suggested here.
However, the subscript "t" ("\u209C") and subscript "2" ("\u2082") are replaced by rectangles, surprisingly:

enter image description here

Data:

x <- c(4, 13.5555555555556, 23.1111111111111, 32.6666666666667, 42.2222222222222, 
51.7777777777778, 61.3333333333333, 70.8888888888889, 80.4444444444444, 
90)  
y <- c(340, 344.444444444444, 348.888888888889, 353.333333333333, 
357.777777777778, 362.222222222222, 366.666666666667, 371.111111111111, 
375.555555555556, 380)  
z <- structure(c(-0.0322313447424346, 0.0285984869688307, 0.0887786175071123, 
0.16315053227001, 0.23707067250712, 0.295511090896593, 0.347041501569685, 
0.406272829045792, 0.477401790172139, 0.554597402177076, -0.0408618465172623, 
0.0461728200953287, 0.102347139431252, 0.145375510047931, 0.187038818431414, 
0.226808247176202, 0.277320010159432, 0.338544933887537, 0.407313832540064, 
0.479861624170109, -0.0665649032100802, 0.0215397967370541, 0.0930584796535344, 
0.135250427917803, 0.153588043910132, 0.170031363460435, 0.206134484667028, 
0.256688005216006, 0.311531747422268, 0.368423006904619, -0.0257780437357965, 
0.050144568734309, 0.11354258394355, 0.141955971268351, 0.142183771057, 
0.141149564914805, 0.148191341986988, 0.164701867687502, 0.18690000323886, 
0.211928320228517, 0.106042749781278, 0.141651784681096, 0.162956565523642, 
0.158081721154576, 0.138570215528201, 0.119627888563561, 0.103125810236989, 
0.089213795470101, 0.0773629539419817, 0.0665481663909424, 0.247293191268696, 
0.234184673795509, 0.219203222343268, 0.194474482229333, 0.169630727954004, 
0.141150711654294, 0.104253225576039, 0.0702177561079657, 0.0441733194628657, 
0.0222312187522258, 0.334497158930934, 0.298706836979317, 0.271188556502483, 
0.264412988247681, 0.246811629808477, 0.217765481462091, 0.190266390882058, 
0.169388685218955, 0.154810446935317, 0.143414052229605, 0.316678207994894, 
0.301781511778714, 0.287073854085446, 0.300822065981404, 0.3107500705885, 
0.318149530390912, 0.328053757919202, 0.343099947424488, 0.359954369266852, 
0.377680459212135, 0.275321074040331, 0.271896370112909, 0.278465594804128, 
0.304742755170115, 0.357810501230542, 0.408155983970446, 0.458861330981766, 
0.512002065103596, 0.561983040571977, 0.610294657219977, 0.250520928783517, 
0.25736729590313, 0.26900454601623, 0.313356103865023, 0.379384255611503, 
0.456230669854048, 0.544731976789402, 0.620277571515017, 0.675202603394509, 
0.71960832120062), .Dim = c(10L, 10L))  

Code:

library(plot3D)
library(viridis)

zlab <- "\u0394\u2090\u1d63\u209C\u0070\u004F\u2082\u0028\u0020\u006B\u0050\u0061\u0029"; zlab
par(mfrow=c(1,1), mar=c(2,0,0,6), xpd=TRUE)
persp3D(x = x, y = y, z = z, border="black", resfac=0.5,
        lwd=0.3, xlim=c(0,90), ylim=c(340,380), zlim=c(-0.2,0.8), theta=-60, phi=5, bty = "g", expand = 0.9,
        ticktype="detailed", xlab="x", ylab="y", zlab=zlab, axes=TRUE, nticks=10, col=plasma(100, begin=0.1, end=1),
        colkey = FALSE, cex.lab=2)  

Questions:

  1. What's the problem and how to fix it?
  2. Also, how to rotate it 180 degrees, as displayed on the plot below (which is what I would like but without using text3D)?

Note: I know that text3D is a good option to place precisely the axes labels, as used below; however, it has the tedious drawback of having to be re-adjusted each time the graph is rotated using theta and/or phi, which is why I don't want to use it.

par(mfrow=c(1,1), mar=c(2,0,0,6), xpd=TRUE)
persp3D(x = x, y = y, z = z, border="black", resfac=0.5,
        lwd=0.3, xlim=c(0,90), ylim=c(340,380), zlim=c(-0.2,0.8), theta=-60, phi=5, bty = "g", expand = 0.9,
        ticktype="detailed", xlab="x", ylab="y", zlab=NA, axes=TRUE, nticks=10, col=plasma(100, begin=0.1, end=1),
        colkey = FALSE, cex.lab=2)
text3D(0, 392, 0.4, expression(paste(Delta*""[art], "pO"[2], " (kPa)")), adj = 0.8, add = T, srt=92, cex=1.5)

enter image description here
Thanks for help


Solution

  • It seems that the par() function uses the Helvetica font by default, a font that I don't have on my locale Windows 11 configuration; maybe this is the cause of the problem?

    Anyway, I changed the font (family) in the par() function as follows ("Microsoft Sans Serif", which is a format very close to "Helvetica"), and it solved my problem of unknown unicodes.
    Note: loading extrafonts allows to extend possibilities of different fonts: library(extrafont); extrafont::loadfonts("all")

    Nevertheless, how to rotate this zlab by 180 degrees (Question 2)?

    Code:

    library(plot3D)
    library(viridis)
    # library(extrafont); extrafont::loadfonts("all") # optional
    
    zlab <- "\u0394\u2090\u1d63\u209C\u0070\u004F\u2082\u0020\u0028\u006B\u0050\u0061\u0029"; zlab
    
    windowsFonts(new_font = windowsFont("Microsoft Sans Serif"))
    
    par(mfrow=c(1,1), mar=c(2,0,0,6), xpd=TRUE, family="new_font")
        persp3D(x = x, y = y, z = z, border="black", resfac=0.5,
                lwd=0.3, xlim=c(0,90), ylim=c(340,380), zlim=c(-0.2,0.8), theta=-60, phi=5, bty = "g", expand = 0.9,
                ticktype="detailed", xlab="x", ylab="y", zlab=zlab, axes=TRUE, nticks=10, col=plasma(100, begin=0.1, end=1),
                colkey = FALSE, cex.lab=2)
    

    enter image description here