A pie chart in R is a statistical graph that is circular in nature. Pie charts use ‘Slices’ to represent or illustrate the numerical distribution of the data.
In a Pie chart, the size of the Slice shows the weightage of the values. In this article, we are going to plot the simple pie chart, adding labels, text and also using ggplot2 as well as the plotrix library.
Pros and Cons of the Pie chart
Although Pie chart serves as one of the best plots to showcase the data distribution, it has some setbacks. Some of the pros and cons of the pie chart are given below:
Pie chart – Pros
- Represents the proportions of the multiple groups of data.
- Can represent the quantity through circle size.
- Simple visualisation graph.
- Highly used graph to represent the data in majority of mediums.
Pie chart – Cons
- Not precise.
- Dynamic data requires plenty of charts to showcase.
- Key decisions cannot be made on this visualised data.
- 3-D plotting cannot be used due to false impressions.
The Syntax for Creating a Pie Chart in R
Pie chart syntax.
- x = A vector or data have various values in it.
- Labels = Annotations for each slice.
- Radius = determines the radius of the pie circle.
- Main = represents the title of the pie chart.
- col = This parameter gives the colour of the slices.
Plotting a Pie Chart in R
Well, I hope you are clear about the concept, definition, and basic understanding of Pie chart by now. Let’s plot a simple pie chart in R which will be very interesting to do.
1. Basic Pie Chart in R
#vector with numerical values Marks<- c(89,90,67,54,77,48) #vector with strings as student names Students_name<-c("Rahul","Ajay","Ram","Vishnu","Rupali","Shyma") #plots a basic pie chart in R pie(Marks,Students_name)
Kudos on your first basic pie chart in R. In this chart, The numerical values (marks) are illustrated as slice sizes and the student names are showcased as the labels for the respective marks.
Are you Excited for more? Without any further delay, let’s plot another pie chart with some more features.
2. Adding a title and color to our Pie chart
The heading itself revealed the subject. In this section, we are going to add a title and color for our pie chart. Let’s see how it works.
#vector having GDP values GDP<-c(21.44,14.14,5.15,4.44,2.94) #Vector having the strings as countries Countries<-c("America","China","Japan","Germany","India") #plots the pie chart with title and colour pie(GDP,labels = Countries,main = "Top 5 countries by GDP",border = 'White',col = rainbow(length(GDP)))
You can now compare this pie chart with the earlier chart. You can see the massive changes in this plot right? This plot makes more sense and looks elegant as well. You can observe that we have added a title ‘Top 5 countries by GDP’ to the graph and the colour (rainbow colours) to the pie slices.
In some cases, you may feel that the labels are not looking good or you may want to show both the data i.e. values as well as the labels. In those cases, what can you do? If your guess is adding a ‘legend’, you are awesome.
Yes, adding a legend to the pie chart makes it more organized, and also it makes a good sense.
3. Pie chart with a legend
A legend is a simple visual explanation of the things which is present on the graph. It uses the color or symbol with the short explanation or the labels to indicate the things on a graph which makes a graph look professional and organized as well.
In this section, we are going to add a legend to our pie chart.
#creates 2 vector of values and labels x<-c(88,85,75,80,90) laptop_brands<-c('Dell','HP','Lenovo','Asus','Apple') #calculates the percentage of the values percentage<-round(x/sum(x)*100) #concatenates the strings with percentages labels_new<-paste(laptop_brands,percentage) labels_new
Output = "Dell 21" "HP 20" "Lenovo 18" "Asus 19" "Apple 22"
#concatenates the above output with the '%' symbol final_labels<-paste(labels_new,'%',sep = "") final_labels
Output = "Dell 21%" "HP 20%" "Lenovo 18%" "Asus 19%" "Apple 22%"
#plots the pie chart with various parameters pie(x,labels = final_labels,col = rainbow(length(final_labels)),main = "Popularity of laptop brands",radius = 1) #adds the legend to the pie chart legend('topright',c("Dell","HP","Lenovo","Asus","Apple"),cex = 0.7,fill = rainbow(length(x)))
Looks good right? Now our pie chart has a legend which indicates the laptop brands and their colours in the pie chart. This is all about adding a legend to a pie chart.
In the next section, we are going to plot a pie chart using ggplot2.
4. Plotting a Pie chart in R using ggplot2
In this section, we are going to use one of the best library for plotting in R – ggplot2.
ggplot2 is data visualisation package in R. ggplot2 adds many features and functionalities to the graphs to make it better interms of presence and smoothness as well.
#creates a data frame df<-data.frame(Phones=c("Samsung","One plus","iphone","Red mi","Realme"),sales=c(58,68,78,38,31))
Phones sales 1 Samsung 58 2 One plus 68 3 iphone 78 4 Red mi 38 5 Realme 31
#plots the ggplot2 pie chart ggplot(df,aes(x='',y=sales,fill=Phones))+ ggtitle("Pie chart using ggplot2")+ geom_bar(stat = "Identity",width = 2,color="Yellow")+ coord_polar("y",start = 0)+ theme_void()
5. Pie chart using plotrix library
Well, the 3D plotting may look way better than normal plots, but a 3-D pie chart is not a good option in representing the negative data and moreover a large number of variables cannot be effectively visualized by pie chart as it becomes ineffective over large data even if you add the labels. The plot becomes overcrowded.
You may wonder why I am mentioning only negatives about this 3D pie chart. Well, the 3D pie chart only looks good over small quantities of data. It fits well for an immediate decision and just to represent your fractions.
Let’s move on from the above discussion and plot a 3D pie chart using the library plotrix.
Plotrix: Plotlix is a combination of several plotting features such as labels, titles, colors, and more. This is used to create 3D plots.
I am using the same data, which i have used in the ggplot2 pie chart. Let’s try to plot a 3-D graph for the above ggplot2 pie chart.
#creates 2 vector of values and labels x<-c(88,85,75,80,90) laptop_brands<-c('Dell','HP','Lenovo','Asus','Apple') #calculates the percentage of the values percentage<-round(x/sum(x)*100) #concatenates the strings with percentages labels_new<-paste(laptop_brands,percentage) labels_new #concatenates the above output with the '%' symbol final_labels<-paste(labels_new,'%',sep = "") final_labels
#install required packages install.packages('plotrix') library(plotrix) #plots the 3-D pie chart pie3D(x,labels = final_labels,explode = 0.3,main='The 3-D pie chart using Plotrix',labelcex = 0.8,shade = 0.5,radius = 2.5)
Well, in this article we have created a pie charts and focused on the in and outs of the pie charts. We have created a pie chart using basic R, ggplot 2 as well as the plotrix libraries.
Pie chart is a simple but effective graph to represent the small amount of data. It will give you all the proportions with the labels which is always easy to understand to all.
I hope, you got good understanding about the pie charts in R. That’s all for now and happy plotting!!!
More study: R documentation