How to Create and Publish a Nested Pie Chart in R with the Plotly Package (aka a Pie Chart within a Donut Chart)

One of the major strengths of R and RStudio is the vast collection of packages to accomplish all sorts of specific tasks and larger projects. Whether you are trying to conduct advanced statistical analysis and machine learning, scrape data from the web, or create publication-ready graphics and visualizations, chances are R has a package available via the Comprehensive R Archive Network (CRAN). If you need help installing R or RStudio on your computer, check out our tutorial and also check out our guide to running basic statistical analyses in R.

After you have found an interesting dataset for a personal or professional project and wrangled it into a manageable data.frame in R, you are ready to create some informative visualizations. R has several excellent packages available for creating graphics, including ggplot2 and plotly, as well as the extensive base R graphics functions that can be called with the plot() function.

However, much of the base R graphics interface has some limitations both in its capabilities for customization and in its coding syntax, which can be difficult for first time and experienced R users alike.

This article provides a tutorial of plotly in R to create a basic pie chart, a donut chart, and a nested pie chart, which is a pie chart within a donut chart.

These are just a few really cool and visually appealing ways to display categorical variables separately and together, and they are great examples of the beautiful default colors in plotly.

Plotly Basic Pie Chart

First, a basic pie chart in plotly is very easy to create and takes on generally high quality, vibrant colors with the default settings. Pie charts are great for displaying percentages for categorical (aka discrete) data with a relatively small amount of categories (2 – 8). Any more than 8 or 10 categories and a pie chart can start to become cluttered and small percentages can show up as only a sliver of color.

Please note that these are simulated data about renewable and non-renewable energy sources in the United States and European Union, and the do not reflect actual data sources as this is a tutorial to show the use of plotly’s pie and donut chart functionalities.

#creating simulated data
data <- data.frame(c('Solar Panels', 'Fossil Fuels', 'Wind Turbines'), c(15,80,5), c(40, 40, 20))
colnames(data) <- c('Energy Source', 'United States', 'European Union')

#creating pie chart
plot1 <- plot_ly(data) %>%
  add_pie(data, labels = ~`Energy Source`, values = ~`European Union`, 
          domain = list(
            x = c(0.15, 0.85),
            y = c(0.15, 0.85)),
          sort = F)

#printing plot

Plotly Donut Chart

Similar to a pie chart, a donut chart is also well-suited for displaying percentage data for categorical variables with a limited number of groups. It is also great for website images in articles or blogs, because you can put content in the center by editing it in Adobe Illustrator or another graphics editing software program. The size of the open circle in the center can also be change by modifying the hole = 0.7 option to smaller or larger values.

#creating donut chart
plot2 <- plot_ly(data) %>%
  add_pie(labels = ~`Energy Source`, values = ~`United States`, type = 'pie', hole = 0.7, sort = F)

#printing plot

Plotly Pie Chart in a Donut Chart (aka a Nested Pie Chart)

The best of both worlds, a pie chart within a donut chart can display two categorical variables together in one easy-to-understand circular chart. Much like the charts described above, this nested pie chart is best created with only a few groups for the inner and outer chart, as it can become cluttered and overwhelming to include many labels in both charts.

This code also has a few additions, including the marker = list() command, which can be used to set the width of the outline line, the color of the pie segments, and much more.

This example shows the use of orca() for publication-ready graphics, which can be installed in the command line (see instructions here: and generates high-quality vector images of plotly plots that are saved to the current working directory.

#creating pie chart nested within donut chart
plot3 <- plot_ly(data) %>%
  add_pie(labels = ~`Energy Source`, values = ~`United States`, type = 'pie', marker = list(line = list(width = 2)),hole = 0.7, sort = F) %>%
  add_pie(data, labels = ~`Energy Source`, values = ~`European Union`, 
          domain = list(
            x = c(0.15, 0.85),
            y = c(0.15, 0.85)),
          sort = F)

#printing plot

#export plot as vector .svg file using orca
orca(plot3, file = "plot3.svg")

For a deeper dive into the available customization in plotly and for its many options for pie charts in particular, the plotly website ( has an excellent guide to all functions and options in the package.

As always, make sure to check out other articles on our site, including the top 10 essential packages for data science in R, tips to succeed in a career in data science, and ways that COVID-19 has imapcted the data science profession.

If you want to stay up to date on the latest from Data Science for Anyone, don’t forget to follow us on Twitter, Instagram and Pinterest!

Leave a Reply