visreg2d(plot.type = "persp")
使用persp()
作出的圖表。如果我沒有記錯,persp()
不能使用expression()
作爲標籤,並且沒有移動標籤的選項(您可以使用\n
(中斷)(如xlab="\nx-label"
)粗略移動它。所以你需要手動使用text()
。需要兩個值,文本角度和中心座標來執行此操作。在過去,我做了一個函數來計算這些值(我指的是R mailing help),我展示了它。
# top/bot are coordinates(x,y,z) of the axis; pmat is output of persp()
# pos means the label is c(left(-1) or right(1), down(-1) or up(1)) of the axis
persp_lab_f <- function(top, bot, pmat, space, pos = c(-1, -1))
{
coords_3d <- list(top = top, bot = bot)
coords_2d <- lapply(coords_3d, function(v, pmat) unlist(trans3d(v[1], v[2], v[3], pmat)), pmat = pmat)
coords_2d$mid <- (coords_2d$top + coords_2d$bot)/2 # mid is calculated from 2d-coordinates
# coords_2d$mid <- unlist(trans3d(((top + bot)/2)[1], ((top + bot)/2)[2], ((top + bot)/2)[3], pmat)) if use mid in 3d-coordinates
tb_diff <- coords_2d$top - coords_2d$bot
angle <- 180/pi * atan2(tb_diff[2], tb_diff[1])
names(angle) <- "angle"
center <- coords_2d$mid + sqrt(space^2/sum(tb_diff^2)) * rev(abs(tb_diff)) * pos
out <- list(angle = angle, center = as.data.frame(t(center)))
return(out)
}
你沒有顯示reproducible example,我做了它(下次plase提供它)。
fit <- lm(Ozone ~ Solar.R + Wind + Temp + I(Wind^2) + I(Temp^2) +
I(Wind*Temp)+I(Wind*Temp^2) + I(Temp*Wind^2) + I(Temp^2*Wind^2),
data=airquality)
vis_d <- visreg2d(fit, xvar = "Wind", yvar = "Temp", plot.type="persp", type="conditional", theta = 140, phi = 40)
x <- vis_d$x
y <- vis_d$y
z <- vis_d$z
x_top <- c(max(x), max(y), min(z))
x_bot <- c(min(x), max(y), min(z))
y_top <- c(max(x), max(y), min(z))
y_bot <- c(max(x), min(y), min(z))
z_top <- c(max(x), min(y), max(z))
z_bot <- c(max(x), min(y), min(z))
pmat <- persp(x, y, z, theta = 140, phi = 40)
xlab_param <- persp_lab_f(x_top, x_bot, pmat, 0.1, pos = c(1, -1))
ylab_param <- persp_lab_f(y_top, y_bot, pmat, 0.1)
zlab_param <- persp_lab_f(z_top, z_bot, pmat, 0.1)
par(mar=c(1,1,1,1))
visreg2d(fit, xvar = "Wind", yvar = "Temp", plot.type="persp", type="conditional", theta = 140, phi = 40,
xlab = "", ylab = "", zlab = "")
text(xlab_param$center, srt = xlab_param$angle + 180, "xxxxxxxxxx")
text(ylab_param$center, srt = ylab_param$angle, "yyyyyyyyyy")
text(zlab_param$center, srt = zlab_param$angle + 180, labels = bquote(Sigma ~ .("zzzzzzzzzz")))
謝謝,這真是棒極了!理想情況下,我想製作兩個並排圖,所以我使用'par(mfrow = c(1,2))''。但是,z軸標籤正在切斷。我試着改變''mai''和''mar''的值,但實際上這似乎增加了外邊緣區域。如果我將調用中的設置從0.1更改爲更大一些,它只是將文本從圖上移開。有任何想法嗎?如果這會有幫助,我可以在第二個答案中粘貼示例圖。 – user29609
@ user29609; 'xpd = T'解決了這個問題,例如'par(mar = c(1,3,1,1)); visreg2d(...);文字(...,xpd = T)' – cuttlefish44
工作,謝謝! – user29609