How to create a stacked bar chart using plotly in R?

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

Recipe Objective

How to create a stacked bar chart using plotly in R? Bar charts are a graphical representation of a categorical or discrete type of data. The data is represented in the form of rectangular bars either horizontally or vertically on the graph. Stacked bar charts are an extension of a bar chart that represents the part of a whole type of data comparison of given categories. This recipe demonstrates an example of stacked bar chart in R.

Step 1 - Install the necessary package and library

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

Step 2 - Define a Vector

data <- data.frame(x_val = c(10,5,6,15,20,19), y_val = c(2,12,19,11,6,14), type = c("A","B","C","D","E","F")) data

 "Data :"
label1	label2	value
X	X1	10
X	X2	20
X	X3	30
Y	Y1	40
Y	Y2	50

Step 3 - Plot a stacked bar chart

Syntax for line chart using plotly - plot_ly(x, type, mode) where, x - input data type - bar mode - lines

figure <- plot_ly(data, x = ~type, y = ~x_val, type = 'bar', name = 'X') figure <- figure %>% add_trace(y = ~y_val, name = 'Y') figure <- figure %>% layout(yaxis = list(title = 'Count'), barmode = 'stack') figure

What Users are saying..

profile image

Savvy Sahai

Data Science Intern, Capgemini
linkedin profile url

As a student looking to break into the field of data engineering and data science, one can get really confused as to which path to take. Very few ways to do it are Google, YouTube, etc. I was one of... Read More

Relevant Projects

Personalized Medicine: Redefining Cancer Treatment
In this Personalized Medicine Machine Learning Project you will learn to classify genetic mutations on the basis of medical literature into 9 classes.

Build Portfolio Optimization Machine Learning Models in R
Machine Learning Project for Financial Risk Modelling and Portfolio Optimization with R- Build a machine learning model in R to develop a strategy for building a portfolio for maximized returns.

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.

Build a Autoregressive and Moving Average Time Series Model
In this time series project, you will learn to build Autoregressive and Moving Average Time Series Models to forecast future readings, optimize performance, and harness the power of predictive analytics for sensor data.

Time Series Classification Project for Elevator Failure Prediction
In this Time Series Project, you will predict the failure of elevators using IoT sensor data as a time series classification machine learning problem.

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.

Learn to Build an End-to-End Machine Learning Pipeline - Part 1
In this Machine Learning Project, you will learn how to build an end-to-end machine learning pipeline for predicting truck delays, addressing a major challenge in the logistics industry.

ML Model Deployment on AWS for Customer Churn Prediction
MLOps Project-Deploy Machine Learning Model to Production Python on AWS for Customer Churn Prediction

Build a Multi-Class Classification Model in Python on Saturn Cloud
In this machine learning classification project, you will build a multi-class classification model in Python on Saturn Cloud to predict the license status of a business.

Census Income Data Set Project-Predict Adult Census Income
Use the Adult Income dataset to predict whether income exceeds 50K yr based oncensus data.