The Wayback Machine - https://web.archive.org/web/20240724195429/https://www.geeksforgeeks.org/r-pie-charts/
Open In App

R – Pie Charts

Last Updated : 08 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report

A pie chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. It depicts a special chart that uses “pie slices”, where each sector shows the relative sizes of data. A circular chart cuts in the form of radii into segments describing relative frequencies or magnitude also known as a circle graph. 

R – Pie Charts

R Programming Language uses the function pie() to create pie charts. It takes positive numbers as a vector input. 

Syntax: pie(x, labels, radius, main, col, clockwise)

Parameters: 

  • x: This parameter is a vector that contains the numeric values which are used in the pie chart.
  • labels: This parameter gives the description to the slices in pie chart.
  • radius: This parameter is used to indicate the radius of the circle of the pie chart.(value between -1 and +1).
  • main: This parameter is represents title of the pie chart.
  • clockwise: This parameter contains the logical value which indicates whether the slices are drawn clockwise or in anti clockwise direction.
  • col: This parameter give colors to the pie in the graph.

Creating a simple pie chart

To create a simple R pie chart: 

  • By using the above parameters, we can draw a pie chart.
  • It can be described by giving simple labels.

Example: 

R




# Create data for the graph.
geeks<- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
 
# Plot the chart.
pie(geeks, labels)


Output:

R - Pie ChartsGeeksforgeeks

R – Pie Charts

Pie chart including the title and colors

To create a color and title pie chart. 

  • Take all parameters which are required to make a R pie chart by giving a title to the chart and adding labels.
  • We can add more features by adding more parameters with more colors to the points.

Example: 

R




# Create data for the graph.
geeks<- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
 
# Plot the chart with title and rainbow
# color pallet.
pie(geeks, labels, main = "City pie chart",
            col = rainbow(length(geeks)))


Output:

R - Pie ChartsGeeksforgeeks

R – Pie Charts

Slice Percentage & Chart Legend

To create chart legend and slice percentage, we can plot by doing the below methods. 

  • There are two more properties of the pie chart: 
    • slice percentage
    • chart legend.
  • We can show the chart in the form of percentages as well as add legends.

Example: 

R




# Create data for the graph.
geeks <- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
 
piepercent<- round(100 * geeks / sum(geeks), 1)
 
# Plot the chart.
pie(geeks, labels = piepercent,
    main = "City pie chart", col = rainbow(length(geeks)))
legend("topright", c("Mumbai", "Pune", "Chennai", "Bangalore"),
                    cex = 0.5, fill = rainbow(length(geeks)))


Output:

R - Pie ChartsGeeksforgeeks

R – Pie Charts

Add pie chart color palettes

With the help of.pal function of the RColorBrewer package in R.

R




Get the library.
library(RColorBrewer)
 
# Create data for the graph.
geeks <- c(23, 56, 20, 63)
labelss <- c("Mumbai", "Pune", "Chennai", "Bangalore")
 
labels<- brewer.pal(length(geeks), "Set2")
 
pie(geeks, labels = labelss)


Output:

R - Pie ChartsGeeksforgeeks

R – Pie Charts

 modify the line type of the borders of the plot we can make use of the lty argument:

R




Get the library.
library(RColorBrewer)
 
# Create data for the graph.
geeks <- c(23, 56, 20, 63)
labelss <- c("Mumbai", "Pune", "Chennai", "Bangalore")
 
labels<- brewer.pal(length(geeks), "Set2")
 
pie(geeks, labels = labelss, col = color, lty = 2)


Output:

R - Pie ChartsGeeksforgeeks

R – Pie Charts

Add shading lines with the density argument. 

R




#Get the library.
library(RColorBrewer)
 
# Create data for the graph.
geeks <- c(23, 56, 20, 63)
labelss <- c("Mumbai", "Pune", "Chennai", "Bangalore")
 
labels<- brewer.pal(length(geeks), "Set2")
 
pie(geeks, labels = labelss,col = color, density = 50, angle = 45)


Output:

R - Pie ChartsGeeksforgeeks

R – Pie Charts

3D Pie Chart

