plotly Pie & Donut Chart in R (4 Examples)

 

Hi! This tutorial will show you how to make plotly pie and donut charts in the R programming language.

First, though, here is a quick overview:

Let’s jump into the R code!

 

Install and Load plotly & dplyr

If you do not have plotly and dplyr already installed in your R programming environment, then in either R Studio or any other IDE that can run the R programming language, run the lines of code to install and load plotly and dplyr; otherwise, you can skip to the next section of this tutorial:

# install plotly and dplyr
install.packages(c("plotly", "dplyr"))
 
# load plotly and dplyr
library(plotly)
library(dplyr)

The R dplyr library is the main library for data manipulation and analysis in R programming. It also enables the use of the pipe operator to run several lines of code together as one chunk.
 

Sample Dataset

For this tutorial, we will make use of the popular diamonds dataset, which comes pre-loaded inside R Studio.

We will use dplyr to group the diamonds by cut and calculate the average price, average depth, average table, and total carat for each diamond cut:

dmds_data <- diamonds |> 
  group_by(cut) |> 
  summarize(avg_price = mean(price),
            avg_depth = mean(depth),
            avg_table = mean(table),
            total_carat = sum(carat)) |> 
  ungroup()

You can take a look at the wrangled data:

print(dmds_data)
 
#     cut    avg_price avg_depth avg_table total_carat
# 1   Fair    4359.      64.0      59.1      1684.
# 2   Good    3929.      62.4      58.7      4166.
# 3 Very Good 3982.      61.8      58.0      9743.
# 4  Premium  4584.      61.3      58.7      12301.
# 5  Ideal    3458.      61.7      56.0      15147.

Now we are ready to visualize the data in both pie and donut charts.
 

Example 1: Build Basic Pie Chart

In this first example, we will build a basic pie chart of the average prices of the different categories of diamond cuts:

fig <- dmds_data |> 
  plot_ly(labels = ~cut,values = ~avg_price, type = "pie") |> 
  layout(title = "Average Prices of Diamonds",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))  
 
fig


In the plot_ly() function, we specified that we wanted to plot the average prices of the diamond cuts by parsing “cut” to the labels = argument and “avg_price” to the values = argument. We also told plotly that we wanted a pie chart, by giving “pie” to the argument type =.

In the layout() function, we declined to show grid, zero line, and tick labels on both the X and Y axes using the FALSE expression for the given arguments.
 

Example 2: Show Label & Set Text Orientation in Pie Chart

In this second example, we will include the labels in the pie chart, and also set a different text orientation for the labels:

fig <- dmds_data |> 
  plot_ly(labels = ~cut,
          values = ~avg_price, 
          textposition = "inside",
          textinfo = "label+percent",
          insidetextorientation="tangential",
          type = "pie") |> 
  layout(title = "Average Prices of Diamonds",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))  
 
fig


In the above code, we introduced 3 new arguments in the plot_ly() function, namely: textposition =, textinfo =, and insidetextorientation =, with which we specified that we want the chart to display labels and percentages inside the chart in a tangential format.

We can change the orientation of the text to “radial” from “tangential” and also change its position to “outside” from “inside”.
 

Example 3: Include Subplots in Pie Chart

In this third example, we will include the subplots of average depth and average table in addition to our original pie chart, like so:

fig <- dmds_data |> 
  plot_ly(labels = ~cut,
          values = ~avg_price, 
          type = "pie",
          name = "Average Price",
          domain = list(x = c(0, 0.4), y = c(0.4, 1))) |>  
  add_pie(labels = ~cut, values = ~avg_depth, name = "Average Depth",
          domain = list(x = c(0.6, 1), y = c(0.4, 1))) |> 
  add_pie(labels = ~cut, values = ~avg_table, name = "Average Table",
          domain = list(x = c(0.25, 0.75), y = c(0, 0.6))) |> 
  layout(title = "Diamonds Pie Chart Subplots",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))  
 
 
 
 
fig


In the example above, we added two new pie chart layers to our existing pie chart, using the function add_pie(). We also used the domain = argument to set the positions of each chart by defining their X and Y coordinates.
 

Example 4: Build Basic Donut Chart

In this fourth and final example, we will build a basic donut chart from our original pie chart showing the average prices of the diamond:

fig <- dmds_data |> 
  plot_ly(labels = ~cut,
          values = ~avg_price) |> 
  add_pie(hole = 0.6) |> 
  layout(title = "Donut Chart of Diamonds Prices",
         xaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE),
         yaxis = list(showgrid = FALSE, zeroline = FALSE, showticklabels = FALSE))  
fig


In the code above, we added add_pie() to the original code chunk and defined the hole = argument as 0.6, which created the hole in the middle of the pie chart in given size to turn it into a donut chart. The hole size can be increased, e.g., hole = 0.8, and decreased based on the user preference.

 

Video, Further Resources & Summary

Do you need more explanations on how to make plotly pie and donut charts in R? Then you should look at the following YouTube video from the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to make plotly pie and donut charts in R.

 

The YouTube video will be added soon.

 

I hope this tutorial has helped you learn how to make plotly pie and donut charts in R. If you would like to learn more about using plotly in the R programming language, then be sure to check out other interesting plotly in R tutorials on Statistics Globe:

This post has shown how to make circular plotly pie and donut charts in R. In case you have further questions, you may leave a comment below.

 

R & Python Expert Ifeanyi Idiaye

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top