How to create a bar chart using plotly in R?

This recipe helps you create a bar chart using plotly in R

Recipe Objective

A bar chart is a type of graph which allows us to compare the unique categories of a categorical variable in a dataset. In this type of graph, data can be represented by vertical or horizontal bars. ​

In this recipe we are going to use Plotly package to plot the required bar chart. 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 plot a bar chart in R using plotly package. ​

STEP 1: Loading required library and dataset

We will use an example of Expenses made by a student

# Data manipulation package library(tidyverse) # Lattice package for data visualisation install.packages("plotly") library(plotly) # Type of expense type_of_expense = c('Rent', 'Grocery', 'Transport') # Amount Amount_USD = c(7000, 3500, 900) #creating a dataframe df = data.frame(type_of_expense,Amount_USD)

STEP 2: Plotting a bar plot using Plotly

We use the plot_ly() function to plot a bar plot.

Syntax: plot_ly( data = , x = , y = , type = "bar" )

Where:

  1. data = dataframe to be used
  2. x = categorical variable to be plotted on x-axis
  3. y = values corresponding to each categorical variable to be plotted on y-axis
  4. color = to change the color of the bar
  5. type = type of chart (in our case "bar")

Note:

  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
fig <- plot_ly(data = df, x = ~type_of_expense, y = ~Amount_USD, type = "bar", color = "orange") %>% layout(title = 'bar chart using Plotly') embed_notebook(fig)

What Users are saying..

profile image

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

Loan Eligibility Prediction in Python using H2O.ai
In this loan prediction project you will build predictive models in Python using H2O.ai to predict if an applicant is able to repay the loan or not.

Build an optimal End-to-End MLOps Pipeline and Deploy on GCP
Learn how to build and deploy an end-to-end optimal MLOps Pipeline for Loan Eligibility Prediction Model in Python on GCP

Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

Deep Learning Project for Time Series Forecasting in Python
Deep Learning for Time Series Forecasting in Python -A Hands-On Approach to Build Deep Learning Models (MLP, CNN, LSTM, and a Hybrid Model CNN-LSTM) on Time Series Data.

Locality Sensitive Hashing Python Code for Look-Alike Modelling
In this deep learning project, you will find similar images (lookalikes) using deep learning and locality sensitive hashing to find customers who are most likely to click on an ad.

Build an End-to-End AWS SageMaker Classification Model
MLOps on AWS SageMaker -Learn to Build an End-to-End Classification Model on SageMaker to predict a patient’s cause of death.

Recommender System Machine Learning Project for Beginners-2
Recommender System Machine Learning Project for Beginners Part 2- Learn how to build a recommender system for market basket analysis using association rule mining.

Recommender System Machine Learning Project for Beginners-1
Recommender System Machine Learning Project for Beginners - Learn how to design, implement and train a rule-based recommender system in Python

Time Series Forecasting Project-Building ARIMA Model in Python
Build a time series ARIMA model in Python to forecast the use of arrival rate density to support staffing decisions at call centres.

Forecasting Business KPI's with Tensorflow and Python
In this machine learning project, you will use the video clip of an IPL match played between CSK and RCB to forecast key performance indicators like the number of appearances of a brand logo, the frames, and the shortest and longest area percentage in the video.