We have seen previously how to make a pie chart and a donut chart based on a single data series. Making a multiple pie/donut chart which allows for comparison of 2+ data series is nothing complicated. If you know already how to make a multiple stacked bar plot, then you know almost everything about multiple pie/donut chart. The principle is as follows: start by making a multiple stacked bar plot, then transform it by placing it into a polar coordinate system.

Before going any further, if you are not so familiar with pie/donut plots or stacked bars, have a quick look at these pages:

Here we will plot the distributions (response variable percentage) of blood types (predictor variable type) in Norway and in the UK (predictor variable country).

The dataframe for this tutorial is as follows:

# dataframe
df <- data.frame(type, country, percentage)
# structure of the dataframe
str(df)
## 'data.frame':    16 obs. of  3 variables:
##  $ type      : Factor w/ 8 levels "A-","A+","AB-",..: 8 2 6 4 7 1 5 3 8 2 ...
##  $ country   : Factor w/ 2 levels "Norway","UK": 1 1 1 1 1 1 1 1 2 2 ...
##  $ percentage: num  33.2 41.6 6.8 3.4 5.8 7.4 1.2 0.6 37 31 ...



We start by making the stacked bar showing both data series. The function to use for drawing the bars is geom_col(). We must map the variables with the function aes() which will look more or less like this: aes(country, percentage, type). Our plan is to:

The code for the plot is as follows:

ggplot(df, aes(x = country, y = percentage, fill = type)) +
  geom_col() +
  scale_fill_viridis_d()

NB: here we used scale_fill_viridis_d() to change the color palette.

From here, we place the plot in a polar coordinate system with coord_polar("y"):

ggplot(df, aes(x = country, y = percentage, fill = type)) +
  geom_col() +
  scale_fill_viridis_d() +
  coord_polar("y")



Note that the legend does not help determine which country is represented by the pie. Is Norway inside or outside? We can solve the problem by adding color=country in aes() to highlight the elements based on the variable country. And with scale_color_manual(values=c("red", "blue")) and size=.8, we change the thickness and colors of the frames around the elements:

ggplot(df, aes(x = country, y = percentage, fill = type, color = country)) +
  geom_col(size=.8) +
  scale_color_manual(values=c("red", "blue")) +
  scale_fill_viridis_d() +
  coord_polar("y")



If you prefer to have a multiple donut chart instead of a multiple pie chart, the only thing to do is to expand the original X-axis towards the left. Since the variable country used on the X-axis is discrete, we must add an empty, discrete element to the left of the countries. We use scale_x_discrete(limits= ) to do so and indicate the order of the elements, starting with the empty element " ", then "Norway" and "UK":

ggplot(df, aes(x = country, y = percentage, fill = type)) +
  geom_col() +
  scale_x_discrete(limits = c(" ", "Norway","UK")) +
  scale_fill_viridis_d() +
  coord_polar("y")



Finally, we may use theme_void() to get rid of the useless elements of the theme such as background and axes:

ggplot(df, aes(x = country, y = percentage, fill = type)) +
  geom_col() +
  scale_x_discrete(limits = c(" ", "Norway","UK")) +
  scale_fill_viridis_d() +
  coord_polar("y") +
  theme_void()



Adding plot title, axis titles, ticks, labels and other essential elements

In this section, you will learn how to set/modify all the necessary elements that make a plot complete and comprehensible. Such elements are: