• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

Data Viz with Python and R

Learn to Make Plots in Python and R

  • Home
  • Python Viz
  • Seaborn
  • Altair
  • R Viz
  • ggplot2
  • About
    • Privacy Policy
  • Show Search
Hide Search

How to make Donut plot with ggplot2

datavizpyr · September 28, 2024 ·

In this tutorial, we will learn how to make a donut plot in R using ggplot2. Donut plot is nothing but a bar plot, reshaped into pie-chart, and then adding a hole at the center to make it look like a donut. We will learn these in step by step manner to understand making Donut plot with ggplot2.

Let us load tidyverse.

library(tidyverse)
theme_set(theme_bw(16))

Let us create some sample data to make a Donut plot.

data <- tibble(
  category = c("Google", "Meta", "Apple", "Nvidia"),
  value = c(100, 120, 50, 150)
)

data

# A tibble: 4 × 2
  category value
  <chr>    <dbl>
1 Google     100
2 Meta       120
3 Apple       50
4 Nvidia     150

We will using proportion to make a Donut plot, so let us compute the proportion of each group of interest.

# Calculate the percentage and cumulative percentage
data <- data |>
  mutate(
    percentage = value / sum(value)
  )
data

# A tibble: 4 × 3
  category value percentage
  <chr>    <dbl>      <dbl>
1 Google     100      0.238
2 Meta       120      0.286
3 Apple       50      0.119
4 Nvidia     150      0.357

Making a Donut plot: Step 1

Now we have the data needed in hand, let us make bar plot using geom_bar() as the first step.

data |>
  ggplot(aes(x = 2, y = percentage, fill = category)) +
  geom_bar(stat = "identity", width = 1, color = "white")
Making Donut plot: Step 1
Making Donut plot: Step 1 – make Barplot

Making a Donut plot: Step 2

In second step, we use coord_polar() in ggplot2 to reshape the bars in barplot to pie-chart.

data |>
  ggplot(aes(x = 2, 
             y = percentage, 
             fill = category)) +
  geom_bar(stat = "identity",
           width = 1, 
           color = "white") +
  coord_polar(theta = "y") 
Making Donut plot: Step 2
Making Donut plot: Step 2

Making a Donut plot: Step 3

In the third step, we make a hole in the middle of the pie chart to make it look like a donut.

data |>
  ggplot(aes(x = 2,
             y = percentage,
             fill = category)) +
  geom_bar(stat = "identity", 
           width = 1, 
           color = "white") +
  coord_polar(theta = "y") +
  xlim(0.5, 2.5)   # Set limits to create a hole in the center

Making Donut plot: Step 3 - Make a hole in Pie Chart
Making Donut plot: Step 3 – Make a hole in Pie Chart

And finally, we can remove any thematic elements, just to keep only the Donut plot.

Making a Donut plot: Step 4

data |>
  ggplot(aes(x = 2, 
             y = percentage,
             fill = category)) +
  geom_bar(stat = "identity", 
           width = 1, 
           color = "white") +
  coord_polar(theta = "y") +
  xlim(0.5, 2.5) +  # Set limits to create a hole in the center
  theme_void() 
Making a Donut plot: Step 4
Making a Donut plot: Step 4

Finally we can add some title and other customization if needed just like any other plot with ggplot2.

data |>
  ggplot(aes(x = 2, y = percentage, fill = category)) +
  geom_bar(stat = "identity", width = 1, color = "white") +
  coord_polar(theta = "y") +
  xlim(0.5, 2.5) +  
  theme_void() +
  labs(title="Donut plot example")
ggsave("how_to_make_donut_plot.png")
How to make a Donut plot
How to make a Donut plot

Related posts:

Customizing Mean mark to boxplot with ggplot2How To Show Mean Value in Boxplots with ggplot2? Scatterplot with marginal multi-histogram with ggExtraHow To Make Scatterplot with Marginal Histograms in R? ggforce geom_circle(): Annotate with a circleHow To Annotate a Plot with Circle in R Default ThumbnailHow to Make Axis Text Bold in ggplot2

Filed Under: ggplot2, ggplot2 coord_polar(), R Tagged With: Donut plot with ggplot2

Primary Sidebar

Tags

Altair barplot Boxplot boxplot python boxplot with jiitered text labels Bubble Plot Color Palette Countplot Density Plot Facet Plot gganimate ggplot2 ggplot2 Boxplot ggplot2 error ggplot boxplot ggridges ggtext element_markdown() Grouped Barplot R heatmap heatmaps Histogram Histograms Horizontal boxplot Python lollipop plot Maps Matplotlib Pandas patchwork pheatmap Pyhon Python R RColorBrewer reorder boxplot ggplot Ridgeline plot Scatter Plot Scatter Plot Altair Seaborn Seaborn Boxplot Stock Price Over Time Stripplot UpSetR Violinplot Violin Plot World Map ggplot2

Buy Me a Coffee

Copyright © 2025 · Daily Dish Pro on Genesis Framework · WordPress · Log in

Go to mobile version