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:





(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?

  • 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$ Jun 4, 2015 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, 2015 at 20:25

1 Answer 1


You can use the plot3Drgl library. E.g.

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))

Or you can use 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))
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.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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