[box type=”note” align=”alignright” class=”” width=””] The article is originally extracted from our book R Data Analysis Cookbook – Second Edition by Kuntal Ganguly. Data analytics with R has emerged to be a very important focus for organizations of all kind. The book will show how you can put your data analysis skills in R to practical use. It contains recipes catering to basic as well as advanced data analysis tasks. [/box]
In this article, we have captured one aspect of using R for the creation of 3D graphics and animation. When, a two-dimensional view is not sufficient to understand and analyze the data; besides the x, y variable, an additional data dimension can be represented by a color variable. Our article gives a step by step explanation on how to plot 3D package in R is used to visualize three-dimensional graphs. Codes and data files are readily available for download towards the end of the post.
Make sure you have downloaded the code and the mtcars.csv file is located in the working directory of R. Install and load the latest version of the plot3D package:
> install.packages("plot3D")
> library(plot3D)
> mtcars=read.csv("mtcars.csv")
> rownames(mtcars) <- mtcars$X
> mtcars$X=NULL
> head(mtcars)
2. Next, create a three-dimensional scatter plot:
> scatter3D(x=mtcars$wt, y=mtcars$disp, z=mtcars$mpg, clab =
c("Miles/(US) gallon"))
3. Now, add title and axis labels to the scatter plot:
> scatter3D(x=mtcars$wt, y=mtcars$disp, z=mtcars$mpg, pch = 18,
theta = 20, phi = 20, main = "Motor Trend Car Road Tests", xlab =
"Weight lbs",ylab ="Displacement (cu.in.)", zlab = "Miles gallon")
Rotating a 3D plot can provide a complete view of the data. Now view the plot in different directions by altering the values of two attributes–theta and phi:
> scatter3D(x=mtcars$wt, y=mtcars$disp, z=mtcars$mpg,clab = c("Cars
Mileage"),theta = 15, phi = 0, bty ="g")
The scatter3D() function from the plot3D package has the following parameters:
There’s more…
The following concepts are very important for this recipe.
We can use the text3D() function to add text based on the car model name, alongside the data points:
> scatter3D(x=mtcars$wt, y=mtcars$disp, z=mtcars$mpg, phi = 0, bty = "g",
pch = 20, cex = 0.5)
> text3D(x=mtcars$wt, y=mtcars$disp, z=mtcars$mpg, labels =
rownames(mtcars), add = TRUE, colkey = FALSE, cex = 0.5)
The three-dimensional histogram function, hist3D(), has the following attributes:
Let’s plot the Death rate of Virgina using a 3D histogram:
data(VADeaths)
hist3D(z = VADeaths, scale = FALSE, expand = 0.01, bty = "g", phi = 20, col
= "#0085C2", border = "black", shade = 0.2, ltheta = 80, space = 0.3,
ticktype = "detailed", d = 2)
To visualize the plot with a line graph, add the type parameter to the scatter3D() function. The type parameter can take the values l(only line), b(both line and point), and h(horizontal line and points both).
The 3D plot with a horizontal line and plot:
> scatter3D(x=mtcars$wt, y=mtcars$disp, z=mtcars$mpg,type="h", clab =
c("Miles/(US) gallon"))
[box type=”download” align=”” class=”” width=””]Download code files here. [/box]
If you think the recipe is delectable, you should definitely take a look at R data Analysis Cookbook- Second edition which will enlighten you more on data visualizations with R.
I remember deciding to pursue my first IT certification, the CompTIA A+. I had signed…
Key takeaways The transformer architecture has proved to be revolutionary in outperforming the classical RNN…
Once we learn how to deploy an Ubuntu server, how to manage users, and how…
Key-takeaways: Clean code isn’t just a nice thing to have or a luxury in software projects; it's a necessity. If we…
While developing a web application, or setting dynamic pages and meta tags we need to deal with…
Software architecture is one of the most discussed topics in the software industry today, and…
View Comments
hello.
my name is maryam my thesis master about graphics and animation in R, Do you have any good books, or articles about this topic?Im thankful for everything :)
can you help me?😢