How to plot subplots using plotly in R?

How to plot subplots using plotly in R?

How to plot subplots using plotly in R?

This recipe helps you plot subplots using plotly in R


Recipe Objective

A line chart is a type of chart which showcases information as a sequence of data points also known as 'markers' which are connected by staright line. It is mainly used to plot the relationship or trend of a categorical variable with respect to a numerical variable. This type of chart could be one of the best example for subplotting. Any two graphs can be plotted side-by-side using subplot() function in Plotly. ​

In this recipe we are going to use Plotly package to plot the required line chart using Dual y axis. Plotly package provides an interface to the plotly javascript library allowing us to create interactive web-based graphics entrirely in R. Plots created by plotly works in multiple format such as: ​

  1. R Markdown Documents
  2. Shiny apps - deploying on the web
  3. Windows viewer

Plotly has been actively developed and supported by it's community. ​

This recipe demonstrates how to create two subplots (line charts) in R using plotly package ​

STEP 1: Loading required library and creating a dataframe

We will use an example of no of schools established in 2 states between 1970 and 2014

# Data manipulation package library(tidyverse) # plotly package for data visualisation install.packages("plotly") library(plotly) # years year = c('1970','1980', '1990', '2000', '2013', '2014') #no of schools in state1 corresponding to a particular year no_of_schools_state1 = c(15, 30, 60, 120, 240, 300) #no of schools in state2 corresponding to a particular year no_of_schools_state2 = c(55, 85, 200, 450, 600, 700) #creating a dataframe df = data.frame(year,no_of_schools_state1, no_of_schools_state2) df
year	no_of_schools_state1	no_of_schools_state2
1970	15			55
1980	30			85
1990	60			200
2000	120			450
2013	240			600
2014	300			700

STEP 2:Plotting 2 subplots (line charts) using Plotly

We use the plot_ly() and subplot() function to plot 2 line charts side-by-side.


  1. The %>% sign in the syntax earlier makes the code more readable and enables R to read further code without breaking it.
  2. We also use layout() function to give a title to the graph
# line chart 1 fig1 = plot_ly() %>% add_lines(data = df, x = ~year, y = ~no_of_schools_state1, name = "State1") #line chart 2 fig2 = plot_ly() %>% add_lines(data = df, x = ~year, y = ~no_of_schools_state2, name = "State2") # subplotting fig 1 and 2 fig = subplot(fig1, fig2) fig = fig %>% layout(title = 'Subplots using Plotly') embed_notebook(fig)

Relevant Projects

Machine Learning Project to Forecast Rossmann Store Sales
In this machine learning project you will work on creating a robust prediction model of Rossmann's daily sales using store, promotion, and competitor data.

Learn to prepare data for your next machine learning project
Text data requires special preparation before you can start using it for any machine learning project.In this ML project, you will learn about applying Machine Learning models to create classifiers and learn how to make sense of textual data.

Data Science Project in Python on BigMart Sales Prediction
The goal of this data science project is to build a predictive model and find out the sales of each product at a given Big Mart store.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Customer Churn Prediction Analysis using Ensemble Techniques
In this machine learning churn project, we implement a churn prediction model in python using ensemble techniques.

German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

Build a Music Recommendation Algorithm using KKBox's Dataset
Music Recommendation Project using Machine Learning - Use the KKBox dataset to predict the chances of a user listening to a song again after their very first noticeable listening event.