# Pie Charts in R

Pie charts are circular graphs that show how different parts contribute to a whole. Pie charts are one of the most common visual representations of data. They use sectors or slices to represent the data visually.

In addition, Pie charts are used to show the percentages of each component that combine to make a whole. They are also used in data science, business intelligence, data analytics, and many other fields.

### Pie Charts in R

Programmers can use R language to create Pie charts to visualize, display and understand the proportions in large amounts of data. The `pie()` function in R can be used for creating a pie chart.

## `pie() in R`

The syntax of the pie() function, used to create pie charts in R, is as follows:

``pie(x, labels, radius, main, col, clockwise)``

### Parameters

The following is a description of the parameters used in the `pie()` function

• `x`: a vector containing the numerical values ​​used to plot the pie chart.
• `labels`: Used to name each slice of the pie chart.
• `radius`: Specifies the radius of the circle of the pie chart. (a value between -1 and +1).
• `main`: Indicates the name of the chart.
• `col`: Indicates the color given to each slice of the chart. This can be a vector containing specified colors.
• `clockwise`: a boolean indicating whether slices are drawn clockwise (clockwise = TRUE) or counterclockwise (clockwise = FALSE). By default, clockwise is FALSE.

However, not all the parameters are mandatory. A variety of pie charts can be created using a combination of these parameters.

## Explanation

The examples that follow demonstrate and explain how to use the `pie()` function to make pie charts in R.

## Pie Charts Using Only Input Vector

The easiest way to create a pie chart in R is by only passing vector `x` to the `pie()` function as an argument value.

```# Create data for the pie chart
data <- c(78, 22, 40, 67, 45)
# Create the pie chart.
pie(data)
```
• Line#2: Declares the input vector as `data`.
• Line#4: Invoking the `pie()` function which creates the pie chart.

### Output

The following pie chart is generated.

## Pie Chart With Specified Labels

By default, R assigns 1, 2, 3, …, n as labels to slices in the pie chart. However, users can specify different labels by passing another vector, containing the labels, as the labels parameter in the `pie()` function.

```# Create data for the pie chart
data <- c(78, 22, 40, 67, 45)
# Create a vector with all the labels
labels <- c( "Slice A", "Slice B", "Slice C", "Slice D", "Slice E" )
# Create the pie chart.
pie(data, labels)
```
• Line#4: Defines the labels vector which contains the labels of each section of the pie chart
• Line#6: Invokes the `pie()` function this time with the labels parameter which assigns the specified label to each section of the pie chart.

### Output

The pie chart returned is given below. Note that the labels are assigned in such a way that the label and data have the same index from the vector. For example, `data (78)` is assigned the label0 (Label A), and so on.

## Pie Chart With Title

A title can be assigned to any pie chart using the main parameter in the `pie()` function.

```# Create data for the pie chart
data <- c(78, 22, 40, 67, 45)
# Create a vector with all the labels
labels <- c( "Slice A", "Slice B", "Slice C", "Slice D", "Slice E" )
# Create the pie chart.
pie(data, labels, main = "My Pie Chart")
```
• Line#6: Invoks pie function including the main parameter which is set to My Pie Chart. This assigns a title to the pie chart.

### Output

The output pie chart is attached below. The specified title specified in the main parameter appears on top of the pie chart.

## Pie Chart With Specified Color

```# Create data for the pie chart
data <- c(78, 22, 40, 67, 45)
# Create a vector with all the labels
labels <- c( "Slice A", "Slice B", "Slice C", "Slice D", "Slice E" )
# Create a vector containing colors
colors <- c("red", "green", "blue", "black", "violet")
# Create the pie chart.
pie(data, labels, main = "My Pie Chart", col = colors)
```
• Line#6: defines the colors for the pie chart which are stored in a vector.

### Output

Following is the resulting pie chart. Colors are assigned to the slices the same way as labels. For example, `colors` is given to the slice representing `data`.

## Three-Dimensional Pie Chart

A 3D pie chart can be created using the `pie3D()` function in R which uses the same parameters as the `pie()` function. The `pie3D()` function is included in the Plotrix library in R.

```Library(plotrix)
# Create data for the pie chart
data <- c(78, 22, 40, 67, 45)
# Create a vector with all the labels
labels <- c( "Slice A", "Slice B", "Slice C", "Slice D", "Slice E" )
# Create a vector containing colors
colors <- c("red", "green", "blue", "black", "violet")
# Create the pie chart.
pie3D(data, labels = labels, main = "My Pie Chart", col = colors)
```
• Line#1: Import the `plotrix` library into the program. This makes the `pie3D()` function available to the R program.
• Line#9: calls the `pie3D()` function which creates a 3D pie chart using the parameters.

### Output

Following is the resulting 3D pie chart.