R Programming Series: 3D Visualization in R

3D- Visualization

In the last blog, we have learned how to create Dynamic Maps Using ggplot2. In this article, we will explore more into the 3D visualization in R programming language by using the plot3d package.

The plot3d package can be used to generate stunning 3-D plots in R. It can generate an interesting array of plots, but in this recipe, we will focus on creating 3-D scatterplots. These arise in situations where we have three variables, and we want to plot the triplets of values on the xyz space.

We will use a specific dataset to plot them into fancy plots using the plot3d package. The following steps are implemented to create 3D visualization in R.

Step 1: Install the required packages which are needed for 3D visualization in R.

Include the required libraries in the mentioned workspace.

Step 2: We will use the dataset named “income.csv” which includes all the necessary parameters which are needed for understanding income rates of every employee.

Dataset on Income - 1

Step 3: Analyze the data structure of the dataset with the mentioned attributes. 

Step 4: It is important to understand the five-point summary of data before proceeding further. Visualization requires a lookout on bivariate and univariate analysis which is clearly understood with a five-point summary of data.

Step 5: Let us with bivariate analysis which focusses on 2-dimensional data and scatter plot is considered as an easy method to create the same.

Here, we are plotting “Education” in the x-axis, “Income” in the y-axis and the “Seniority” level in the z-axis. The distinct legends are created based on the range of income parameters. The plot is helpful to show the relationship between Income, Education, and Society.

Income, education, seniority plot - 2

We can add more effects to the mentioned 3D plot with plane surfaces and ranges depicted in a specific order. Consider that we want to implement a linear regression model to establish the relationship between Seniority, Education and Income we can create a predictive model for the same.

Step 6: Create a predictive model with the help of the RGL package. RGL is the 3D real-time rendering package in the R programming language. It provides high-level functions to create an interactive graph. To create a 3D plot of linear regression, we need to create a predictive model of the same.

Step 7: Once the required parameters for linear regression are taken into consideration, we can create an interactive graph where we plot the data points as a scattered graph and later embed the linear regression model in them.

Income, education, seniority 3-d plot--- 3

Now we will embed the linear regression line as mentioned below:

embedding linera regression in 3d plot - 4

Step 8: We can create a surface plot that defines the volume and intensity of the data. Following steps are implemented to create a specific plot as mentioned below:

Here, we convert the dataset into a separate vector and we also created the index based on the names of candidates. Text3d is the function that adds text to the plane surface. The text represents actual data representation. As we converted the row names with names of candidates, it becomes easy to display text in the 3D plot.

surface plot - 5

Step 9: We can convert the values in proper labels with distinct colors. This helps to visualize the 3D plot more easily and distinctly with a specific color range. 

Contour Plots

Contour plots visually represent the intensity of the plot. The color and graphical representation help in the visual analysis of data. 

The output of the contour plot is mentioned below:

Contour plot - 7

The interactive 3d plot is mentioned below:

Interactive 3-d plot - 8

We can also create a plot with an animation feature which increases the interactive rate. For this, it is important to install an “animation” package which helps in creating the plots as desired.

The ranges of the graphs are depicted below:

Animation graph - 9

Animation graph II - 10

The file is saved with .html extension and represents the graphical animation of the values in three co-ordinates.

So, this was all about 3D visualization in R programming!

In the next section, we will be going to learn about Data Wrangling and Visualization in R programming language.


Please enter your comment!
Please enter your name here