Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

PIE CHART IN R

A pie chart, also known as circle chart or pie plot, is a circular graph that represents
proportions or percentages in slices, where the area and arc length of each slice is
proportional to the represented quantity.
How to draw pir chart in R
A circle chart can be created with the pie function in base R. Even though there exists
more packages to create pie charts, like ggplot2, in this tutorial we will review how to
create circle chart with the pie function and the PieChart function of
the lessR package, to display percentages.
The pie() R function
The R pie function allows you to create a pie chart in R. Consider, for instance, that
you want to create a piechart of the following variable, that represents the count of
some event:
Sample vector
count <- c(7, 25, 16, 12, 10, 30)
The code for a pie chart in R is as follows. Note that you can customize the size of the
pie (from -1 to 1) with the radius argument, that by default takes the value 0.8.
pie(count)
Program – 23:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
You can also modify the direction of the pie with the clockwise argument, that by
default is FALSE
Program – 24:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, clockwise = TRUE)
However, you may have noticed that the plot doesn’t display the corresponding value
of each slice. To solve this issue you can pass the vector to the labels argument as
follows.
Program – 25:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, labels = count)
If preferred, you can add a character vector with the names you desire to represent
each slice:
Program – 26:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, labels = count)
Program – 27:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, labels = count)
pie(count, labels = c("0-15", "16-30", "31-45", "46-60", "61-75", "76-90"))
Customization
In addition, you can modify the color of the graph with the col argument. In the
following block of code we show you how to use different color palettes. Note that
the cex argument allows you to modify the size of the labels.
Program – 28:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, labels = count, col = 1:6, cex = 2)
Program – 29:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, labels = count, col = rainbow(6), cex = 2)
Program – 30:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, labels = count, col = topo.colors(6), cex = 2)
Program – 31:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
par(mfrow = c(1, 3))
pie(count, labels = count, col = 1:6, cex = 2)
pie(count, labels = count, col = rainbow(6), cex = 2)
pie(count, labels = count, col = topo.colors(6), cex = 2)
par(mfrow = c(1, 1))
You can also specify a vector of colors for the border of each slice. If you want the
color to be equal to the color of the area of the slice
Program – 32:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, labels = count, col = rainbow(6), border = rainbow(6))
If you want to modify the line type of the borders of the plot you can make use of
the lty argument:
Program – 33:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
par(mfrow = c(2, 3))
pie(count, labels = count, col = rainbow(6), lty = 1)
pie(count, labels = count, col = rainbow(6), lty = 2)
pie(count, labels = count, col = rainbow(6), lty = 3)
pie(count, labels = count, col = rainbow(6), lty = 4)
pie(count, labels = count, col = rainbow(6), lty = 5)
pie(count, labels = count, col = rainbow(6), lty = 6)
par(mfrow = c(1, 1))
Furthermore, you can add shading lines with the density argument. The greater the
value, the greater number of lines to be displayed.
Program – 34:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
par(mfrow=(c(2,3)))
pie(count, labels = count, col = rainbow(6), density = 0)
pie(count, labels = count, col = rainbow(6), density = 10)
pie(count, labels = count, col = rainbow(6), density = 30)
pie(count, labels = count, col = rainbow(6), density = 50)
pie(count, labels = count, col = rainbow(6), density = 70)
pie(count, labels = count, col = rainbow(6), density = 100)
par(mfrow=(c(1,1)))
Note that the angle argument can be used to modify the angle of the lines.
Program – 35:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
par(mfrow=(c(2,3)))
pie(count, labels = count, col = rainbow(6), density = 0,angle=90)
pie(count, labels = count, col = rainbow(6), density = 10,angle=45)
pie(count, labels = count, col = rainbow(6), density = 30,angle=180)
pie(count, labels = count, col = rainbow(6), density = 50,angle=225)
pie(count, labels = count, col = rainbow(6), density = 70,angle=270)
pie(count, labels = count, col = rainbow(6), density = 100,angle=0)
par(mfrow=(c(1,1)))
Pie chart in R with percentage
Circle charts are very useful to show percentages, but the pie function doesn’t allow
you to automatically display them. In order to show percentages for the sample vector
(as the sum of the elements is equal to 100), you can type:
Program – 36:
count <- c(7, 25, 16, 12, 10, 30)
pie(count)
pie(count, labels = paste0(count, "%"))
Pie chart of categorical data
Consider, for instance, that you want to create a circle chart with the corresponding
percentage of males and females registrered in some event. You have the following
data:
gender_var <- factor(c(rep("Male", 10), rep("Female", 20)))
Hence, as you have a character variable, you can use the table function to count the
number of males and females of the character vector and pass the output to the pie
function.
Program – 37:
gender_var <- factor(c(rep("Male", 10), rep("Female", 20)))
print(gender_var)
gender_table <- table(factor(gender_var))
print(gender_table)
pie(gender_table)
Adding a legend
In order to create a pie chart in R with legend you need to use the legend
function. As an example, if you want to display a legend in the top left of the image
you can execute the following code:
Program – 38:
count<- c(20, 50, 30)
pie(count, labels = paste0(count, "%"),col= c("red", "green", "blue"))
legend("topleft", legend = c("ClassTests", "QUIZ", "MID"),fill = c("red", "green",
"blue"))
3D pie chart
In this final section you will learn how to draw a 3D pie chart in R. For that
purpose, you will need to install the plotrix package, that contains the pie3D function.
Installing plotrix package in R
Step-1: Click on TOOLS under MENU in RStudio
Step-2: type plotrix under Packages(separate multiple with space or comma)
Step-3: choose plotrix package
Step-4: click on Install
The code to draw a 3D pie chart in R is the following:
Program – 39:
library(plotrix)
num_data <- c(65, 35)
pie3D(num_data)
Note that by default the function doesn’t display labels, so you will need to
indicate them in the labels argument
Program – 40:
library(plotrix)
num_data <- c(65, 35)
pie3D(num_data, labels = num_data)
In addition, you can explode the pie with the explode argument:
Program – 41:
library(plotrix)
num_data <- c(65, 35)
pie3D(num_data, labels = num_data, explode = 0.25)
Program – 42:
library(plotrix)
num_data <- c(65, 35)
pie3D(num_data, labels = num_data, col = rainbow(6), labelcol = "red", border =
"blue")
Program-43:
Write a R program to draw pie chart of your academic performance from last 3
semesters?

You might also like