模型3D图(3D散点图+模型表面+连接点到面) [英] 3d plot of models (3d scatterplot + model surface + connecting points to surface)

查看:1624
本文介绍了模型3D图(3D散点图+模型表面+连接点到面)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是这个问题的 3D版。

考虑以下数据:

Income2<-structure(list(X = 1:30, Education = c(21.5862068965517, 18.2758620689655, 
12.0689655172414, 17.0344827586207, 19.9310344827586, 18.2758620689655, 
19.9310344827586, 21.1724137931034, 20.3448275862069, 10, 13.7241379310345, 
18.6896551724138, 11.6551724137931, 16.6206896551724, 10, 20.3448275862069, 
14.1379310344828, 16.6206896551724, 16.6206896551724, 20.3448275862069, 
18.2758620689655, 14.551724137931, 17.448275862069, 10.4137931034483, 
21.5862068965517, 11.2413793103448, 19.9310344827586, 11.6551724137931, 
12.0689655172414, 17.0344827586207), Seniority = c(113.103448275862, 
119.310344827586, 100.689655172414, 187.586206896552, 20, 26.2068965517241, 
150.344827586207, 82.0689655172414, 88.2758620689655, 113.103448275862, 
51.0344827586207, 144.137931034483, 20, 94.4827586206897, 187.586206896552, 
94.4827586206897, 20, 44.8275862068966, 175.172413793103, 187.586206896552, 
100.689655172414, 137.931034482759, 94.4827586206897, 32.4137931034483, 
20, 44.8275862068966, 168.965517241379, 57.2413793103448, 32.4137931034483, 
106.896551724138), Income = c(99.9171726114381, 92.579134855529, 
34.6787271520874, 78.7028062353695, 68.0099216471551, 71.5044853814318, 
87.9704669939115, 79.8110298331255, 90.00632710858, 45.6555294997364, 
31.9138079371295, 96.2829968022869, 27.9825049000603, 66.601792415137, 
41.5319924201478, 89.00070081522, 28.8163007592387, 57.6816942573605, 
70.1050960424457, 98.8340115435447, 74.7046991976891, 53.5321056283034, 
72.0789236655191, 18.5706650327685, 78.8057842852386, 21.388561306174, 
90.8140351180409, 22.6361626208955, 17.613593041445, 74.6109601985289
)), .Names = c("X", "Education", "Seniority", "Income"), class = "data.frame", row.names = c(NA, 
-30L))

如何才能做一个3D绘图模型,包括:3D散点图+模型表面+连接点到面

How can one do a 3d plot of the model, including: 3d scatterplot + model surface + connecting points to surface ?

我使用已经有一个简单而美丽的解决方案 RGL 包:

I already have one easy and beautiful solution using car and rgl packages:

scatter3d(Income ~Seniority + Education, data=Income2, fit="smooth")

不过,我希望看到更多的方式来做到这一点,特别是与基地的图形,和纯 RGL

But I would like to see more ways to do it, specially with base graphics, lattice and "pure" rgl.

推荐答案

我想出如何与基础图形做到这一点:

I figured out how to do it with base graphics:

拟合模型:

model <- loess(Income ~Education + Seniority, data=Income2)

创建了 X 的sequencies:

Create sequencies of x's and y's:

x <-range(Income2$Education)
x <- seq(x[1], x[2], length.out=50)    
y <- range(Income2$Seniority)
y <- seq(y[1], y[2], length.out=50)

X 以Z 值C $ C>:

Create values of z with all the combinations of x and y:

z <- outer(x,y, 
           function(Education,Seniority)
                     predict(model, data.frame(Education,Seniority)))

与剧情 persp

p <- persp(x,y,z, theta=30, phi=30, 
           col="lightblue",expand = 0.5,shade = 0.2,
           xlab="Education", ylab="Seniority", zlab="Income")

项目3D点到2D,这样你就可以使用

obs <- trans3d(Income2$Education, Income2$Seniority,Income2$Income,p)
pred <- trans3d(Income2$Education, Income2$Seniority,fitted(model),p)
points(obs, col="red",pch=16)
segments(obs$x, obs$y, pred$x, pred$y)

这篇关于模型3D图(3D散点图+模型表面+连接点到面)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