Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Know your problem
Know your data
Visualize your data - pie charts
11. Draw the pie chart
Work with your chart
Check yourself

Instruction

Now you know everything necessary to plot a pie chart, so let's do that.

First, we will add the polar coordinate system to our plot. We do this by adding the following command to ggplot:

coord_polar(theta = "y")

Setting this coordinate system changes how R interprets some of the other commands. The whole syntax will look like this:

ggplot(data = dataset, aes(x = "", y = numerical_variable, fill = categorical_variable)) + coord_polar(theta = "y") + geom_col(width = 1)

First, look at the second part of this code; we set the polar coordinates. Next, look at the mapping arguments in aes(). Here, we remove the x argument by setting it to "". We also set the y argument as the dataset's numerical variable and the fill argument as the dataset's categorical variable. Lastly we added geom_col(width = 1). In the polar coordinate system, this will plot wedges for each category. (When we use the familiar Cartesian system, which has two perpendicular axes, this command sets the bar size.)

Exercise

Let's plot our data on a pie chart.

To do that, use the above syntax for the ggplot() function. Set france_beverages as the dataset, the percent column as y and the beverage column as fill. Pass this command to the object pie.

Stuck? Here's a hint!

You should write:

pie <- ggplot(data = france_beverages, aes(x = "", y = percent, fill = beverage)) + 
  coord_polar(theta = "y") + 
  geom_col(width = 1)