Video link https://youtu.be/cqi_bCDd6mU

Sunburst chart using R

We will create a sunburst chart in R, customise it and then save it as a static image so that you can use it in your word document reports.

Packages used in this demo

If you do not have these packages already installed on your computer then please install them using the following commands in the next threee lines

install.packages(highcharter)

install.packages(dplyr)

install.packages(gapminder)

Once you have your packages installed then we will load the packages in the r environment using the following commands

# Packages for creating the chart
library(highcharter)

# Other supporting packages
library(dplyr)

# Sample data for our charts
library(gapminder)

gapminder dataset will be used in this demo, so let us see what gapminder dataset is all about.

# Let us view the data
head(gapminder)

Prepare our data for the chart

We need to get the population data for the top 10 countries which had the highest population in year 2007. Following command will prepare that data for us.

gapminder_2007 <- gapminder::gapminder %>%
  filter(year  == max(year))

dout <- data_to_hierarchical(gapminder_2007, c(continent, country), pop)
#dout

Basic sunburst chart

We will use the conntient and the countries under that continent for the sunburst chart we will display their population in the sunburst chart.

hchart(dout, type = "sunburst")

Customise the sunburst chart

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_economist())

pl3

Themes

There are a number of themes which you can try. hc_theme_538() hc_theme_alone() hc_theme_bloom() hc_theme_chalk() hc_theme_darkunica() hc_theme_db() hc_theme_economist() hc_theme_elementary() hc_theme_ffx() hc_theme_flat() hc_theme_flatdark() hc_theme_ft() hc_theme_ggplot2() hc_theme_google() hc_theme_gridlight() hc_theme_handdrawn() hc_theme_hcrt() hc_theme_monokai() hc_theme_null() hc_theme_sandsignika() hc_theme_smpl() hc_theme_sparkline() hc_theme_sparkline_vb() hc_theme_superheroes() hc_theme_tufte() hc_theme_tufte2() hc_theme_merge()

Let us show you some of these themes

hc_theme_538

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_538())
pl3

hc_theme_alone()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_alone())

pl3

hc_theme_bloom()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_bloom())

pl3

hc_theme_chalk()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_chalk())

pl3

hc_theme_darkunica()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_darkunica())

pl3

hc_theme_db()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_db())

pl3

hc_theme_economist()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_economist())

pl3

hc_theme_elementary()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_elementary())

pl3

hc_theme_ffx()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_ffx())

pl3

hc_theme_flat()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_flat())

pl3

hc_theme_flatdark()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_flatdark())

pl3

hc_theme_ft()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_ft())

pl3

hc_theme_ggplot2()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_ggplot2())

pl3

hc_theme_google()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_google())

pl3

hc_theme_gridlight()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_gridlight())

pl3

hc_theme_handdrawn()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_handdrawn())

pl3

hc_theme_hcrt()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_hcrt())

pl3

hc_theme_monokai()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_monokai())

pl3

hc_theme_null()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_null())

pl3

hc_theme_sandsignika()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_sandsignika())

pl3

hc_theme_smpl()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_smpl())

pl3

hc_theme_sparkline()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_sparkline())

pl3

hc_theme_sparkline_vb()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_sparkline_vb())

pl3

hc_theme_superheroes()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_superheroes())

pl3

hc_theme_tufte()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_tufte())

pl3

hc_theme_tufte2()

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_tufte2())

pl3

hc_theme_merge()

You can combine multiple hc_theme objects. For example, you can use a built-in theme and them alter some properties of that theme. In our example below we have used the hc_theme_superheroes(). By using the hc_theme_merge we changed the background to transparent and the also changed the title colour and font.

pl3 <- highchart() %>%
  hc_chart(type = "sunburst") %>%
  hc_title(text = "Using Highcharter for creating a sunburst chart") %>%
  hc_subtitle(text = "#techanswers88",style = list(fontSize = "16px",fontWeight = "bold", color = "red")) %>%
  hc_add_series(name = "pop" , data = dout)%>%
  hc_add_theme(hc_theme_merge(hc_theme_superheroes(), hc_theme(chart = list(
                              backgroundColor = "transparent" )
                              , title = list(  style = list(
                                color = "Blue",fontFamily = "Erica One"))
                              )))
                              

pl3

Save your chart

As highcharter charts are rendered slowly on screen so give it a delay before saving the chart. In our example we have used delay = 4 so that the chart gets saved after 4 seconds when it start rendering. If your chart is complicated and takes more time to render and your saved image is not complete then increase the delay eg. delay = 10

Remember that it will save the chart which was created last. If you are creating 10 charts then you can use the same command after

## save as a static image to be use in your word document or presentation
library(htmlwidgets)
library(webshot)

# Use these commands for saving the charts. Change the name of the plot as needed.
htmlwidgets::saveWidget(widget = pl3, file = "D:\\tmp\\myChart.html")
webshot::webshot(url = "D:\\tmp\\myChart.html" , file ="D:\\tmp\\SunburstChart.png", delay = 4 )

Youtube video link https://youtu.be/cqi_bCDd6mU