R 带函数调用的三维曲面绘制

R 带函数调用的三维曲面绘制,r,ggplot2,3d,R,Ggplot2,3d,我想做一个曲面图,其中z是对pwr.t.test函数的调用,在x和y处求值 d = rep( seq(.05,.1,.01), length(seq(.2,.26,.01)) ) s = rep(seq(.2,.26,.01), length(seq(.05,.1,.01))) M =mesh(d,s) surf3D(x = M$x, y = M$y, z = pwr.t.test(d=M$x/M$y,power=.8,sig.level=.1,type="two.s

我想做一个曲面图,其中z是对pwr.t.test函数的调用,在x和y处求值

d = rep( seq(.05,.1,.01), length(seq(.2,.26,.01)) )
s = rep(seq(.2,.26,.01), length(seq(.05,.1,.01)))
M =mesh(d,s)
surf3D(x = M$x,
       y = M$y,
       z = pwr.t.test(d=M$x/M$y,power=.8,sig.level=.1,type="two.sample",alternative="two.sided")$n        ,
       colkey=FALSE,
       bty="b2",
       main="test")

如何做到这一点?

如果您只是试图绘制所需的样本大小,那么3D绘图可能有点过头了。所以,这里首先是一种制作2D热图的方法

首先,设置X和Y值。请注意,这些不必输入多次(例如,使用
rep

然后,使用
outer
计算每个组合的样本量。我还喜欢设置行/列名以帮助跟踪情况

z <- outer(x, y, getN)
row.names(z) <- x
colnames(z) <- y
这就是我相信你想要绘制的矩阵

在这里,我使用
dplyr
将其转换为长格式,并将其传递到
ggplot
(并使用
viridis
获得漂亮的调色板,尽管其他颜色也很好)

给予

现在,如果您真的想要3D版本,可以使用
plotly
来实现:

plot_ly(
  x = x
  , y = y
  , z = z
  , type = 'surface')
给予

请注意,生成的版本是交互式的

或使用
plot3D

M <- mesh(x,y)

surf3D(
  x = M$x
  , y = M$y
  , z = z
)

M您是否已检查所选函数的矩形角处是否有值
x
y
?(如果此
pwr.t.test
来自pwr包,则它不是。)
           0.2      0.21     0.22     0.23     0.24     0.25     0.26
0.05  550.2098  483.6650 428.5144 382.2977 343.1846 309.7905 281.0524
0.06  631.5180  550.2098 483.6650 428.5144 382.2977 343.1846 309.7905
0.07  732.3030  631.5180 550.2098 483.6650 428.5144 382.2977 343.1846
0.08  859.3212  732.3030 631.5180 550.2098 483.6650 428.5144 382.2977
0.09 1022.5344  859.3212 732.3030 631.5180 550.2098 483.6650 428.5144
0.1  1237.1243 1022.5344 859.3212 732.3030 631.5180 550.2098 483.6650
data.frame(z, check.names = FALSE) %>%
  mutate(xVal = row.names(.)) %>%
  gather(yVal, `Sample Size`, -xVal) %>%
  ggplot(
    aes(x = xVal
        , y = yVal
        , fill = `Sample Size`)) +
  geom_tile() +
  viridis::scale_fill_viridis()
plot_ly(
  x = x
  , y = y
  , z = z
  , type = 'surface')
M <- mesh(x,y)

surf3D(
  x = M$x
  , y = M$y
  , z = z
)