2016-11-16 48 views
0

我使用visreg包和visreg2d()函數來創建顯示兩個gbm模型變量之間相互作用的3D部分依賴關係圖。我需要更改x,y和z軸標籤以及間距,以便爲期刊文章製作精美的圖。我嘗試更改par()中的設置,但這隻允許我更改標籤大小。在gbm模型結果的3D部分圖上更改間距和軸標籤 - visreg包

我生成具有以下情節:

visreg2d(final,n.trees=1000,xvar="ProbMn50ppb",yvar="DTW60Jurgens",plot.type="persp",type="conditional", 
     theta=140,phi=40) 

「最後」是我的模型對象。

我可以通過在visreg2d()命令中設置axes=FALSE來刪除軸。所以我猜測解決方案可能是使用自定義間距創建自定義標籤。我也可以用xlab=, ylab= zlab=更改標籤。然而,另一個問題是我不能使用z標籤的表達式,我需要它。

這裏是我的陰謀目前看起來像標籤和軸: enter image description here

回答

1

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"))) 

enter image description here

+0

謝謝,這真是棒極了!理想情況下,我想製作兩個並排圖,所以我使用'par(mfrow = c(1,2))''。但是,z軸標籤正在切斷。我試着改變''mai''和''mar''的值,但實際上這似乎增加了外邊緣區域。如果我將調用中的設置從0.1更改爲更大一些,它只是將文本從圖上移開。有任何想法嗎?如果這會有幫助,我可以在第二個答案中粘貼示例圖。 – user29609

+1

@ user29609; 'xpd = T'解決了這個問題,例如'par(mar = c(1,3,1,1)); visreg2d(...);文字(...,xpd = T)' – cuttlefish44

+0

工作,謝謝! – user29609

相關問題