3D plots and grid.arrange in R

1k views Asked by At

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?

1

There are 1 answers

2
emilliman5 On BEST ANSWER

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)

enter image description here