Video link https://youtu.be/cHYCw9HulTk
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.
If you do not have these packages already installed on your computer then please install them using the following commands in the next three 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)
## # A tibble: 6 x 6
## country continent year lifeExp pop gdpPercap
## <fct> <fct> <int> <dbl> <int> <dbl>
## 1 Afghanistan Asia 1952 28.8 8425333 779.
## 2 Afghanistan Asia 1957 30.3 9240934 821.
## 3 Afghanistan Asia 1962 32.0 10267083 853.
## 4 Afghanistan Asia 1967 34.0 11537966 836.
## 5 Afghanistan Asia 1972 36.1 13079460 740.
## 6 Afghanistan Asia 1977 38.4 14880372 786.
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.
ds <- gapminder%>%
dplyr::filter(year == max(year))%>%
dplyr::arrange(-pop)%>%
head(10)
We will use the conntinent and the countries for the sunburst chart. We will display their population in the sunburst chart.
# Our first chart
hc <- ds %>%
hchart("pyramid",hcaes(x = country,y = pop))
hc
#Chart with more customisations
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_ggplot2())
hc
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 <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_538())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_alone())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_bloom())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_chalk())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_darkunica())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_db())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_economist())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_elementary())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_ffx())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_flat())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_flatdark())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_ft())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_ggplot2())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_google())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_gridlight())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_handdrawn())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_hcrt())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_monokai())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_null())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_sandsignika())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_smpl())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_sparkline())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_sparkline_vb())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_superheroes())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_tufte())
hc
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_tufte2())
hc
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.
hc <- ds %>%
hchart("pyramid",hcaes(x = country, y = pop))%>%
hc_title(text = "Highcharter Pyramid Chart") %>%
hc_subtitle(text = "Top 10 countries by population <br>techanswers88") %>%
hc_add_theme(hc_theme_merge(hc_theme_superheroes(), hc_theme(chart = list(
backgroundColor = "transparent" )
, title = list( style = list(
color = "Blue",fontFamily = "Erica One"))
)))
hc
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.
## save as a static image to be use in your word document or presentation
htmlwidgets::saveWidget(widget = hc, file = "D:\\tmp\\myChart.html")
webshot::webshot(url = "D:\\tmp\\myChart.html" , file ="D:\\tmp\\PyramidChart.png", delay = 4 )
Video link https://youtu.be/cHYCw9HulTk