3 Plots In R
3D surface plots. Given the Z height values on a (X,Y) grid, we can draw the perspective plots of this surface over the (X,Y) plane. There are many options available in R for this. We will learn about the persp function of the Graphics library and persp3D function of the plot3D library. Both these functions take almost similar set of parameters as arguments.
How to make interactive 3D scatter plots in R.
In this case, 1,1 corresponds to the first plot, 1,2 corresponds the the second plot, and 2,1:2 corresponds to the third plot. This example is taken with slight modification from here. If you want the bottom plot to be the same 'width' as the two above, you can tweak the margins for that plot. There are many functions in R programming for creating 3D plots. In this section, we will discuss on the persp function which can be used to create 3D surfaces in perspective view. This function mainly takes in three variables, x, y and z where x and y are vectors defining the location along x- and y-axis.
Building AI apps or dashboards in R? Deploy them to Dash Enterprise for hyper-scalability and pixel-perfect aesthetic.
10% of the Fortune 500 uses Dash Enterprise to productionize AI & data science apps.
Find out if your company is using Dash Enterprise
Install Dash Enterprise on Azure Install Dash Enterprise on AWS
Plotly is a free and open-source graphing library for R. We recommend you read our Getting Started guide for the latest installation or upgrade instructions, then move on to our Plotly Fundamentals tutorials or dive straight in to some Basic Charts tutorials.
Basic 3D Scatter Plot
3D Scatter Plot with Color Scaling
3D Bubble Plot
See https://plotly.com/r/reference/#scatter3d for more information and chart attribute options!
What About Dash?
Dash for R is an open-source framework for building analytical applications, with no Javascript required, and it is tightly integrated with the Plotly graphing library.
Learn about how to install Dash for R at https://dashr.plot.ly/installation.
Everywhere in this page that you see fig
, you can display the same figure in a Dash for R application by passing it to the figure
argument of the Graph
component from the built-in dashCoreComponents
package like this:
Scatter Plots
You learned from the Plot chapter that the plot()
function is used to plot numbers against each other.
A 'scatter plot' is a type of plot used to display the relationship between two numerical variables, and plots one dot for each observation.
It needs two vectors of same length, one for the x-axis (horizontal) and one for the y-axis (vertical):
Example
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)
plot(x, y)
3 Variable Plot In R
Result:
3 Plots In R T
Try it Yourself »The observation in the example above should show the result of 12 cars passing by.
That might not be clear for someone who sees the graph for the first time, so let's add a header and different labels to describe the scatter plot better:
Example
y <- c(99,86,87,88,111,103,87,94,78,77,85,86)
plot(x, y, main='Observation of Cars', xlab='Car age', ylab='Car speed')
Result:
Try it Yourself »To recap, the observation in the example above is the result of 12 cars passing by.
The x-axis shows how old the car is.
The y-axis shows the speed of the car when it passes.
Are there any relationships between the observations?
It seems that the newer the car, the faster it drives, but that could be a coincidence, after all we only registered 12 cars.
Compare Plots
In the example above, there seems to be a relationship between the car speed and age, but what if we plot the observations from another day as well? Will the scatter plot tell us something else?
To compare the plot with another plot, use the points()
function:
Example
Draw two plots on the same figure:
x1 <- c(5,7,8,7,2,2,9,4,11,12,9,6)
y1 <- c(99,86,87,88,111,103,87,94,78,77,85,86)
# day two, the age and speed of 15 cars:
x2 <- c(2,2,8,1,15,8,12,9,7,3,11,4,7,14,12)
y2 <- c(100,105,84,105,90,99,90,95,94,100,79,112,91,80,85)
plot(x1, y1, main='Observation of Cars', xlab='Car age', ylab='Car speed', col='red', cex=2)
points(x2, y2, col='blue', cex=2)
Result:
Try it Yourself »Line Plot In R
Note: To be able to see the difference of the comparison, you must assign different colors to the plots (by using the col
parameter). Red represents the values of day 1, while blue represents day 2. Note that we have also added the cex
parameter to increase the size of the dots.
Combine 3 Plots In R
Conclusion of observation: By comparing the two plots, I think it is safe to say that they both gives us the same conclusion: the newer the car, the faster it drives.