3
$\begingroup$

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)

(source: http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_addat.html)

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

$\endgroup$
2
  • 1
    $\begingroup$ 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 $\endgroup$ – Kasra Manshaei Jun 4 '15 at 19:38
  • $\begingroup$ 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. $\endgroup$ – KMP Jun 4 '15 at 20:25
1
$\begingroup$

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?
  add_markers() %>%
  layout(scene = list(xaxis = list(title = 'eat'),
                     yaxis = list(title = 'pork'),
                     zaxis = list(title = 'chops')))
htmlwidgets::saveWidget(as_widget(p), "index.html")

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.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.