Hello and welcome! In today's lesson, we will venture into the world of Pie Charts in R. Pie charts are an elegant way to showcase proportions in relation to the total — for instance, illustrating populations by continents. Today, you will learn how to create and customize pie charts in R using the ggplot2 package.
Pie charts are a graphical representation in which a circle, or "the pie," is divided into individual "slices," each representing a category. Picture a pizza: each slice represents a distinct fruit type — apples, oranges, bananas, and grapes. The size of each piece represents the quantity of the fruit. We'll be using the versatile R data visualization library, ggplot2, to generate interactive pie charts.
Let's create a pie chart that depicts the favorite fruits of a group of individuals. This could represent consumer preferences in a market study. Here's the dataset:
Let's start creating a pie chart for this dataset.
Pie Chart Components:
- Slices: Portions reflecting each fruit preference.
- Labels: Names of each type of fruit.
In crafting the Pie Chart with the ggplot2 library, we can create an informative and attractive pie chart. We'll first generate a bar plot and then use coord_polar() to convert it to a pie chart.
Pie charts are essentially bar plots transformed into a circular format. ggplot2 doesn't have a direct geom function for pie charts, so we craft a bar plot first and then transform it. Using coord_polar() is a convenient way to convert bar plots into pie charts because it translates the Cartesian coordinates of a bar plot into polar coordinates, thus giving us the circular shape of a pie chart.
This chart helps us visualize how preferences are divided—similar to illustrating market shares in business.



