Open In App

Multiline Plot using Plotly in R

Last Updated : 13 Jun, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

In general, Multiline plots are plotted when one wants to visualize one or more linear variables on the same plot. In R programming this can be implemented using ggplot2 and plotly but in this article, we are going to focus on the plotly package because it produces comparatively faster and more efficient graphs than the ggplot2 package.

Multiline plots, often called line plots or line charts, are a form of graph used to show the relationship between several continuous variables across one common axis, usually time or another continuous variable.

The idea behind a multiline plot is to display each variable as a line on the graph, with the common variable being represented by the x-axis and the variable values being represented by the y-axis. Each line displays the trend or pattern of the relevant variable throughout the designated x-axis range.

Steps to plot Multline Plot using Plotly in R

Step 1. Install and load the required package

R




# install and load required packages
install.packages("plotly")
library(plotly)


Step 2. Create or import the data frame you want to plot. In this article, we are going to create a data frame that consists of marks of different section students as follows:

R




# Creation of sample data frame
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. Using plotly function we are going to plot a Multiline plot by adding multiple lines as traces to the line plot as follows:

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

R




# Multiline plot using plotly
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 using Plotly in RGeeksforgeeks

Multiline Plot using Plotly in R

Customized Multiline Plot using Plotly in R

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.

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




# Loading the required package
library(plotly)
 
#creation of sample dataframe
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)
)
 
# Customized Multi Line plot using R
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:

Customizing the Multiline Plot

Customizing the Multiline Plot

  • Plot_ly(data = df, x = Roll_number, y = Marks_Section1, name = “Section 1”, type = “scatter”, mode = “lines+markers”, line = list(width = 6, dash = ‘dot’, colour = “yellow”)).
     
  •  By indicating the input data frame df, this line generates the basic plot. It sets the Roll_number column as the x-axis value and the Marks_Section1 column as the y-axis value.
     
  • This trace is given the label “Section 1” by the name parameter. A scatter plot is produced by setting the type parameter to “scatter” and the mode parameter to “lines+markers” to display both lines and markers.
     
  • The line argument alters the way the line looks by setting its width to 6, dash to ‘dot’, and color to ‘yellow’.
     
  • The next function call is chained together using the pipe operator (%>%).
     
  • y = “Marks_Section3”, name = “Section 3”, mode = “lines”, line = list(width = 4, dash = “dash”, color = “green”) Another trace is added to the plot by this line. It assigns the label “Section 3” to this trace, sets the y-axis values to the Marks_Section3 column, and selects “lines” as the mode. By setting the width to 4, dash to “dash,” and color to “green,” the line parameter allows for complete customization of the line’s look.

layout(title = “Customised Multiline Plot Using Plotly”): This line uses the layout() function to change the plot’s title to “Customised Multiline Plot Using Plotly.”
 

Adding Unified Hover to the Multiline 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




# Loading the required package
library(plotly)
 
#creation of sample dataframe
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)
)
 
# Customized Multi Line plot using R
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:

Adding unified hover to the Multiline Plot

Adding unified hover to the Multiline Plot

  •  By indicating the input data frame df, this line generates the basic plot. It sets the Roll_number column as the x-axis value and the Marks_Section1 column as the y-axis value.
     
  • This trace is given the label “Section 1” by the name parameter. A scatter plot is produced by setting the type parameter to “scatter” and the mode parameter to “lines+markers” to display both lines and markers.


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads