首页 > 解决方案 > 使用 plotly (RStudio) 绘制 3D 曲面函数

问题描述

我正在使用,我想知道是否可以在 RStudio 中绘制曲面函数 f(x,y)=z。实际上,我想绘制一架飞机。现在,我正在绘制的是这样的:

fit3 <- lm(y1~x1+ealax2,data = datos2)

# Outer part ~~~~~~~~~~~~~~~~~~~~~
x <- (0:250)/250
y <- (0:250)*100
beta <- fit3$coefficients
fxy <- function(x,y){
  beta[1] + beta[2]*x + beta[3]*y
}
z <- outer(X = x,Y = y,FUN = fxy)
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

plot_ly(x=x,y=y,z = t(z), type = "surface") %>% 
  add_trace(data = datos2, x = ~x1, y = ~ealax2, z = ~y1,
            mode = "markers", type = "scatter3d", 
            marker = list(size = 5, color = "red"))

我得到了这个:

带有散点图的曲面

我想要的不是做“外部”部分,即:

x <- (0:250)/250
y <- (0:250)*100
beta <- fit3$coefficients
fxy <- function(x,y){
  beta[1] + beta[2]*x + beta[3]*y
}
z <- outer(X = x,Y = y,FUN = fxy)

所以事情会更容易。我的意思是,有更简单的方法可以在 R 中绘制 y=f(x) 函数,例如:

curve(cos(x),from=-2,to=5)

而且我正在寻找这样的东西,但在情节上,而且还有 3D 情节。有没有办法做到这一点,或者最后是 2D 版本?

您将能够从这个站点下载我的数据:datos2.csv

标签: rplotlyscatter-plotcurvesurface

解决方案


推荐阅读