Open In App

Multiline Plot using Plotly in R

Last Updated : 22 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

A multiline plot is used to visualize the relationship between several continuous variables on one graph. In R, the plotly package supports creating interactive and efficient multiline plots. Unlike ggplot2, which can be slower, plotly provides faster rendering and better interactivity. This article focuses on creating and customizing multiline plots using plotly in R.

Syntax:

plot_ly(df,type,marker,labels,values) %>% layout() %>% add_trace()

Where:

  • df : data frame
  • type : used to specify the type of plot we want to visualize
  • x,y: the variables of the dataset which need to be plotted along x,y axis are specified respectively
  • layout(): this function is used to change the layout as required (like assigning a title to the plot)
  • add_trace(): this function is used to append similar new traces to existing dimension

Steps to Create a Multline Plot using Plotly

Step 1: Install and load the required package

Before you begin, make sure you have plotly installed and loaded into your R environment:

R
install.packages("plotly")
library(plotly)

Step 2: Create or Import the Data Frame

we’ll create a sample data frame that holds the marks of students in various sections

R
df=data.frame(
  Roll_number=c(1,2,3,4,5,6,7,8,9,10),
  Marks_Section1=c(85,84,76,88,67,81,91,85,92,88),
  Marks_Section2=c(88,85,80,98,76,86,73,97,90,82),
  Marks_Section3=c(80,74,76,87,80,82,77,84,74,58)
)

Step 3: Plot the Multiline Plot

Now, we can use plotly function to generate a multiline graph. Marks in each section are plotted as independent lines (traces)

R
fig <-plotly::plot_ly(data = df,x = ~Roll_number,
                      y = ~Marks_Section1,name = "Section 1",
                      type = "scatter",mode = "lines") %>%
  add_trace(y = ~Marks_Section2, name = "Section 2") %>% 
  add_trace(y = ~Marks_Section3, name = "Section 3")
fig

Output:

multiline_plot

Multiline Plot using Plotly in R

Customized Multiline Plot using Plotly

The Multiline plot using the Plotly package can be customized to make it more attractive and informative. We can change its colors, modes, line style, add hovers, etc.

Example: Customizing the width, mode, and color of the MultiLine plot

We can customize the width, mode, and color of our line of the Multiline Plot in R. The below code shows how can we customize it.

R
library(plotly)

df=data.frame(
  Roll_number=c(1,2,3,4,5,6,7,8,9,10),
  Marks_Section1=c(85,84,76,88,67,81,91,85,92,88),
  Marks_Section2=c(88,85,80,98,76,86,73,97,90,82),
  Marks_Section3=c(80,74,76,87,80,82,77,84,74,58)
)

fig <-plotly::plot_ly(data = df,x = ~Roll_number, 
                      y = ~Marks_Section1,name = "Section 1",
                      type = "scatter",mode = "lines+markers",
                      line=list(width=6,sash='dot', color="yellow")) %>%
  add_trace(y = ~Marks_Section2, name = "Section 2",mode = "lines", 
            line = list(width = 5, dash = "dot", color="red")) %>% 
  add_trace(y = ~Marks_Section3, name = "Section 3",mode = "lines",
            line = list(width = 4, dash = "dash", color="green")) %>%
  layout(title="Customized Multiline Plot using Plotly")
fig

Output:

multiline_plot

Customizing the Multiline Plot

Explanation:

  • plot_ly(): Draws a scatter plot of Roll_number on the x-axis and Marks_Section1 on the y-axis. Alters “Section 1” using dashed yellow lines (width 6).
  • Adding trace of “Section 3”: Changes Marks_Section3 to be on the y-axis with dashed green lines (width 4).
  • layout(): Specifies plot title as “Customized Multiline Plot Using Plotly”.

Adding Unified Hover to the Plot

We can also add a unified hover to our Multiline Plot using Plotly in R. Follow the below code to see how can we add a unified hover.

R
library(plotly)

df=data.frame(
  Roll_number=c(1,2,3,4,5,6,7,8,9,10),
  Marks_Section1=c(85,84,76,88,67,81,91,85,92,88),
  Marks_Section2=c(88,85,80,98,76,86,73,97,90,82),
  Marks_Section3=c(80,74,76,87,80,82,77,84,74,58)
)

fig <-plotly::plot_ly(data = df,x = ~Roll_number,
      y = ~Marks_Section1,name = "Section 1",
      type = "scatter",mode = "lines+markers",
      line=list(width=6,sash='dot', color="yellow")) %>%
  add_trace(y = ~Marks_Section2, name = "Section 2",mode = "lines",
            line = list(width = 5, dash = "dot", color="red")) %>%
  add_trace(y = ~Marks_Section3, name = "Section 3",mode = "lines",
            line = list(width = 4, dash = "dash", color="green")) %>%
  layout(title="Customized Multiline Plot using Plotly",hovermode = "x unified")
fig

Output:

multiline_plot

Adding unified hover to the Multiline Plot

Explanation:

  • Creates scatter plot: Roll_number as the x-axis and Marks_Section1 as the y-axis.
  • Trace label: Labeled as “Section 1”.
  • Mode: Shows both lines and markers (mode = “lines+markers”).

Related Articles:



Next Article
Article Tags :

Similar Reads