plotly Sunburst Chart in R (3 Examples)


Hi! This tutorial will show you how to make a sunburst chart in plotly in the R programming language.

A sunburst chart is used to display hierarchical data, with each level of the hierarchy represented by a circle or an arc that is sliced for each category.

Sunburst charts require the tree data model, where the “id” is used to set unique identifiers; “parent” is used to set the parent node; “children” is used to set the children nodes; and “values” is used to set the chart values.

Just as in a biological context, the children nodes emerge from the parent node, and the children can also have their own children too, which can be mapped in the chart as well.

Here is a quick overview of this tutorial:

Let’s dive into the R code!


Install & Load plotly & dplyr

If you do not have plotly and dplyr already installed in your R environment, then you should run the lines of code below; otherwise, you can skip to the next section of this tutorial.

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

The dplyr library is the go-to library for data analysis and manipulation in R, and it enables the use of the pipe operator to pipe lines of code together, so that they can be run as a chunk.

Example 1: Make Sunburst Chart

In this first example, we will build a basic sunburst chart as follows.

labels <- c("Andy", "John", "Megan", "Tom", "Naomi", "Matt", "Florence", "Harry", "Sam")
parents <- c("", "Andy", "Andy", "Megan", "Megan", "Andy", "Andy", "Florence", "Andy")
values <- c(20,34,25,20,8,15,15,9,9)
fig <- plot_ly(
  type = "sunburst",
  labels = labels,
  parents = parents,
  values = values

In the example above, we created the parents, labels, and values categories, which we parsed to the plot_ly() function, and also specified the chart type as “sunburst”. You can click on any category that has a child to expand it.


Example 2: Make Sunburst Chart with Repeated Labels

In this second example, we will make a sunburst chart with repeated labels. We will, however, first create the data frame that will be used in this example.

df <- data.frame(
  ids = c(
    "North America", "Europe", "Australia", "North America - Football", "Soccer",
    "North America - Rugby", "Europe - Football", "Rugby",
    "Europe - American Football", "Australia - Football", "Association",
    "Australian Rules", "Austtralia - American Football", "Australia - Rugby",
    "Rugby League", "Rugby Union"
  labels = c(
    "North<br>America", "Europe", "Australia", "Football", "Soccer", "Rugby",
    "Football", "Rugby", "American<br>Football", "Football", "Association",
    "Australian<br>Rules", "American<br>Football", "Rugby", "Rugby<br>League",
  parents = c(
    "", "", "", "North America", "North America", "North America", "Europe",
    "Europe", "Europe", "Australia", "Australia - Football", "Australia - Football",
    "Australia - Football", "Australia - Football", "Australia - Rugby",
    "Australia - Rugby"
  stringsAsFactors = FALSE

Next, we will create the sunburst chart with repeated labels:

fig <- df |> 
    ids = ~ids,
    labels = ~labels,
    parents = ~parents,
    type = "sunburst"

As you can see when you hover over the chart, there are only labels and no numerical values; and as is characteristic of sunburst charts, you can click on any category that has a child to expand it.

Example 3: Adjust Text Orientation inside Sunburst Chart

In this third example, we will adjust the text orientation inside the sectors of the sunburst chart. We will make use of the popular coffee dataset. To download the dataset, run the line of code below.

df <- read.csv("")

Next, we will create a sunburst chart and set its text orientation:

fig <- df |> 
    type = "sunburst",
    ids = ~ids,
    labels = ~labels,
    parents = ~parents,
    domain = list(column = 1),
    maxdepth = 2,
    insidetextorientation = "radial"

In the code above, we set the text orientation of the sunburst chart sectors, by parsing “radial” to the argument insidetextorientation = argument. Using “auto”, the text may be rotated to fit with the maximum size inside the plot.


Video, Further Resources & Summary

Do you need more explanations on how to make a plotly sunburst chart in R? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to make plotly sunburst chart in R.


The YouTube video will be added soon.


With that, we have demonstrated how to make a plotly sunburst chart in the R programming language. As you can see, it is another interesting way to interactively visualize hierarchical data.

This post has shown how to make a plotly sunburst chart 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.