This is an R tutorial to help beginners to make scatter plots using the Plotly Package.
First, let’s install the “Plotly” Package. If you have already installed the Plotly package you can skip the “install” step. Since we only want to install a package once, we can type the following commad in the R Console, instead of the R script.
install.packages("plotly")
After install the package, we can add the Plotly library. Libraries are usually added at the begging of the R scripts.
library(plotly)
For this tutorial, we will be using a buiilt-in R data: mtcars. Let’s load the data, and have a closer look to out dataset. If you want to see more lines from the dataset, you can replace the number 3 in the head function with the number of lines you want to see, or you can use the View function.
# Load dataset
data("mtcars")
# Examine the first 3 rows of the data
head(mtcars, 3)
## mpg cyl disp hp drat wt qsec vs am gear carb
## Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
## Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
## Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
If you want to see the full dataset, type:
View(mtcars)
We can get the information of this dataset using the code:
?mtcars
The mtcars dataset contains 32 observations and 11 features: 1. mpg Miles/(US) gallon 2. cyl Number of cylinders 3. disp Displacement (cu.in.) 4. hp Gross horsepower 5. drat Rear axle ratio 6. wt Weight (1000 lbs) 7. qsec 1/4 mile time 8. vs V/S 9. am Transmission (0 = automatic, 1 = manual) 10. gear Number of forward gears 11. carb Number of carburetors
In this example we will plot a scatter plot of miles per gallon versus displacement. Below, we set column mpg as x axis and column disp as y axis. We set the dots in the scatter of size 10 and color red. In the layout(), we set plot title and names of the x axis and y axis.
plot_ly(data = mtcars, x = ~mpg, y = ~disp, type = "scatter", mode = "markers", marker = list(size = 10, color = "red")) %>%
layout(title = "Miles per gallon versus displacement", xaxis = list(title = "Miles per gallon"),
yaxis = list(title = "Displacement"))
We can also add a colorbar “Miles per gallon” to the scatter plot and let the colors of the dots changed by the value of column cyl. We can set the colorscale of the colorbar as ‘Viridis’.
plot_ly(data = mtcars, x = ~mpg, y = ~disp, type = "scatter", mode = "markers", marker = list(size = 10, colorbar = list(title = "Number of cylinders"), color = ~cyl, colorscale='Viridis', reversescale =T)) %>%
layout(title = "Miles per gallon versus displacement", xaxis = list(title = "Miles per gallon"),
yaxis = list(title = "Displacement"))
Similarly we can let the size of dots depends on the weigt, the gear column.
plot_ly(data = mtcars, x = ~mpg, y = ~disp, type = "scatter", mode = "markers", marker = list(size = ~gear, colorbar = list(title = "Number of cylinders"), color = ~cyl, colorscale='Viridis', reversescale =T)) %>%
layout(title = "Miles per gallon versus displacement", xaxis = list(title = "Miles per gallon"),
yaxis = list(title = "Displacement"))
Then, we realizes these dots are too small, we can increase the size of the dots inscale by add “*4“:
plot_ly(data = mtcars, x = ~mpg, y = ~disp, type = "scatter", mode = "markers", marker = list(size = ~gear*4, colorbar = list(title = "Number of cylinders"), color = ~cyl, colorscale='Viridis', reversescale =T)) %>%
layout(title = "Miles per gallon versus displacement", xaxis = list(title = "Miles per gallon"),
yaxis = list(title = "Displacement"))