简体   繁体   中英

How to customize nticks in persp R

I am new to R 3D plotting. I basically have a matrix of size 9x3. Here is what I have to plot them:

###loads the above matrix
d2 <- read.csv("data.csv", header=T, dec=".",sep = " ")
###loads x,y,z
x <- c(2,3,4,5,6,7,8,9,10)
y <- c(3,4,10)
z <- as.matrix(d2)


persp(x, y, z,
  zlab="Score", ylab = "C", xlab="T",
  theta=40, phi=-5, ylim=c(3,10), xlim=c(2,10), 
  nticks = 5, ticktype = "detailed",  
  col="springgreen", shade=0.5)

Everything is fine except that I am not happy with the ticks on the axes.

Is there a way to force the nticks for x , and y , and z ?

For example, is there a way to do something like x.nticks=5 , y.nticks=2 , z.nticks=7 ?

Edit : dput(d2) output:

structure(list(X571.4711426 = c(415.0601344, 398.2104115, 392.3533142, 
389.1941271, 388.099164, 387.919963, 387.8443483, 387.808006, 
387.8046256), X569.3318221 = c(412.6845238, 396.1001583, 390.6924488, 
387.7928128, 386.7903531, 386.6250436, 386.549898, 386.5178556, 
386.5147072), X564.8071975 = c(408.1278631, 392.3859494, 387.5985228, 
384.9989269, 384.0664302, 383.9174889, 383.8442499, 383.8178535, 
383.8151726)), .Names = c("X571.4711426", "X569.3318221", "X564.8071975"
), class = "data.frame", row.names = c(NA, -9L))
d2 <- structure(list(X571.4711426 = c(415.0601344, 398.2104115, 392.3533142, 
                                      389.1941271, 388.099164, 387.919963, 387.8443483, 387.808006, 
                                      387.8046256), X569.3318221 = c(412.6845238, 396.1001583, 390.6924488, 
                                      387.7928128, 386.7903531, 386.6250436, 386.549898, 386.5178556, 
                                      386.5147072), X564.8071975 = c(408.1278631, 392.3859494, 387.5985228, 
                                                                    384.9989269, 384.0664302, 383.9174889, 383.8442499, 383.8178535, 
                                                                    383.8151726)), .Names = c("X571.4711426", "X569.3318221", "X564.8071975"
                                                                    ), class = "data.frame", row.names = c(NA, -9L))
d2

x <- c(2,3,4,5,6,7,8,9,10)
y <- c(3,4,10)
z <- as.matrix(d2)

pmat <- persp(x, y, z,
              zlab="Score", ylab = "C", xlab="T",
              theta=40, phi=-5, ylim=c(3,10), xlim=c(2,10), 
              col="springgreen", shade=0.5ticks
)

min.x  <- min(x)
max.x  <- max(x)
x.axis <- seq(min.x,max.x,by=2) # by = 2 will get you 5 ticks
min.y  <- min(y)
max.y  <- max(y)
y.axis <- seq(min.y,max.y, by = 5) # by = 5 will get you 2 ticks
min.z  <- round(min(z))
max.z  <- round(max(z))
z.axis <- seq(min.z, max.z, by=5) # by = 5 will get you 7 ticks 

tick.start <- trans3d(x.axis, min.y, min.z, pmat)
tick.end   <- trans3d(x.axis, (min.y - 0.20), min.z, pmat)
segments(tick.start$x, tick.start$y, tick.end$x, tick.end$y)

#Note the (min.y - 0.20) in the calculation of tick.end. This places the second line, parallel to the X axis, at the position -0.20 on the Y axis (i.e., into negative/unplotted space).

#The tick marks on the Y and Z axes can be handled similarly:

tick.start <- trans3d(max.x, y.axis, min.z, pmat)
tick.end   <- trans3d(max.x + 0.20, y.axis, min.z, pmat)
segments(tick.start$x, tick.start$y, tick.end$x, tick.end$y)

tick.start <- trans3d(min.x, min.y, z.axis, pmat)
tick.end <- trans3d(min.x, (min.y - 0.20), z.axis, pmat)
segments(tick.start$x, tick.start$y, tick.end$x, tick.end$y)

labels <- as.character(x.axis)
label.pos <- trans3d(x.axis, (min.y - 0.25), min.z, pmat)
text(label.pos$x, label.pos$y, labels=labels, adj=c(0, NA), srt=270, cex=0.5)

#The adj=c(0, NA) expression is used to left-justify the labels, the srt=270 expression is used to rotate the labels 270°, and the cex=0.5 expression is used to scale the label text to 75% of its original size.

#The labels on the Y and Z axes are produced similarly:
labels <- as.character(y.axis)
label.pos <- trans3d((max.x + 0.25), y.axis, min.z, pmat)
text(label.pos$x, label.pos$y, labels=labels, adj=c(0, NA), cex=0.5)

labels <- as.character(z.axis)
label.pos <- trans3d(min.x, (min.y - 0.5), z.axis, pmat)
text(label.pos$x, label.pos$y, labels=labels, adj=c(1, NA), cex=0.5)

# And you can of course change the specific settings to whatever you want

Fully Custom Ticks Example 习惯

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