如何反转 R 中 3D 图中的 z 轴?

问题描述 投票:0回答:1

我绘制了广义加性模型的 3D 透视图。然而,Z 轴

Growers (proportion)
是从上到下读取的。我看到这是默认设置,但我有兴趣反转 Z 轴以从下到上显示值

可重现的示例:

n <- 200
sig2 <- 4
x1 <- runif(n, 0, 1)
x2 <- runif(n, 0, 1)
x3 <- runif(n, 0, 1)
x4 <- runif(n, 0, 1)
y <- x1^2 + x1 * x2 * x4

mod <- gam(y ~ s(x1, x2) + s(x3, x4))


p1 <- wrap_elements(panel = ~vis.gam(mod, view = c("x1",  "x2"), zlab = "Growers (proportion)", 
                                     theta = 120, plot.type = "persp", type = "response", ticktype = "detailed"))
p2 <- wrap_elements(panel = ~vis.gam(mod, view = c("x3",  "x4"), zlab = "Growers (proportion)", 
                                     theta = 35, plot.type = "persp", type = "response", ticktype = "detailed"))

fig <- p1 + p2 + plot_layout(ncol = 2, tag_level = 'new') +
  plot_annotation(tag_levels = list(c('(a)', '(b)'))) 
fig

这是当前的情节。如您所见,Z 轴

Growers (proportion)
从上到下读取。我们能以某种方式反转 Z 轴吗?

r ggplot2 plot visualization mgcv
1个回答
0
投票

从评论来看,

vis.gam
的替代品似乎是可以接受的。我们可以使用
plotly
中的曲面图来做同样的事情,但它首先需要一些数据操作,将边际模型预测放入每个图的矩阵中:

df1 <- expand.grid(x1 = seq(0, 1, 0.01), x2 = seq(1, 0, -0.01),
                  x3 = mean(x3), x4 = mean(x4))
x1_x2 <- matrix(predict(mod, newdata = df), ncol = 101)

df2 <- expand.grid(x3 = seq(0, 1, 0.01), x4 = seq(1, 0, -0.01),
                  x1 = mean(x1), x2 = mean(x2))
x3_x4 <- matrix(predict(mod, newdata = df), ncol = 101)

现在我们可以做:

library(plotly)

p1 <- plot_ly(x = seq(0, 1, 0.01), y = seq(0, 1, 0.01),
              z = x1_x2, type = "surface",
              colorscale = list(c(0, 0.9, 1), c("red", "yellow", 'white')))

p2 <- plot_ly(x = seq(0, 1, 0.01), y = seq(0, 1, 0.01),
              z = x3_x4, type = "surface",
              colorscale = list(c(0, 0.9, 1), c("red", "yellow", 'white')))

给我们:

p1

p2

© www.soinside.com 2019 - 2024. All rights reserved.