How to Draw a plotly Line Plot in R (Example)

 

This article provides several examples of line plots using the plotly package in R.

 

 

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

Line plots (or line charts; curve charts) are one of the most fundamental chart types.

They are typically used to represent changes over time or repeated measurement. Typically, values that are farther left on the plot are earlier in the time series, and values that are plotted higher on the graph have larger values.

At its simplest, a line plot is just a scatterplot that has a line drawn between each dot, so line plots will always have type = "scatter" in the plotly code.

 

Example Data

To illustrate the primary features of line plots in plotly, we’ll create two synthetic data sets. You may need to install and load the tidyr package to transform df_wide into df_long

df_wide <- data.frame(date = c("2020-01-01", "2020-04-01", "2020-07-01", "2020-10-01"),
                      VarA = c(15,20,25,18),
                      VarB = c(22,24,26,28),
                      VarC = c(5,10,20,40))
 
df_long <- df_wide %>% tidyr::pivot_longer(cols = c(VarA, VarB,VarC))

These data frames have identical information, but are structured differently in order to show examples that work in a variety of settings.

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

 

Creating a Line Plot

This code demonstrates how to plot the values of VarA over time:

plot_ly(
  data = df_wide,
  x = ~date, 
  y = ~VarA,
  type = "scatter",
  mode = "lines"
)


To add the other variables from the wide data set, we need to render multiple graphs and stack them on top of each other. We’ll also add the name argument so that legend properly labels them:

plot_ly(
  data = df_wide,
  x = ~date, 
  y = ~VarA,
  name = "VarA",
  type = "scatter",
  mode = "lines"
) %>% 
add_trace(y = ~VarB, name = "VarB")%>% 
add_trace(y = ~VarC, name = "VarC")


The same graph is easier to create with the long data set, since we can simply map the color to the name column:

plot_ly(
  data = df_long,
  x = ~date, 
  y = ~value,
  color = ~name,
  type = "scatter",
  mode = "lines"
)


We’ll mostly use df_long in this tutorial, since this data structure is easier to work with.

 

Line and Marker Options

There are three modes for plotting this information:

  • mode = "lines" plots the lines, but not the dots
  • mode = "lines+markers" plots the lines and dots
  • mode = "markers" plots the dots, but not the lines

Here’s an example for plotting the lines and markers together:

plot_ly(
  data = df_long,
  x = ~date, 
  y = ~value,
  color = ~name,
  type = "scatter",
  mode = "lines+markers"
)


 

Line Options

You can alter several properties of the lines that are being plotted. In plotly, these options are typically stored in a list and passed to the line argument in this format: line = list(...)

Here is an example to increase the width of the lines:

plot_ly(
  data = df_long,
  x = ~date, 
  y = ~value,
  color = ~name,
  type = "scatter",
  mode = "lines+markers",
  line = list(width = 4)
)


We can also make the lines dashed (or dotted):

plot_ly(
  data = df_long,
  x = ~date, 
  y = ~value,
  color = ~name,
  type = "scatter",
  mode = "lines+markers",
  line = list(width = 4, dash = "dot")
)


If you need each line to have different properties, the data structure of df_wide makes this easier. Here is an example where the properties are different for each line being plotted:

plot_ly(
  data = df_wide,
  x = ~date,
  y = ~VarA, 
  name = "VarA",
  type = "scatter",
  mode = "lines",
  line = list(width = 2, 
              dash = "solid",
              color = "green")) %>%
  add_trace(y = ~VarB, name = "VarB", mode = "lines", line = list(width = 4, dash = "dash", color = "gray"))%>% 
  add_trace(y = ~VarC, name = "VarC", mode = "lines+markers", line = list(dash = "dot", color = "red"))


 

Unified Hover Info

When you have multiple lines plotted on the same graphic, it can sometimes be easier to compare all y-values at each value of x.

We can modify this by introducing the layout configurations in plotly, and setting hovermode = "x unified":

plot_ly(
  data = df_long,
  x = ~date, 
  y = ~value,
  color = ~name,
  type = "scatter",
  mode = "lines+markers",
  line = list(width = 4, dash = "dot")) %>% 
  layout(hovermode = "x unified")


 

Video, Further Resources & Summary

Check out this video for a tutorial of building these line plots 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:

This page has explained how to create an interactive line chart using the plotly package in R. Please leave a comment below, in case you have any further questions.

 

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