简体   繁体   中英

Putting axis labels with italics parallel to axes in R rgl package

I am trying to create a 3D plot using the R package 'rgl.' I would like to have all axis labels parallel to the axes but las = 0 does not work and had no luck using text3d() or mtext3d(). Is it possible to do so, depending on your the figure you are seeing? Here are my codes:

X = c(0, 0.2, 0.4, 0.6, 0.8, 1.0)
Y = c(0, 2, 4, 6, 8, 10)
Z = c(100, 220, 440, 660, 880, 1100)
df = data.frame(X, Y, Z)

#install.packages('rgl')
library(rgl)
# Cannot put axis labels parallel to the axes
plot3d(df$X, df$Y, df$Z, type = 'n', box = F, add = F,
xlab = 'Number of cars (N)',
ylab = 'Size (S)',
zlab = 'Weight (kg)')
plot3d(df$X, df$Y, df$Z, type = 'l', lwd = 2, add = T, col = 'blue')
rgl.snapshot('Figure1.png')

and this is what I get after trying to position axes as best as I can.

图1.png

I cannot even figure out this but ideally, I want to make letters in parentheses in italics but it would not let me use substitute and it is ignored. Here are my codes:

# Cannot use italics in the labels
plot3d(df$X, df$Y, df$Z, type = 'n', box = F, add = F,
xlab = substitute(paste('Number of cars (', italic('N'), ')')),
ylab = substitute(paste('Size (', italic('S'), ')')),
zlab = 'Weight (kg)')
plot3d(df$X, df$Y, df$Z, type = 'l', lwd = 2, add = T, col = 'blue')
rgl.snapshot('Figure2.png')

And I lose axis labels entirely.

图2.png

I have spent time searching for examples and documents but I cannot find any way to change the direction of axis labels or use italics. This is my first time using the rgl package and I appreciate any help!

New edits:

Thanks to the answer by user2554330, the labels can include italics but the labels are still positioned far from axes. Both of the following codes work and produce this figure (I can rotate the figure in the interactive window but at no moment all labels align with each axis).

plot3d(df$X, df$Y, df$Z, type = 'n', box = F, add = F,
xlab = expression(paste('Number of cars (', italic('N'), ')')),
ylab = expression(paste('Size (', italic('S'), ')')),
zlab = 'Weight (kg)')
plot3d(df$X, df$Y, df$Z, type = 'l', lwd = 2, add = T, col = 'blue')
rgl.snapshot('Figure3.png')

plot3d(df$X, df$Y, df$Z, type = 'n', box = F, add = F,
xlab = as.expression(substitute(paste('Number of cars (', italic('N'), ')'))),
ylab = as.expression(substitute(paste('Size (', italic('S'), ')'))),
zlab = 'Weight (kg)')
plot3d(df$X, df$Y, df$Z, type = 'l', lwd = 2, add = T, col = 'blue')
rgl.snapshot('Figure3.png')

图3.png

It would be ideal if I can also make all three axes starting from 0 at one of the corners but again, I could not make it happen by rotating the figure.

The problem is that you are not using either a character or expression vector for your labels. You need to use expression() , since substitute() produces a language object (which is an element of an expression vector). (With your original code, I got the error Error in rep(text, len = newlen): attempt to replicate an object of type 'language' Don't ignore error messages!)

For example,

plot3d(df$X, df$Y, df$Z, type = 'n', box = F, add = F,
   xlab = expression(paste('Number of cars (', italic('N'), ')')),
   ylab = expression(paste('Size (', italic('S'), ')')),
   zlab = 'Weight (kg)')

If you really need to use substitute (eg to put a value in the label), then wrap it in as.expression() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM