如何在R中绘制三维散点图?

时间:2016-04-15 05:52:51

标签: r matlab plot

我有MATLAB代码,我试图转换为R。

这是MATLAB代码:

x1=[2 3 1 2 2.5];
x2=[2 3 2.1 2 2.5];
yy=[2 3 6 6.5 9.5];

xx=[x1; x2];
AA=yy*xx'*inv(xx*xx');

xx1=[0 0 4 4];
xx2=[0 4 0 4];
xxx=[xx1; xx2];

yy1=AA*xxx;
yy2=reshape(yy1,2,2);
[mg1,mg2]=meshgrid(0:4:4);

figure;
plot3(x1,x2,yy,'r.');
grid on;
axis([0 4 0 4 0 12]);
hold on;
surf(mg1,mg2,yy2);
view([259 44]);
xlabel('Feature 1');ylabel('Feature 2');zlabel("Responses");

这是我用上面的MATLAB代码

制作的R代码
x1 = matrix(c(2, 3, 1, 2, 2.5), nrow=1, ncol = 5)
x2 = matrix(c(2, 3, 2.1, 2, 2.5), nrow=1, ncol = 5)
yy = matrix(c(2, 3, 6, 6.5, 9.5), nrow=1, ncol = 5)

xx = rbind(x1,x2)

AA = yy %*% t(xx) %*% solve(xx %*% t(xx))

xx1 = matrix(c(0, 0, 4, 4), nrow = 1, ncol = 4)
xx2 = matrix(c(0, 4, 0, 4), nrow = 1, ncol = 4)
xxx = rbind(xx1,xx2)

yy1 = AA %*% xxx
yy2 = matrix(yy1,nrow = 2, ncol = 2)

s3d <-scatterplot3d(x1,x2,yy, pch=16, highlight.3d=TRUE,
                type="h", main="3D Scatterplot")
fit <- lm(yy ~ x1+x2) 
s3d$plane3d(fit)
grid()
persp(mg1,mg2,yy2,
  theta=30, phi=30, expand=0.6,
  col='lightblue', shade=0.75, ltheta=120,
  ticktype='detailed')

此时,我收到以下错误:

  

s3d $ plane3d(fit)中的错误:dims [product 55]与object [0]的长度不匹配

获得的输出:

所需输出:

0 个答案:

没有答案