DEV Community

Cover image for R pie

R pie

・3 min read

How to construct Pie Charts in R programming

Creating a basic pie chart in R is as easy as pie. If list or vector is a list of positive numbers, then simply use the pie() function to draw the pie.


let's try to use the following data to construct pie chart.
To represent the five data listed above in pie chart we type the following codes:

<< Codes>>

Enter fullscreen mode Exit fullscreen mode

<< Result>>

Alt Text
looking at the graph it is exactly the chart of the 5 data but it doesn't make sense enough as sometime it might not be just number we might need to label. so let's take a look at a beautiful example.


Represent the following table in form of pie chart.

Students Scores
Maxwizard 60
Precious 50
Abass 70
John 65
Victoria 55

to draw the pie we need to list all the score and the students name accordingly inside two different vector then put inside the pie function.

<< Codes>>

Enter fullscreen mode Exit fullscreen mode

<< Result>>

Alt Text
study the codes and the result it is not different from example1 except that we make use of label parameter. You should be careful when you need to label, notice that it is the score that we need to draw the graph. so all the students name should be assigned to the label parameter.

However, while dealing with chart we might need to write the headings for our chart. Also we might prefer some colours to others. Therefore there are some other parameters which you can use to perform these actions, they are:

  1. col : this is use to change the color of the part of the chart.
  2. main : this is used to write the heading of your chart.


the following table show the number of students in each class in Government secondary school. draw a pie chart for the table.

Class Number of Student
JSS1 40
JSS2 30
JSS3 30
SSS1 25
SSS2 45
SSS3 65

<< Codes>>

pie(Number,labels=Class,col=color,main="Government Secondary School")
Enter fullscreen mode Exit fullscreen mode

<< Result>>

Alt Text
wooow! we just get a beautiful result! that is cool! but sometimes you might be working with so many data and you might not want to label that way. I mean you may like to represent it in form of key therefore you use something called legend label.


use the data in Example3 to plot a pie chart.

<< Codes>>

pie(Number,col=color,main="Government Secondary School",labels="")
Enter fullscreen mode Exit fullscreen mode

<< Result>>

Alt Text
Study the codes and the result, we make use of a function called legend() to construct the legend label, the funtion takes four parameters.

  1. the position of the legend box: this is parameter used to specify where the legend box should be place. it can be bottomleft,bottomright,topleft or topright. you can change the value written in your code to any of the other three to see how it works.
  2. legendLabel: the is the key of the legend it usually contain the list of data(label) in the chart.
  3. fill: this is the color, it should be the same color use to construct the chart.
  4. cex: this is use to specify how big the legend box should be you can change the value to 1 then 2 to see the effect.

Now I beleve you can now construct beautiful pie chart using R. coding is easy and fun just keep struggling and do not stop. like and comment below. you can also chat me up on whats-app (07045225718) have a nice day!.

Discussion (0)