In this guide, we will discuss R Pie Charts.
R programming language has several libraries for creating charts and graphs. A pie-chart is a representation of values in the form of slices of a circle with different colors. Slices are labeled with a description, and the numbers corresponding to each slice are also shown in the chart. However, pie charts are not recommended in the R documentation, and their characteristics are limited. The authors recommend a bar or dot plot on a pie chart because people are able to measure length more accurately than volume.
The Pie charts are created with the help of the pie () function, which takes positive numbers as vector input. Additional parameters are used to control labels, colors, titles, etc.
There is the following syntax of the pie() function:
pie(X, Labels, Radius, Main, Col, Clockwise)
Here,
- X is a vector that contains the numeric values used in the pie chart.
- Labels are used to give the description to the slices.
- Radius describes the radius of the pie chart.
- Main describes the title of the chart.
- Col defines the color palette.
- Clockwise is a logical value that indicates the clockwise or anti-clockwise direction in which slices are drawn.
Example
# Creating data for the graph. x <- c(20, 65, 15, 50) labels <- c("India", "America", "Shri Lanka", "Nepal") # Giving the chart file a name. png(file = "Country.jpg") # Plotting the chart. pie(x,labels) # Saving the file. dev.off()
Output
Title and color
A pie chart has several more features that we can use by adding more parameters to the pie() function. We can give a title to our pie chart bypassing the main parameter. It tells the title of the pie chart to the pie() function. Apart from this, we can use a rainbow color pallet while drawing the chart by passing the col parameter.
Note: The length of the pallet will be the same as the number of values that we have for the chart. So for that, we will use length() function.
Let’s see an example to understand how these methods work in creating an attractive pie chart with title and color.
Example
# Creating data for the graph. x <- c(20, 65, 15, 50) labels <- c("India", "America", "Shri Lanka", "Nepal") # Giving the chart file a name. png(file = "title_color.jpg") # Plotting the chart. pie(x,labels,main="Country Pie chart",col=rainbow(length(x))) # Saving the file. dev.off()
Output
Slice Percentage & Chart Legend
There are two additional properties of the pie chart, i.e., slice percentage and chart legend. We can show the data in the form of percentages as well as we can add legends to plots in R by using the legend() function. There is the following syntax of the legend() function.
legend(x,y=NULL,legend,fill,col,bg)
Here,
- x and y are the coordinates to be used to position the legend.
- legend is the text of legend
- fill is the color to use for filling the boxes beside the legend text.
- col defines the color of line and points besides the legend text.
- bg is the background color for the legend box.
Example
# Creating data for the graph. x <- c(20, 65, 15, 50) labels <- c("India", "America", "Shri Lanka", "Nepal") pie_percent<- round(100*x/sum(x), 1) # Giving the chart file a name. png(file = "per_pie.jpg") # Plotting the chart. pie(x, labels = pie_percent, main = "Country Pie Chart",col = rainbow(length(x))) legend("topright", c("India", "America", "Shri Lanka", "Nepal"), cex = 0.8, fill = rainbow(length(x))) #Saving the file. dev.off()
Output
3 Dimensional Pie Chart
In R, we can also create a three-dimensional pie chart. For this purpose, R provides a plotrix package whose pie3D() function is used to create an attractive 3D pie chart. The parameters of pie3D() function remain same as pie() function. Let’s see an example to understand how a 3D pie chart is created with the help of this function.]
Example
# Getting the library. library(plotrix) # Creating data for the graph. x <- c(20, 65, 15, 50,45) labels <- c("India", "America", "Shri Lanka", "Nepal","Bhutan") # Give the chart file a name. png(file = "3d_pie_chart1.jpg") # Plot the chart. pie3D(x,labelslabels = labels,explode = 0.1, main = "Country Pie Chart") # Save the file. dev.off()
Output
Example
# Getting the library. library(plotrix) # Creating data for the graph. x <- c(20, 65, 15, 50,45) labels <- c("India", "America", "Shri Lanka", "Nepal","Bhutan") pie_percent<- round(100*x/sum(x), 1) # Giving the chart file a name. png(file = "three_D_pie.jpg") # Plotting the chart. pie3D(x, labels = pie_percent, main = "Country Pie Chart",col = rainbow(length(x))) legend("topright", c("India", "America", "Shri Lanka", "Nepal","Bhutan"), cex = 0.8, fill = rainbow(length(x))) #Saving the file. dev.off()
Output
Next Topic; Click Here
Thanks a lot for the post. Much obliged.