EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials R Programming Tutorial Pie Chart in R
Secondary Sidebar
R programming Tutorial
  • Chart/graphs
    • Graphs in R
    • Bar Charts in R
    • Pie Chart in R
    • Histogram in R
    • Line Graph in R
    • Plot Function in R
    • Scatterplot in R
    • R Boxplot labels
  • Basic
    • What is R Programming Language
    • Careers in R Programming
    • Install R
    • List of R Packages
    • Introduction of R Tools Technology
    • R Programming Language
    • DataSet in R
    • What is RStudio?
    • R-studio-Functions
    • R Packages
    • Time series?in R
    • R Data Types
    • R for data science
    • R Operators
    • R Data Frame
    • R Analytics Tool
    • R Tree Package
    • Vectors in R
  • Control statement
    • If Statement in R
    • If Else Statement in R
    • Else if in R
    • Switch Statement in R
  • Loops
    • Loops in R
    • For Loop in R
    • Nested For Loop in R
    • While Loop in R
    • Next in R
  • Regression in R
    • Simple Linear Regression in R
    • Linear Regression in R
    • Multiple Linear Regression in R
    • Logistic Regression in R
    • Poisson Regression in R
    • OLS Regression in R
    • P-Value in Regression
  • Anova in R
    • ANOVA in R
    • One Way ANOVA in R
    • Two Way ANOVA in R
  • Data Structure
    • R list
    • Arrays in R
    • Data Frames in R
    • Factors in R
    • R Vectors
  • Advanced
    • Statistical Analysis with R
    • R String Functions
    • Data Exploration in R
    • R CSV Files
    • KNN Algorithm in R
    • Sorting in R
    • lm Function in R
    • Hierarchical Clustering in R
    • R Normal Distribution
    • Binomial Distribution in R
    • Decision Tree in R
    • GLM in R
    • Arima Model in R
    • Linear Model in R
    • Predict Function in R
    • Survival Analysis in R
    • Standard Deviation in R
    • Statistical Analysis in R
    • Predictive Analysis?in R
    • T-test in R
    • Database in R
  • Programs
    • Functions in R
    • Boxplot in R
    • R Program Functions
    • Factorial in R
    • Random Number Generator in R
  • Interview question
    • R Interview Questions

Related Courses

R Programming Certification Course

Statistical Analysis Course Training

All in One Data Science Courses

Pie Chart in R

By Priya PedamkarPriya Pedamkar

pie charts on r

Introduction to Pie Charts in R

Pie Chart in R is one of the basic chart features which are represented in the circular chart symbol. The section of the circle shows the data value proportions. The sections of the pie chart can be labeled with meaningful names. Pie charts are generally preferred for small-size vector variables. Pie charts can be of two-dimensional view or three-dimensional views based upon the R packages. Pie is the function in R language which is supporting two-dimensional pie charts. Pie charts are very useful for data analysis. Pie charts in R can be assigned with a meaning title using main as a parameter in the pie function.

Using the pie charts, patterns in the data can be understood easily, whereas if we go through the numeric figure, often understanding takes a while. For example, if we plot the above example as a pie chart, we can understand the amount of production and proportion of production within a minute.

There are various packages for plotting pie charts in R, and among those many options, we shall focus on two methods in this article.

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (86,408 ratings)

Syntax

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

The above section provided a brief idea of the pie chart and its use. In this section, we shall learn about pie charts in R specifically. For those who are new to R, it is a programming language mainly used for data analysis and machine learning. R is quite rich in its functionality and provides hundreds of libraries for various use cases.

In R, it can be created by using a simple in-built function, and the syntax for the same is given below.

pie(x, labels, radius, main, col, clockwise)

Where,

  • x is called a vector, and it contains the numeric values which are to be used in the pie chart, such as those production figures in the above example.
  • labels take a name for those values in X, such as the name of chemicals.
  • The radius argument is for the radius of the circle of the pie chart. Its typical value lies between −1 and +1.
  • The main argument can be used to provide the title of the chart.
  • col argument can be used to provide the colors to the chart.
  • clockwise is a logical value that takes either True or False, indicating if the slices of charts are drawn in a clockwise or anti-clockwise manner.