Here we are going to create a 3D Pie chart using plotrix package and then we will use pie3D() function to plot 3D plot.

R




# Get the library.
library(plotrix)
 
# Create data for the graph.
geeks <- c(23, 56, 20, 63)
labels <- c("Mumbai", "Pune", "Chennai", "Bangalore")
 
piepercent<- round(100 * geeks / sum(geeks), 1)
 
# Plot the chart.
pie3D(geeks, labels = piepercent,
    main = "City pie chart", col = rainbow(length(geeks)))
legend("topright", c("Mumbai", "Pune", "Chennai", "Bangalore"),
                    cex = 0.5, fill = rainbow(length(geeks)))


Output:

R - Pie ChartsGeeksforgeeks

R – Pie Charts

 



Previous Article
Next Article

Similar Reads

Create Multiple Pie Charts using ggplot2 in R
Pie Chart, also known as circle chart, is the graphical representation of the relative size or frequency of the data in a circular format. Basically, it helps in visualizing the relative size or frequency of a particular group of data as a part of the whole. This article discusses how multiple pie charts can be created into one frame for consecutiv
3 min read
Pie Charts in R using ggplot2
A Pie Chart or Circle Chart is a circular statistical graphical technique that divides the circle in numeric proportion to represent data as a part of the whole. In Circle Chart the arc length of each slice is proportional to the quantity it represents. Pie charts are very widely used in the business world and the mass media to understand trends. A
2 min read
Side by Side pie charts in R
In this article, we will discuss how to draw Pie Charts side by side in R Programming Language. Method 1: Using base R functions To draw plots side by side par() function is used. Syntax: par(mfrow, mar, mgp, las) Parameters: mfrow- A numeric vector of length 2, which sets the rows and column in which frame has to be divided.mar – A numeric vector
2 min read
How to create a pie chart with percentage labels using ggplot2 in R ?
In this article, we are going to see how to create a pie chart with percentage labels using ggplot2 in R Programming Language. Packages Used The dplyr package in R programming can be used to perform data manipulations and statistics. The package can be downloaded and installed using the following command in R. install.packages("dplyr") The ggplot2
4 min read
Nested Pie Chart in R
In this article, we will discuss how to create a nested pie chart in the R Programming Language. A Pie Chart is a circular plot that can display only one series of data. The area of slices of the pie represents the ratio of the parts of the data that are visualized by that slice. But sometimes we need to show two series of data simultaneously to an
5 min read
How to Create Pie Chart Using Plotly in R
The pie chart is a circular graphical representation of data that is divided into some slices based on the proportion of it present in the dataset. In R programming this pie chart can be drawn using Plot_ly() function which is present in the Plotly package. In this article, we are going to plot a pie chart for the default dataset iris which is pres
3 min read
Pie chart using ggplot2 with specific order and percentage annotations
The ggplot2 package is a powerful and widely used package for graphic visualization. It can be used to provide a lot of aesthetic mappings to the plotted graphs. This package is widely available in R. The package can be downloaded and installed into the working space using the following command : install.packages("ggplot2") A data frame is created
2 min read
Pie chart with facet_wrap in R
In general, Facetting is the process that refers to the split of the output chart(plot) window as grids to display similar charts under the same section. This can be achieved for the plots which are plotted using the ggplot2 package in R Programming Language. Syntax: facet_wrap(facets, nrow, ncol, scales, shrink, dir, strip.position) Where, facets
2 min read
R - Charts and Graphs
R language is mostly used for statistics and data analytics purposes to represent the data graphically in the software. To represent those data graphically, charts and graphs are used in R. R - graphs There are hundreds of charts and graphs present in R. For example, bar plot, box plot, mosaic plot, dot chart, coplot, histogram, pie chart, scatter
6 min read
Adding Colors to Charts in R Programming
R Programming language is mostly used for statistics and data analytics purposes to represent the data graphically in the software. To represent those data graphically, charts and graphs are used in R. Adding Colors to Charts in R Programming There are hundreds of charts and graphs present in R. For example, bar plot, box plot, mosaic plot, dot cha
4 min read
Article Tags :
three90RightbarBannerImg