Join Barton Poulson for an in-depth discussion in this video Creating 3D scatter plots, part of R Statistics Essential Training.
The last variation of the scatter plot that we're going to look at when you have multiple quantitative variables, is the 3D scatter plot. We're going to again use the iris data. Load that into the work space. Take a look at the first five rows. First we're going to produce a static 3D scatter plot. Now for this we need to use a special package. We're going to use one called scatterplot3d package. That makes sense. If you don't have this installed already, run this line, install.packages scatterplot3d.
That'll take a moment. It'll download it, and then we need to load it. To make it available by using require or library scatterplot3d. And then we're going to produce the most basic scatterplot. Right now I'm just going to choose three variables, the first three, with the function scatterplot3d, and then iris is the data set. And here we go. Let me zoom in on that one. So it puts it in a box to highlight the axes for the x, y and z dimensions. Now the problem of course with the 3D scatter plot as well as it's really kind of a cool thing, because it's a 2D surface it's nearly impossible to place any of these things because the circles are all drawn the same size regardless of where they are.
There's really no perspective. We got an orthographic projection here. But it's really hard to place them. That being said, I'm still going to go ahead and do a modified version of the 3D scatter plot. What I'm going to do, I'm going to add coloring and vertical lines that connect each of these to the floor of the scatter plot. That gives you some way of checking where they are. Also, it's something called a regression plane. That's all kind of an interesting thing, so first what we're going to do is I'm going to produce this one called s3d, I need to save it as an object so I can put the plane in it.
And so I'm going to do scatter plot 3D, as so you do in iris, now I'm changing the points using pch changing them from Empty circles to solid circles, I'm going to be highlighting the 3D, I'm going to be filling things in and drawing the height down to the bottom and putting the title on the top. So let me do that right now. Okay, it's pretty busy and then let's come here. And let's calculate the plane. So we're going to create an object called plane by using a linear model.
There's going to predict petal length on the basis of sepol length and sepol width. So if I run that one, we now plane loading up on the work space on the top, right? And now I can add the plane. To the chart by using this function, plane 3D. Alright. Let's zoom in on this. Okay. Well, we certainly have a whole lot more stuff there. Unfortunately, I find this to be a very confusing chart. That's one of the reasons I'm usually a fan of 3D scatter plots as they have a lot of stuff. It's kind of impressive but it actually is very hard to read and tell what's going on.
(SOUND) Now one way around this is to actually produce an interactive or dynamic spinning 3D scatter block, this actually makes it much easier to see the pattern because you can move it around and see where things are located in space. Now here's the deal, this is important, you need to use an RGL package that's called 3D visualization devices I'm using open GL. But it is not compatible with Rstudio. It will cause Rstudio to crash when you close the window. Instead, we need to go to the standard console version of R and run it from there.
I've already loaded things over there so I'm going to skip over to R and there we have it. If you don't have RGL installed already, you'll need to do that. Once you've installed that package, we need to load it. I'm also going to load the package R color brewer. And then what I do is I'm going to highlight this entire block of code. So here I'm using the function of plot 3D from RGL. I explicitly define the x, y and z variables. They actually are just by the position that they come in this.
I'm putting labels on each of the three and I'm going to be coloring the dots by the species that they are using in Colorbrewers dark two palette. Then making the dots a lot larger, going to make 'em up to eight pixels. And when I run that it's going to open up a separate window here. I'm going to maximize this window. And right now it's not a big improvement, but the difference is I can come in here with a cursor and I can move it around. And it makes it a lot easier to see for instance this is sort of maximum separation of the groups here.
And so this can be a very handy thing, of course it's obviously still a 2D display, because we've got a flat screen and we're not using stereoscopic vision, but by moving around you get a good resemblance to 3D units easy to tell where things go. But that is how you can make a 3D display. Now, I'm going to leave R and go back to Rstudio here. There's all that code. And anyhow. That's where we're going to finish. So this spinning 3D displays are a fascinating thing. They're interesting. They're visually attractive but, on the other hand, I find them to be hard to read.
And that's where I actually prefer doing a scatter plot and matrix. Doing two at a time I find it much easier to see the patterns in there. So I'd actually step back one of the movies and use that one instead. But I wanted you to be aware of the possibility of a 3D scatter plot matrix. Right now I'm going to go through and I'm going to do a little bit of detaching. Just cleaning up shop here. And that's the end of that chapter.
- Installing R on your computer
- Using the built-in datasets
- Importing data
- Creating bar and pie charts for categorical variables
- Creating histograms and box plots for quantitative variables
- Calculating frequencies and descriptives
- Transforming variables
- Coding missing data
- Analyzing by subgroups
- Creating charts for associations
- Calculating correlations
- Creating charts and statistics for three or more variables
- Creating crosstabs for categorical variables