How to create a pie chart using plotly in R?

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

Recipe Objective

A pie-chart is pictorial representation of categorical variable and it's numerical constituent in the form of a circle. The circle is divided into sectors that indicates a certain proportion of the whole. We prefer to use pie chart when there are less than 5 categories that needs to be compared. ​

In this recipe we are going to use Plotly package to plot the required pie-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 pie-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) # plotly 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)

Observations: 200
Variables: 3
$ Age                     19, 21, 20, 23, 31, 22, 35, 23, 64, 30, 67, 35…
$ Annual.Income..k..      15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 19, 19…
$ Spending.Score..1.100.  39, 81, 6, 77, 40, 76, 6, 94, 3, 72, 14, 99, 1…

STEP 2: Plotting a pie chart using Plotly

We use the plot_ly() function to plot a pie chart.

Syntax: plot_ly( data = , labels = , values = , type = "pie", textinfo = "label+percent", insidetextorientation = "radial" )

Where:

  1. data = dataframe to be used
  2. labels = unique names of the categorical variable
  3. values = Corresponding values of the categories
  4. type = type of chart (in our case "pie")
  5. textinfo = information to be appeared on the section as labels
  6. insidetextorientation = indicates the alignment of the text

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, labels = ~type_of_expense, values = ~Amount_USD, type = "pie", textinfo = "label+percent", insidetextorientation = "radial") %>% layout(title = 'Pie Chart using Plotly') embed_notebook(fig) ​

What Users are saying..

profile image

Ed Godalle

Director Data Analytics at EY / EY Tech
linkedin profile url

I am the Director of Data Analytics with over 10+ years of IT experience. I have a background in SQL, Python, and Big Data working with Accenture, IBM, and Infosys. I am looking to enhance my skills... Read More

Relevant Projects

Skip Gram Model Python Implementation for Word Embeddings
Skip-Gram Model word2vec Example -Learn how to implement the skip gram algorithm in NLP for word embeddings on a set of documents.

Demand prediction of driver availability using multistep time series analysis
In this supervised learning machine learning project, you will predict the availability of a driver in a specific area by using multi step time series analysis.

BERT Text Classification using DistilBERT and ALBERT Models
This Project Explains how to perform Text Classification using ALBERT and DistilBERT

Build a Medical AI Assistant using Unsloth and QLoRA
In this AI Project, you will learn to fine-tune the LLaMA 3.1 8B model using Unsloth and QLoRA to build a domain-specific medical AI assistant capable of accurate, context-aware, and memory-efficient clinical conversations. It also integrates a Streamlit chatbot interface for real-time interaction and deployment.

BigMart Sales Prediction ML Project in Python
The goal of the BigMart Sales Prediction ML project is to build and evaluate different predictive models and determine the sales of each product at a store.

Build a Graph Based Recommendation System in Python-Part 2
In this Graph Based Recommender System Project, you will build a recommender system project for eCommerce platforms and learn to use FAISS for efficient similarity search.

OpenCV Project to Master Advanced Computer Vision Concepts
In this OpenCV project, you will learn to implement advanced computer vision concepts and algorithms in OpenCV library using Python.

Build Classification Algorithms for Digital Transformation[Banking]
Implement a machine learning approach using various classification techniques in Python to examine the digitalisation process of bank customers.

Linear Regression Model Project in Python for Beginners Part 1
Machine Learning Linear Regression Project in Python to build a simple linear regression model and master the fundamentals of regression for beginners.

AWS Project to Build and Deploy LSTM Model with Sagemaker
In this AWS Sagemaker Project, you will learn to build a LSTM model on Sagemaker for sales forecasting while analyzing the impact of weather conditions on Sales.