Chapter 9 Pie Chart

The information below shows the population (in millions) of the top ten most populous cities in the US in 2019. (Source: moving.com)

  • New York - 8.60
  • Los Angeles - 4.06
  • Chicago - 2.68
  • Houston - 2.40
  • Phoenix - 1.71
  • Philadelphia - 1.58
  • San Antonio - 1.57
  • San Diego - 1.45
  • Dallas - 1.40
  • San Jose - 1.03

Using the given information, let us create a data frame and call it top_ten.

##          Cities Population
## 1      New York       8.60
## 2   Los Angeles       4.06
## 3       Chicago       2.68
## 4       Houston       2.40
## 5       Phoenix       2.71
## 6  Philadelphia       1.58
## 7   San Antonio       1.57
## 8     San Diego       1.45
## 9        Dallas       1.40
## 10     San Jose       1.03

9.1 Basic R Pie Chart

To draw a pie chart, use the function pie(quantitative_variable)

The pie chart is drawn in the clockwise direction from the given data. Different color slices are added automatically. The color default are the pastels. However, the graph does not tell us much. Let us add labels to each slice using the argument labels = variable.

The graph is now more understandable. The pie chart is drawn according to the data frame entries going in the counterclockwise motion. If you want to draw the pie chart in ascending or descending order, you will want to rearrange the dataset and rename the object first. Then draw the pie chart of the new object.

Let us rearrange the dataset in descending order and name it top_ten_descend. Then draw and add a title to the pie chart.

Enhancements in Basic R (Optional)

Here are some enhancements for your pie chart.

  • Use the argument, col = rainbow(length(vector)) to make the color of each pie more vibrant
  • Add percentages to the labels

To add percentages to the label, calculate the percentage of each entry as compared to the total. Then round the result to the nearest whole number in order to avoid decimals.

9.2 Ggplot2 Pie Chart

Ggplot2 does not have a specific geometric function to build pie charts. The key is to go back to geom_bar( ) and add the polar coordinate function to make the graph circular. There are ways to enhance the pie chart but we will keep it to a minimum here. Leave the x in aesthetics blank with just the quotation marks. The pie chart will be drawn in the counterclockwise motion, alphabetically.

As you can see, the pie chart has many extraneous background information. Let us make some changes and enhance the pie chart a little bit.

  • Remove the numeric labels and the polar grid by appending the function, theme_void( ).
  • Add borders to each slice by adding the argument color into geom_bar( ).
  • Draw the pie chart in the clockwise motion by adding a negative sign to the target vector.

Let us incorporate the changes, add a title and look at the resulting pie chart.

Reordering the pie chart in ggplot2 is similar to reordering bar graphs. For the argument fill, use the function reorder( ).