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

# How to create a pie chart using plotly in R?

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

0

## 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) ```

#### 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.

##### 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.

##### Expedia Hotel Recommendations Data Science Project
In this data science project, you will contextualize customer data and predict the likelihood a customer will stay at 100 different hotel groups.

##### Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

##### 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.

##### Time Series Forecasting with LSTM Neural Network Python
Deep Learning Project- Learn to apply deep learning paradigm to forecast univariate time series data.

##### Build a Similar Images Finder with Python, Keras, and Tensorflow
Build your own image similarity application using Python to search and find images of products that are similar to any given product. You will implement the K-Nearest Neighbor algorithm to find products with maximum similarity.

##### 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.

##### Natural language processing Chatbot application using NLTK for text classification
In this NLP AI application, we build the core conversational engine for a chatbot. We use the popular NLTK text classification library to achieve this.