How to plot pie chart using ggplot in R?

This recipe helps you plot pie chart using ggplot in R

Recipe Objective

How to plot a pie chart using ggplot in R? A pie chart is a circular garph, that represents data. It is a pictorial representation of the data. In a pie chart,the chart is divided into slices, where each slice is a proportional part of the whole data. The pie chart is a very useful tool for data analysis. ggplot is a package in R that helps in data visualization. This recipe demonstrates an example of pie charts using the ggplot.

Step 1 - Install necessary libraries

library("ggplot2") library("dplyr")

Step 1 - Define a dataframe

Syntax for pie chart plots using ggplot is- ggplot (data, aes (x=,y=)+geom_bar () where, data — the required data to be plotted in a pie chart aes (x=,y=)) — the aes function — creates mapping from data to geom geom_line — the geometric object to be drawn.

data <- data.frame("category" = c('A', 'B', 'C', 'D','E'), "values" = c(10, 20, 30, 40, 50)) print(data)

Step 2 - Plot the pie chart

ggplot(data, aes(x="", y=values, fill=category)) + geom_bar(stat="identity", width=1) + coord_polar("y", start=0)

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

Hands-On Approach to Regression Discontinuity Design Python
In this machine learning project, you will learn to implement Regression Discontinuity Design Example in Python to determine the effect of age on Mortality Rate in Python.

Learn to Build a Neural network from Scratch using NumPy
In this deep learning project, you will learn to build a neural network from scratch using NumPy

Build a Credit Default Risk Prediction Model with LightGBM
In this Machine Learning Project, you will build a classification model for default prediction with LightGBM.

Learn How to Build a Linear Regression Model in PyTorch
In this Machine Learning Project, you will learn how to build a simple linear regression model in PyTorch to predict the number of days subscribed.

Build and Deploy Text-2-SQL LLM Using OpenAI and AWS
In this LLM project, you will learn to build a user-friendly web application that leverages Large Language Models (LLMs) to convert natural language queries into optimized SQL commands.

Deploying Machine Learning Models with Flask for Beginners
In this MLOps on GCP project you will learn to deploy a sales forecasting ML Model using Flask.

PyTorch Project to Build a GAN Model on MNIST Dataset
In this deep learning project, you will learn how to build a GAN Model on MNIST Dataset for generating new images of handwritten digits.

Time Series Python Project using Greykite and Neural Prophet
In this time series project, you will forecast Walmart sales over time using the powerful, fast, and flexible time series forecasting library Greykite that helps automate time series problems.

AI Video Summarization Project using Mixtral, Whisper, and AWS
In this AI Video Summarization Project, you will build a quiz generation tool by extracting key concepts from educational videos and generating concise summaries.

Build a Graph Based Recommendation System in Python -Part 1
Python Recommender Systems Project - Learn to build a graph based recommendation system in eCommerce to recommend products.