Pie Chart in R Programming

The Pie Chart in R is very useful to display the region-wise sales, Countrywide customers, Sales by Country, etc. Let me show how to Create a Pie Chart, Format its color, borders, adding legions, and creating a 3D Pie Chart in R Programming language with example.

R Pie Chart Syntax

The syntax to draw pie chart in R Programming is

pie(x, labels = names(x), col = NULL, main = NULL)

and the complex syntax behind this pie chart is:

pie(x, labels = names(x), edges = 200, radius = 0.8, clockwise = FALSE, 
     init.angle = if(clockwise) 90 else 0, density = NULL, angle = 45, 
     col = NULL, border = NULL, lty = NULL, main = NULL, ..)

There are many arguments supported by the Pie Chart in R programming language, and the following are the most used arguments in real-time:

  • x: Please specify a vector of non-negative numeric values. It decides the size of each Pie chart slice
  • labels: This argument helps us to pass names to each slice.
  • edges: Th numeric value accepted by this argument change the outer circle of the pie. We strictly recommend keeping the value to default 200.
  • radius: This is the radius of the Pie Chart circle whose value is between -1 and 1
  • clockwise: It is a Boolean value indicating if slices drew in the clockwise direction or counter-clockwise direction. By default, it is counter-clockwise
  • init.angle: This argument can help you to specify the starting angle (in degrees) for slices. By default, it is 0. If the clockwise is TRUE. Then init.angle becomes 90 degrees.
  • density: Please specify the shading lines density (in lines per inch). By default, it is NULL, which means no shading lines.
  • angle: You can assign the slope of shading lines using this argument.
  • col: Please specify the vector of colors you want to use for your pie chart. By default, it uses a set of 6 pascal colors.
  • border, lty: This argument (possibly vector) passed to polygon, which draws each slice of a pie.
  • Main: You can provide the Title for your Pie chart.

Create basic R Pie Chart

In this example, we show how to create a Pie chart using the vectors

# Basic Pie Chart in R Example

values <-  c(906, 264, 289, 339, 938)
countries <-  c("India","Sri Lanka","Nepal","Bhutan", "China")

pie(values, labels = countries)
Pie Chart in R Programming 1

First, we declared one vector of random numbers and another vector of random countries

values <-  c(906, 264, 289, 339, 938)
countries <-  c("India","Sri Lanka","Nepal","Bhutan", "China")

Next, we used the pie to draw the pie chart. From the below code snippet, you can observe that Slice size decided on values.

pie(values, labels = countries)

Changing Borders and lty of a Pie Chart in R Programming

In this example, we show how to change the Pie chart border color and to change the line style using border and lty arguments.

# R Pie Chart - Border and lty Example

values <-  c(906, 264, 289, 339, 938)
countries <-  c("India","Sri Lanka","Nepal","Bhutan", "China")

pie(values, labels = countries, border = "red", lty = 2)
Pie Chart in R Programming 2

TIP: To assign different border colors, we use a Vector of colors. For example, border = c(“red”, “green”, “black”…)

Create a Pie Chart in R Programming

We create a Pie Chart using the external data. For this, we are importing data from the CSV file using read.csv function. I suggest you refer R Read CSV article to understand the steps involved in CSV file import in R Programming.

# R Pie Chart Example
getwd()

employee <- read.csv("Products.csv", TRUE, sep = ",")
data <- aggregate(employee$SalesAmount, 
                  by=list(employee$EnglishCountryRegionName), FUN=sum)
print(data)

pie(data$x, data$Group.1)
Pie Chart in R Programming 3

From the below code snippet, you can observe that we used the Aggregate function to find the total amount of sales in each country. Or we can say the Sum of Sales Amount Group By Countries.

data <- aggregate(employee$SalesAmount, 
                  by=list(employee$EnglishCountryRegionName), FUN=sum)

The above statement returns the output as a List. So, we are using the $ to extract data from List

pie(data$x, data$Group.1)

Change Colors of Pie Chart in R

In this example, we change the Pie chart Slice colors using col argument

# R Pie Chart - Changing Colors Example
getwd()

employee <- read.csv("Products.csv", TRUE, sep = ",")
data <- aggregate(employee$SalesAmount, 
                  by=list(employee$EnglishCountryRegionName), FUN=sum)
print(data)

pie(data$x, data$Group.1, col = rainbow(length(data$x)))
Pie Chart in R Programming 4

Change Slice Direction & Name of Pie Chart

We change the R Pie chart Slice direction using a clockwise argument and assign title/name to the pie chart using the main argument.

# R Pie Chart - Changing Slic Direction & Assigning Name
getwd()

employee <- read.csv("Products.csv", TRUE, sep = ",")
data <- aggregate(employee$SalesAmount, 
                  by=list(employee$EnglishCountryRegionName), FUN=sum)
print(data)

pie(data$x, data$Group.1, col = rainbow(length(data$x)),
    clockwise = TRUE, main = "Sales By COuntry")
Pie Chart in R Programming 5

Adding Legend to Pie Chart

Here, we Add Legend to the Pie chart using legend function

# R Pie Chart - Adding Legend & Custom Colors
getwd()

employee <- read.csv("Products.csv", TRUE, sep = ",")
data <- aggregate(employee$SalesAmount, 
                  by=list(employee$EnglishCountryRegionName), FUN=sum)
print(data)
cols = c("red", "orange", "grey", "white", "black", "yellow")

pie(data$x, data$Group.1, col = cols,  main = "Sales By COuntry")

legend("topright", c("Australia","Canada","France",
                     "Germany", "United Kingdom", "United States"), 
       cex = 0.8, fill = cols)
Pie Chart in R Programming 6

Changing Density of a Pie Chart Slice

We change the Slice density using density argument

# R Pie Chart - Changing the Density
getwd()

employee <- read.csv("Products.csv", TRUE, sep = ",")
data <- aggregate(employee$SalesAmount, 
                  by=list(employee$EnglishCountryRegionName), FUN=sum)
print(data)
cols = c("red", "orange", "grey", "blue", "black", "yellow")

pie(data$x, data$Group.1, col = cols,  density = 50, 
    main = "Sales By Country")

legend("topright", c("Australia","Canada","France",
                     "Germany", "United Kingdom", "United States"), 
       cex = 0.7, fill = cols)
Pie Chart in R Programming 7

3D Pie Chart in R Programming

Let us see how to create a 3D Pie chart in R Programming. To draw the 3D Pie chart, we need a pie3D function, and to use this function, we have to add a plotrix library (package) to our R Studio.

# 3D R Pie Chart Example

employee <- read.csv("Products.csv", TRUE, sep = ",")
data <- aggregate(employee$SalesAmount, 
                  by=list(employee$EnglishCountryRegionName), FUN=sum)
print(data)

pie3D(data$x,labels = data$Group.1, explode = 0.1, 
      main = "3D Pie Chart in R ")
Pie Chart in R Programming 8

Comments are closed.