# Plotting multiple datasets on a 3D plot

I want to make a 3D scatter plot of multiple data selections on a single plot (i.e same axes).

I know that in 2D this is possible by using par() function like so:

plot(6:25,rnorm(20),type="b",xlim=c(1,30),ylim=c(-2.5,2.5),col=2)

par(new=T)

plot(rnorm(30),type="b",axes=F,col=3)

par(new=F)

Can I do something like that on a 3D plot, preferably an interactive 3D plot, like the ones created using plot3D from 'rgl' package?

• Welcome to the community :) As there are "r" and "data visualization" tags this question is not irrelevant but maybe stackoverflow is a better place to get a helpful answer Jun 4 '15 at 19:38
• I found one solution to the problem here: <br/> stackoverflow.com/questions/19731187/… <br/> which requires creating a data frame before doing this. I was wondering if there is a simpler, quicker way of doing it.
– KMP
Jun 4 '15 at 20:25

You can use the plot3Drgl library. E.g.

library("plot3Drgl")
x <- c(1,2,3,4,5)
y <- c(6,7,8,9,10)
z <- c(11,12,13,14,15)
colors <- c(rep("red",3), rep("blue",2))
plot3d(x=x,y=y,z=z,col=colors)


Or you can use plotly:

library("plotly")
x <- c(1,2,3,4,5)
y <- c(6,7,8,9,10)
z <- c(11,12,13,14,15)
colors <- c(rep("red",3), rep("blue",2))
df<-as.data.frame(cbind(x,y,z,colors))
colnames(df)<-c("eat", "pork", "chops", "colors")

p <- plot_ly(data=df, x=~eat, y=~pork, z=~chops, color =~colors, colors=c('#BF382A', '#0C4B8E')) %>% ## Like a pipe?

This saves the output as an html file. In order to get this to work, you'll also need to install pandoc. On CentOS/RedHat do yum install pandoc pandoc-citeproc. On Mac OSX, using homebrew, do brew install pandoc.