Note: That X is a mandatory argument, and the rest all are optional.

How to create a pie chart in R?

Now that we understood the syntax of the pie chart as well let’s build a pie chart. For this, we will again use the same example in the introduction section above.

First of all, let’s convert the example above in the form of a table for easy understanding.

Name of chemical Amount produced (in MT)
AB1 90
AB2 50
AB3 100
AB4 40
AB5 20
Total 300

First, we use the following two lines of R code to convert the table above into two vectors, one for the name of the chemical and the other for the volume of the chemical.

Now, we plot a simple pie chart by only providing the x value in the syntax above:

  • chem <- c(“AB1″,”AB2″,”AB3″,”AB4″,”AB5”)
  • vol <- c(90,50,100,40,20)
  • pie(vol)

Its output is the figure below:

simple pie chart

If you observe the output, it is not very clear as to what is represented by what. So to make it more intuitive, we input a few more arguments in the pie function and run again.

  • chem <- c(“AB1″,”AB2″,”AB3″,”AB4″,”AB5”)
  • vol <- c(90,50,100,40,20)
  • pie(x=vol, labels = chem, radius = 1,main = “Pie chart for chemical production”, clockwise = T)

simple pie chart 02

This picture is better to understand as it contains the name of the chemicals as well as a title. Please note the color scheme in both of these charts is coming by default which we can change as per our need or wish. We will do that in the section below.

How to change pie charts and fill color?

In this section, let’s learn how can be a change pie chart.

First, let’s show the number of chemicals in the chart instead of the name of chemicals.

  • pie(x=vol, labels = vol, radius = 1,main = “Pie chart for chemical production”, clockwise = T)
  • Run it yourself and see the output.

Next, lets change the color of the charts.

  • chem <- c(“AB1″,”AB2″,”AB3″,”AB4″,”AB5”)
  • vol <- c(90,50,100,40,20)
  • pie(x=vol, labels = chem, radius = 1,main = “Pie chart for chemical production”, col=c(“red”,”blue”,”green”,”black”,”yellow”),clockwise = T)

Here we specified the colors that we want. The output is as below:

Fill color

How to create a 3D pie chart?

In this section, we will learn how to build a 3D pie chart in R. for building a 3d pie chart; we need to install a library first as it can not be done from a basic inbuilt function.

You should install the library plotrix before running the code for the pie chart. To install the library, simply run the following command in R.

  • Install.packages(“plotrix”)

After that, run the following two lines to get a 3d plot.

  • chem <- c(“AB1″,”AB2″,”AB3″,”AB4″,”AB5”)
  • vol <- c(90,50,100,40,20)
  • library(plotrix)
  • pie3D(vol,labels = chem,explode = 0.1, main = “Pie Chart for chemicals “)

The output is as below:

chart for chemicals

Conclusion

Pie charts are used a lot, and it is very intuitive and informative, which I believe is very clear by now. In financial domains and many other sectors too, pie charts are the basic visualization where almost all analysis begins. It is simple yet very powerful. In this article, we provided enough details which should help anyone start with building pie charts with great confidence and ease. Students and learners are also advised to look into the help menu of R, where they can learn more details and additional functionalities of pie charts. There is a great visualization package called ggplot2 in R, which provides many customization options to pie charts and all other visualization in general; candidates are advised to look into that as well. Finally, if there is any question or further doubt, you can always comment on this article and get in touch for more explanations, examples as well as theoretical discussions.

Recommended Articles

This has been a guide to Pie charts in R. Here; we discussed how to create a pie chart, How to change the pie chart and fill color, and How to create a 3D pie chart. You may also look at the following articles to learn more –

  1. Pie Chart Examples
  2. Binomial Distribution in R
  3. Graphs vs Charts
  4. Guide To Line Graph in R
Popular Course in this category
R Programming Training (13 Courses, 20+ Projects)
  13 Online Courses |  20 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Statistical Analysis Training (15 Courses, 10+ Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more