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:
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.
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.
Students=c("Maxwizard","Precious","Abass","John","Victoria") Scores=c(60,50,70,65,55) pie(Scores,label=Students)
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:
col: this is use to change the color of the part of the chart.
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|
Class<-c("JSS1","JSS2","JSS3","SSS1","SSS2","SSS3") Number<-c(40,30,30,25,45,65) color<-c('red','green','orange','yellow','blue','violet') pie(Number,labels=Class,col=color,main="Government Secondary School")
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.
Class<-c("JSS1","JSS2","JSS3","SSS1","SSS2","SSS3") Number<-c(40,30,30,25,45,65) color<-c('red','green','orange','yellow','blue','violet') pie(Number,col=color,main="Government Secondary School",labels="") legendLabel=Class legend("bottomleft",legendLabel,fill=color,cex=0.8)
- 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.
- legendLabel: the is the key of the legend it usually contain the list of data(label) in the chart.
- fill: this is the color, it should be the same color use to construct the chart.
- 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!.