Youtube Channel: https://www.youtube.com/c/TechAnswers88

Simple method to create a doughnut chart.

Video link https://youtu.be/CA6kLd-ao1Y

Packages used in this example

library(dplyr) # to manipulate our data
library(ggplot2)  # to actually plot our charts

Create a sample dataset

We will create some fictitious data for countries, with their population.

# Create our dataset
df <- data.frame(country = c('A','B','C','D','E'),
                 popMil     = c(200,158, 3000, 354, 453)
                 
                )

df

Transform our sample dataset

Create a column called perc which will show the percentage.

Create a column called ymax which will show the cumulative sum of the perc column.

Create a column called ymin which will show the previous row’s ymax value.

# Transform our dataset
# Create two more columns

df2 <- df%>%
      dplyr::mutate( perc = popMil/sum(popMil)
                    , ymax = cumsum(perc)
                    , ymin = ifelse(is.na(lag(ymax)), 0, lag(ymax)))

                    
df2                   

First chart

pl <- ggplot(data = df2 )
pl <- pl + geom_rect(aes(ymin = ymin, ymax = ymax,fill = country,
                         xmin= 3, xmax = 4))
pl <- pl + coord_polar(theta="y",  start=1)
pl <- pl +  theme_void() 
pl <- pl + xlim(c(-1, 4))
pl <- pl + geom_text(aes(x = 3.5, y = perc ,label = paste0(country,"\n",round(perc*100,1), "%"))
                         , position = position_stack(vjust = 0.5), size = 2.5)
pl <- pl + labs(title    = "Doughnut chart")
pl <- pl + labs(subtitle = "Using ggplot")

pl

# Change the start
pl <- ggplot(data = df2 )
pl <- pl + geom_rect(aes(ymin = ymin, ymax = ymax,fill = country,
                         xmin= 3, xmax = 4))
pl <- pl + coord_polar(theta="y",  start=2)
pl <- pl +  theme_void() 
pl <- pl + xlim(c(-1, 4))
pl <- pl + geom_text(aes(x = 3.5, y = perc ,label = paste0(country,"\n",round(perc*100,1), "%"))
                         , position = position_stack(vjust = 0.5), size = 2.5)
pl <- pl + labs(title    = "Doughnut chart")
pl <- pl + labs(subtitle = "Using ggplot")

pl

Second chart

pl <- ggplot(data = df2 )
pl <- pl + geom_rect(aes(ymin = ymin, ymax = ymax,fill = country,
                         xmin= 2, xmax = 4))
pl <- pl + coord_polar(theta="y")
pl <- pl +  theme_void() 
pl <- pl + xlim(c(-1, 4))
pl <- pl + geom_text(aes(x = 3.5, y = perc ,label = paste0(country,"\n",round(perc*100,1), "%"))
                         , position = position_stack(vjust = 0.5), size = 2.5)
pl <- pl + labs(title    = "Doughnut chart")
pl <- pl + labs(subtitle = "Using ggplot")
pl

Third chart

pl <- ggplot(data = df2 )
pl <- pl + geom_rect(aes(ymin = ymin, ymax = ymax,fill = country,
                         xmin= 1, xmax = 4))
pl <- pl + coord_polar(theta="y")
pl <- pl +  theme_void() 
pl <- pl + xlim(c(-1, 4))
pl <- pl + geom_text(aes(x = 3.5, y = perc ,label = paste0(country,"\n",round(perc*100,1), "%"))
                         , position = position_stack(vjust = 0.5), size = 2.5)
pl <- pl + labs(title    = "Doughnut chart")
pl <- pl + labs(subtitle = "Using ggplot")
pl

Fourth chart

pl <- ggplot(data = df2 )
pl <- pl + geom_rect(aes(ymin = ymin, ymax = ymax,fill = country,
                         xmin= 0, xmax = 4))
pl <- pl + coord_polar(theta="y")
pl <- pl +  theme_void() 
pl <- pl + xlim(c(-1, 4))
pl <- pl + geom_text(aes(x = 3.5, y = perc ,label = paste0(country,"\n",round(perc*100,1), "%"))
                         , position = position_stack(vjust = 0.5), size = 2.5)
pl <- pl + labs(title    = "Doughnut chart")
pl <- pl + labs(subtitle = "Using ggplot")
pl

Fifth chart

pl <- ggplot(data = df2 )
pl <- pl + geom_rect(aes(ymin = ymin, ymax = ymax,fill = country,
                         xmin= 0, xmax = 4), colour = "black")
pl <- pl + coord_polar(theta="y")
pl <- pl +  theme_void() 
pl <- pl +  xlim(c(0, 4))
pl <- pl + geom_text(aes(x = 3.5, y = perc ,label = paste0(country,"\n",round(perc*100,1), "%"))
                         , position = position_stack(vjust = 0.5), size = 2.5)
pl <- pl + labs(title    = "Piechart")
pl <- pl + labs(subtitle = "Using ggplot")
pl