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 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 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 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. By default, it uses a set of 6 pascal colors.
- border, lty: This argument (possibly vector) passed to polygon, which draws each slice of it.
- Main: You can provide the Title.
Create basic R Pie Chart
In this example, we show how to create it using the vectors
First, we declared one vector of random numbers and another vector of random countries.
Next, we used the pie to draw the chart. From the below code snippet, you can observe that Slice size decided on values.
values <- c(906, 264, 289, 339, 938) countries <- c("India","Sri Lanka","Nepal","Bhutan", "China") pie(values, labels = countries)

Changing Borders and lty of a Pie Chart in R Programming
In this example, we show how to change the border color and to change the line style using border and lty arguments.
# 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)

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.
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. This statement returns the output as a List.
So, we are using the $ to extract data from List
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)

Change Colors of Pie Chart in R
In this example, we change the Slice colors using the col argument
# 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)))

Change Slice Direction & Name of Pie Chart
We change the R Pie chart Slice direction using a clockwise argument and assign title/name using the main argument.
# 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")

Adding Legend
Here, we Add Legend to the R Pie chart using legend function
# 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)

Changing Density of a Pie Chart Slice
We change the Slice density using density argument of the 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)

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 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 ")

Comments are closed.