简体   繁体   English

在R中平滑3d图

[英]Smoothing 3d plot in R

I made 3d plot in rgl.persp3d but I don't know how to smooth that to see trend. 我在rgl.persp3d中制作了3d图,但我不知道如何使它平滑以查看趋势。 Or maybe next solution is to implement wireframe in rgl.persp3d (because I need this plot to be interactive). 或者,下一个解决方案是在rgl.persp3d中实现线框(因为我需要此图是交互式的)。 Please, help. 请帮忙。

library(mgcv)


x<- rnorm(200)
y<- rnorm(200)
z<-rnorm(200)

tab<-data.frame(x,y,z)
tab

#surface wireframe:

mod <- gam(z ~ te(x, y), data = tab)

wyk <- matrix(fitted(mod), ncol = 20) #8 i 10 też ok

wireframe(wyk, drape=TRUE, colorkey=TRUE)

线框

#surface persp3d


library(rgl)
library(akima)


z_interpolation <- 200

tabint <- interp(x, y, z)

x.si <- tabint$x
y.si <- tabint$y
z.si <- tabint$z
nbcol <- 200
vertcol <- cut(t, nbcol)
color = rev(rainbow(nbcol, start = 0/6, end = 4/6))
persp3d(x.si, y.si, z.si, col = color[vertcol], smooth=T)

persp3d

So wireframe is neither smoothed nor interactive ...and rgl.persp3d is interactive but no smoothed. 因此,线框既不会平滑也不会交互……并且rgl.persp3d是交互式但没有平滑。 And I can't have both smoothed and interactive. 而且我不能兼顾顺畅和互动。

rgl just draws what you give it. rgl只是画出您所提供的。 You need to use mgcv as in your first example to do the smoothing, but you don't get a matrix of fitted values back at the end, so you'll want to use deldir to turn the results into a surface. 您需要像在第一个示例中一样使用mgcv进行平滑,但是最后没有得到拟合值的矩阵,因此,您需要使用deldir将结果转换为曲面。 For example, 例如,

library(mgcv)

x<- rnorm(200)
y<- rnorm(200)
z<-rnorm(200)

tab<-data.frame(x,y,z)
tab

#surface wireframe:

mod <- gam(z ~ te(x, y), data = tab)

library(rgl)
library(deldir)

zfit <- fitted(mod)
col <- cm.colors(20)[1 + 
         round(19*(zfit - min(zfit))/diff(range(zfit)))]

persp3d(deldir(x, y, z = zfit), col = col)
aspect3d(1, 2, 1)

This gives a nice smooth surface, for example 例如,这样可以使表面光滑

在此处输入图片说明

A simpler way, without the delaunay stuff: 没有delaunay内容的简单方法:

library(mgcv)

x <- rnorm(200)
y <- rnorm(200)
z <- rnorm(200)

tab <- data.frame(x,y,z)

mod <- mgcv::gam(z ~ te(x, y), data = tab)
grid <- -5:5
zfit <- predict(mod, expand.grid(x = grid, y = grid))
persp3d(grid, grid, zfit)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM