How to Draw a plotly Scatterplot in R (Example)

 

This article provides several examples of scatterplots in plotly using the R programming language.

 

 

Kirby White Researcher Statistician Programmer

Note: This article was created in collaboration with Kirby White. Kirby is an organizational effectiveness consultant and researcher, who is currently pursuing a Ph.D. at the Seattle Pacific University. You can read more about Kirby here!

 

Overview

Scatterplots (or xy-plots) are one of the most fundamental chart types.

They are used to spot relationships between two numeric variables. The intersection of each pair of values is depicted as a dot. Typically, dots higher and to the right have higher values than dots that are lower or to the left.

A third variable can be plotted in the size, color, or shape of each dot, typically to represent a categorical variable.

Example Data

We’ll use the iris dataset for this example, as it is already loaded in R.

If you have not already done so, install and load the plotly package using this code: install.packages("plotly") and library(plotly)

 

Let’s look at the relationship between lengths and widths of iris petals. At its simplest, you can create a scatterplot of this data in plotly with this code:

plot_ly(
  data = iris,
  x = ~Petal.Length,
  y = ~Petal.Width,
  type = "scatter",
  mode = "markers"
)


The plot_ly() function initialized the graphing library while data = iris indicates the data frame where the data is stored. x = ~Petal.Length and y = ~Petal.Width indicate the columns to plot on each axis.

 

Changing The Color or Shape of Each Dot

If we want to encode the species of each data point in the colors of each dot, we can add that with a single argument in our function.

plot_ly(
  data = iris,
  x = ~Petal.Length,
  y = ~Petal.Width,
  type = "scatter",
  mode = "markers",
  color = ~Species
)


When we want our graphics to be more accessible for those with atypical vision (e.g., colorblindness), we may want to change the shape of each object in addition to changing the color. We can map symbol to species:

plot_ly(
  data = iris,
  x = ~Petal.Length,
  y = ~Petal.Width,
  type = "scatter",
  mode = "markers",
  color = ~Species, 
  symbol = ~Species
)


 

Changing The Transparency and Size of Each Dot

If a scatterplot is getting too crowded, you may want to change the size of each dot. You can adjust the value of size as needed:

plot_ly(
  data = iris,
  x = ~Petal.Length,
  y = ~Petal.Width,
  type = "scatter",
  mode = "markers",
  color = ~Species, 
  symbol = ~Species,
  size = 3
)


Alternatively, if you want larger values to be represented with a larger dot, you can map size to a field:

plot_ly(
  data = iris,
  x = ~Petal.Length,
  y = ~Petal.Width,
  type = "scatter",
  mode = "markers",
  color = ~Species, 
  symbol = ~Species,
  size = ~Petal.Length
)


Another strategy for dealing with crowded scatterplots is to adjust the transparency of each dot. This lets areas with more points appear darker than areas with few dots. You can change the alpha argument from 0 (completely transparent) to 1 (completely opaque).

plot_ly(
  data = iris,
  x = ~Petal.Length,
  y = ~Petal.Width,
  type = "scatter",
  mode = "markers",
  color = ~Species, 
  symbol = ~Species,
  size = 3,
  alpha = .3
)


 

Adding Text to the Popup

We can add the species name to the popup with another simple addition:

plot_ly(
  data = iris,
  x = ~Petal.Length,
  y = ~Petal.Width,
  type = "scatter",
  mode = "markers",
  color = ~Species, 
  symbol = ~Species,
  size = 3,
  alpha = .65,
  text = ~Species
)


 

Video, Further Resources & Summary

Check out this video for a tutorial of building these scatterplots in plotly:

 

The YouTube video will be added soon.

 

You can check out these other articles for more detailed examples of these popular charts in plotly:

In summary: This plot tutorial has explained how to create an interactive xy-plot using the plotly package in R. In case you have any comments or questions, let us know in the comments section below.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top