Create Nested Pie chart using ggplot2 package
To create a nested pie chart in the R Language using the ggplot2 package, we first create a rectangular plot and then use the coord_polar() function to turn it into a nested pie/donut chart. The coord_polar() function converts the cartesian coordinates system into a polar coordinate system in ggplot2.
To install & import the ggplot2 package in the R console, the syntax is given:
install.package('ggplot2') library(ggplot2)
Syntax:
plot + coord_polar( theta, start, direction, clip )
where,
- theta: determines the angle
- start: determines the setting offset
- direction: determines the direction of transformation i.e. x or y
- clip: determines whether drawing should be clipped or not
Example:
Here, is a nested pie chart made using the ggplot2 package.
R
# load library ggplot2 library (ggplot2) # create sample data frame sample_data <- data.frame (group= c ( 'Group1' , 'Group2' , 'Group1' , 'Group2' , 'Group1' , 'Group2' ), name= c ( 'name1' , 'name1' , 'name2' , 'name2' , 'name3' , 'name3' ), value= c (25,30,35,20,40,50)) # create nested pie chart using ggplot ggplot (sample_data, aes (x = factor (group), y = value, fill = factor (name))) + geom_col () + scale_x_discrete (limits = c ( "Group1" , "Group2" )) + coord_polar ( "y" ) |
Output:
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 analyze the data better. We can do this by creating a nested pie chart which is a pie chart nested inside a donut chart.
Contact Us