R Pie Charts

R programming language provides many numbers of libraries to create charts and graphs. A pie-chart is a representation of data as slices of a circle with different colors representing counts or proportions. Each slice is labeled and the numbers corresponding to each slice is also represented in the chart. In R, pie() function is used to create a pie chart. This function takes positive numbers as a vector input. The additional parameters are used to control colors, labels, title, etc.

Syntax:
pie(x, labels, radius, main, col, clockwise)
Here, x is a vector of numeric values used in the pie chart. lables is the description of the slices. radius is the radius of the circle of the pie chart (Value between -1 and +1) main is the title of the chart col represents the color. clockwise is a logical value represents if the slices are drawn clockwise or anti clockwise.
Example: Let's create a simple pie chart using the input vector and labels. The following script will create and save the pie chart in the current R working directory:
# Create data for the graph.
x <- c(25, 32, 51, 11)
labels <- c("Delhi", "Bangalore", "Bhopal", "Mumbai")
# Give the chart file a name.
png(file = "city.jpg")
# Plot the chart.
pie(x,labels)
# Save the file.
dev.off()
Output: 
pie chart in the current R 

Pie Chart Title and Color We can add more parameters to the pie function. Here, we will use main and col parameter.

Example:
# Create data for the graph.
x <- c(25, 32, 51, 11)
labels <- c("Delhi", "Bangalore", "Bhopal", "Mumbai")
# Give the chart file a name.
png(file = "city_title_colours.jpg")
# Plot the chart with title and rainbow color pallet.
pie(x, labels, main = "City pie chart", col = rainbow(length(x)))
# Save the file.
dev.off()
Output:

city pie function 

Slice Percentage and Chart Legend We can also add slice percentage and a chart legend by creating additional chart variables.

Example:
# Create data for the graph.
x <- c(25, 32, 51, 11)
labels <- c("Delhi", "Bangalore", "Bhopal", "Mumbai")
piepercent<- round(100*x/sum(x), 1)
# Give the chart file a name.
png(file = "city_percentage_legends.jpg")
# Plot the chart.
pie(x, labels = piepercent, main = "City pie chart",col = rainbow(length(x)))
legend("topright", c("Delhi", "Bangalore", "Bhopal", "Mumbai"), cex = 0.8,
       fill = rainbow(length(x)))
# Save the file.
dev.off() 
Output:
Slice Percentage and Chart Legend

3D pie Chart R provides additional package to draw a pie chart in 3 dimensions. The name of the package is plotrix. This package has a function pie3D() that is used for 3D design.

Example:
# install the pacakage
install.packages("plotrix")
# Get the library.
library("plotrix")
# Create data for the graph.
x <- c(25, 32, 51, 11)
lbl <- c("Delhi", "Bangalore", "Bhopal", "Mumbai")
# Give the chart file a name.
png(file = "3d_pie_chart.jpg")
# Plot the chart.
pie3D(x,labels = lbl,explode = 0.1, main = "Pie Chart of Countries ")
# Save the file.
dev.off()
Output: 

3D pie Chart