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

Jingwei Li

Graduate Research assistance at Stony Brook University
linkedin profile url

ProjectPro is an awesome platform that helps me learn much hands-on industrial experience with a step-by-step walkthrough of projects. There are two primary paths to learn: Data Science and Big Data.... Read More

Relevant Projects

OpenCV Project for Beginners to Learn Computer Vision Basics
In this OpenCV project, you will learn computer vision basics and the fundamentals of OpenCV library using Python.

MLOps Project for a Mask R-CNN on GCP using uWSGI Flask
MLOps on GCP - Solved end-to-end MLOps Project to deploy a Mask RCNN Model for Image Segmentation as a Web Application using uWSGI Flask, Docker, and TensorFlow.

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

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.

NLP and Deep Learning For Fake News Classification in Python
In this project you will use Python to implement various machine learning methods( RNN, LSTM, GRU) for fake news classification.

MLOps Project on GCP using Kubeflow for Model Deployment
MLOps using Kubeflow on GCP - Build and deploy a deep learning model on Google Cloud Platform using Kubeflow pipelines in Python

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.

Build a Logistic Regression Model in Python from Scratch
Regression project to implement logistic regression in python from scratch on streaming app data.

Detectron2 Object Detection and Segmentation Example Python
Object Detection using Detectron2 - Build a Dectectron2 model to detect the zones and inhibitions in antibiogram images.

MLOps AWS Project on Topic Modeling using Gunicorn Flask
In this project we will see the end-to-end machine learning development process to design, build and manage reproducible, testable, and evolvable machine learning models by using AWS