Pie Charts In R

Hi there. I have played around with making pie charts in the statistical programming language R. The pie charts shown here are from the base R package and from the ggplot2 data visualization package.

Table Of Contents


  • http://www.sthda.com/english/wiki/ggplot2-pie-chart-quick-start-guide-r-software-and-data-visualization
  • http://stackoverflow.com/questions/20442693/how-to-use-ggplot2-to-generate-a-pie-graph
  • http://stackoverflow.com/questions/41338757/adding-percentage-labels-on-pie-chart-in-r
  • http://stackoverflow.com/questions/7145826/how-to-format-a-number-as-percentage-in-r
  • R Graphics Cookbook by Winston Chang (O’Reilly Media, 2012)

The Sample Data: Favourite Food Survey

For the pie chart, I am creating a fake sample dataset. This data table will contain favourite food choices and their counts from a survey.

The food choices are Pizza,  Pasta,  Sushi, and Caesar Salad.

You can check the contents of the table by typing out table and/or using the str() command in R.

The column names in the table can be changed as well by using colnames().

One could compute the total number of people in this food survey. (Add from top to bottom of second column in table.)

A Pie Chart Using Base R Graphics

In base R, the pie() function is used to create a pie chart. The pie() function requires many arguments. In this example, I use x, labels, col (colours), and main (title).

This pie chart from base R looks okay but I rather use ggplot2 for this. Also, I have not added in percentage labels here. One could find out how to do it somewhere online.

Using ggplot2 To Create A Pie Chart

The ggplot2 package in R is very good for data visuals. ggplot2 allows R users to create pie charts, bar graphs, scatter plots, regression lines and more.

Load the ggplot2 package using this code below.

In ggplot2, the pie chart follows from the bar graph. This next lines of codes shows why a bar graph is not that good for displaying this data.

To create the actual pie chart more code is needed on top of the existing code for the bar graph. The main line of code is coord_polar(theta = “y”, start = 0) which will convert the bar graph into a pie (circular) graph.

From the previous code,scale_fill_manual() allows for colour choices in the pieces of the pie chart. The labs() function allows for customization of the title and labels. The theme() functions allows for customization of the appearance of the title.

Adding Percentage Labels To The Pie Chart Using ggplot2

The pie chart above is very nice but it could use percentage labels. Adding the percentage labels takes a bit of work here but it is manageable.

The dplyr package for data manipulation and data wrangling is loaded into R.

The next lines of code will convert the values under the Food column as factors and add label positions based on cumulative counts and midpoints. These labels will come out as percentages on the pie chart. The reference used is from http://stackoverflow.com/questions/41338757/adding-percentage-labels-on-pie-chart-in-r.

Now the ggplot() command along with the corresponding code can be used. The line with geom_text() enables the percentage labels onto the pie chart.

Adding Percentage And Count Labels To The Pie Chart Using ggplot2

Percentages are sometimes misleading. It is better to add counts with them. The code here is very similar to the previous code but the line with paste0() is modified such that the counts are included.

The paste0() function is R’s version of concatenation. We can combine values and strings together with paste0().

Running the ggplot() function with the add-on code gives us a nice pie chart.

2 thoughts on “Pie Charts In R

    1. dkmathstats Post author

      Thank you for your comment. I am glad you liked this post.


Leave a Reply