简体   繁体   中英

3D plots and grid.arrange in R

I am attempting to graph three 3D plots and displaying all three at the same time. For the other plots I have been using the grid.arrange function from the package gridExtra. However, I am getting an error with my 3D plots. The error I am getting reads: "Error in gList(list(xyz.convert = function (x, y = NULL, z = NULL) : only 'grobs' allowed in "gList""

My code looks somethings like this:

cont1<-data.frame(matrix(rnorm(300), nrow=10, ncol=3))
pre1<-data.frame(matrix(rnorm(300), nrow=10, ncol=3))
post1<-data.frame(matrix(rnorm(300), nrow=10, ncol=3))

library(scatterplot3d)
attach(as.data.frame(cont1))
p<-scatterplot3d(X1,X2,X3, main="3D Scatterplot")
fit <- lm(X3 ~ X1+X2) 
p$plane3d(fit)


attach(as.data.frame(pre1))
l<-scatterplot3d(X1,X2,X3, main="3D Scatterplot")
fit <- lm(X3 ~ X1+X2) 
l$plane3d(fit)


attach(as.data.frame(post1))
m<-scatterplot3d(X1,X2,X3, main="3D Scatterplot")
fit <- lm(X3 ~ X1+X2) 
m$plane3d(fit)

require(gridExtra)
grid.arrange(p,l,m)

How can I fix this?

I am not sure that grid.arrange can be used with anything by ggplot2 plots... Here is a potential solution using par . If you want a 1x3 arrangement set mfrow=c(1,3) or a 3x1, mfrow=c(3,1)

library(gridExtra)
library(scatterplot3d)

cont1<-data.frame(matrix(rnorm(300), nrow=10, ncol=3))
pre1<-data.frame(matrix(rnorm(300), nrow=10, ncol=3))
post1<-data.frame(matrix(rnorm(300), nrow=10, ncol=3))

par(mfrow=c(2,2))
p<-with(cont1, scatterplot3d(X1,X2,X3, main="3D Scatterplot"))
fit <- lm(X3 ~ X1+X2) 
p$plane3d(fit)

l<-with(pre1, scatterplot3d(X1,X2,X3, main="3D Scatterplot"))
fit <- lm(X3 ~ X1+X2) 
l$plane3d(fit)

m<-with(post1, scatterplot3d(X1,X2,X3, main="3D Scatterplot"))
fit <- lm(X3 ~ X1+X2) 
m$plane3d(fit)

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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