Rainbow Deals - hours only!Up to 80% off on all courses and bundles.-Close
Know your problem
Know your data
Visualize your data - pie charts
Work with your chart
13. Sort the wedges 
Check yourself


We have a nice pie chart, but we can do a bit better – let's improve it a little.

If your categorical variable is nominal, sort wedges according to their size.

In the first chapter, we had an ordinal categorical variable, which had a natural order. We should respect this order. Here we have a nominal categorical variable, beverage, and it has no natural order. In that case, we often order variable's categories based on values related to this categorical variable.

For example, we'll arrange beverage categories in descending order (from greatest to least) according to each beverage's share of total alcohol consumption. Look at each beverage type's percentages:

  beverage  percent
1  Beer    18.803419
2  Other   1.709402
3  Spirits 23.076923
4  Wine    56.410256

We will arrange them in the following order:

Other < Beer < Spirits < Wine

We should also pay attention to the 'Other' category. This category is often the least interesting, in terms of the story we are telling. It is common to set this category at the end, regardless of its share of the whole. In this case, it is already at the end, so we don't have to change the order.


Order beverage types in descending order, based on the percent variable, and plot the chart again.

To do that use the factor function on the beverage column. Here is the syntax:

factor(column, levels)

Pass the result of this transformation to france_beverages$beverage. This will overwrite the original beverage column in the france_beverages dataset to include sorted categories.

After reordering the beverage variable, plot the chart again by calling the commands given in the code editor.

When you're done, press Run and Check Code.

Stuck? Here's a hint!

You should write:

france_beverages$beverage <- factor(
    c("Other", "Beer", "Spirits", "Wine"))