Donut plot

definition - mistake - related - code

Definition


A donut plot is a pie chart with a hole in the center. It’s a way to show the distribution of a categorical variable, but it can be a little bit tricky to read. It’s not recommended to use it if you have a large number of categories.

It’s similar to a pie chart, but it’s easier to compare the size of the slices. It’s also possible to add a number inside each slice to show the exact proportion.

# load library
library(ggplot2)

# Create test data.
data <- data.frame(
  category=c("A", "B", "C"),
  count=c(10, 60, 30)
)

# Compute percentages
data$fraction <- data$count / sum(data$count)

# Compute the cumulative percentages (top of each rectangle)
data$ymax <- cumsum(data$fraction)

# Compute the bottom of each rectangle
data$ymin <- c(0, head(data$ymax, n=-1))

# Compute label position
data$labelPosition <- (data$ymax + data$ymin) / 2

# Compute a good label
data$label <- paste0(data$category, "\n value: ", data$count)

# Make the plot
ggplot(data, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=category)) +
  geom_rect() +
  geom_label( x=3.5, aes(y=labelPosition, label=label), size=6) +
  scale_fill_brewer(palette=4) +
  coord_polar(theta="y") +
  xlim(c(2, 4)) +
  theme_void() +
  theme(legend.position = "none")

What for?


Donut plots are used to visualize the distribution of categorical data. They are particularly useful when you want to highlight the proportions of different categories within a dataset. The central hole of the donut plot can be used to display additional information, such as total values or percentages, making it easier to interpret the data at a glance.

Compared to pie charts, donut plots provide a more effective visual comparison of category sizes due to the space in the center, which allows for a better understanding of the proportions. However, they are best suited for datasets with a limited number of categories, as too many slices can make the plot difficult to read.

Key purposes of using a donut plot include:

  • Highlighting the proportional distribution of categories within a dataset.
  • Comparing the relative sizes of different categories.
  • Displaying additional information in the center of the plot for enhanced data interpretation.
  • Providing a visually appealing alternative to pie charts for presentations and reports.

Variation


The donut plot is a variation of different charts. Here are some examples:

  • Pie chart: The most basic form of a donut plot. It’s a circle divided into slices to illustrate numerical proportions.
  • Bar chart: A bar chart is a chart with rectangular bars with lengths proportional to the values they represent.

Build your own


The R, Python, React and D3 graph galleries are 4 websites providing hundreds of chart example, always providing the reproducible code. Click the button below to see how to build the chart you need with your favorite programing language.

R graph gallery Python gallery React gallery D3 gallery

Dataviz decision tree

Data To Viz is a comprehensive classification of chart types organized by data input format. Get a high-resolution version of our decision tree delivered to your inbox now!


High Resolution Poster
 

A work by Yan Holtz for data-to-viz.com