我是 R 3D 绘图的新手。我基本上有一个大小为 9x3 的矩阵。这是我必须绘制它们的内容:
###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)
一切都很好,只是我对轴上的刻度不满意。
有没有办法强制
x
、 y
和 z
的nticks?例如,有没有办法做类似
x.nticks=5
、 y.nticks=2
、 z.nticks=7
的事情?编辑 : dput(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 <- 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
完全自定义刻度示例
关于r - 如何在 persp R 中自定义 nticks,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37571376/