How to create a bar chart using plotly in R?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to create a bar chart using plotly in R?

How to create a bar chart using plotly in R?

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

0

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)

Relevant Projects

Loan Eligibility Prediction using Gradient Boosting Classifier
This data science in python project predicts if a loan should be given to an applicant or not. We predict if the customer is eligible for loan based on several factors like credit score and past history.

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

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

Data Science Project on Wine Quality Prediction in R
In this R data science project, we will explore wine dataset to assess red wine quality. The objective of this data science project is to explore which chemical properties will influence the quality of red wines.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

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.

Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

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