Loading the data
The data for this analysis comes from the Tidy Tuesday project, the data published on the 06-02-2024. It is a dataset of UNESCO World Heritage Sites by country and year for Norway, Sweden and Denmark.
Show the code
library(tidyverse)
library(highcharter)
library(echarts4r)
# Load second most recent tidy tuesday data
heritage <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2024/2024-02-06/heritage.csv')Let's take a peak at the data:
Show the code
heritage %>%
head(5)# A tibble: 3 × 3
country `2004` `2022`
<chr> <dbl> <dbl>
1 Norway 5 8
2 Denmark 4 10
3 Sweden 13 15
There are only three rows and three columns. For this blog post we will explore 4 types of interactive visuals, that demonstrate the magnitude of change in the number of sites for the 2 years.
Data Visualisation
The first is a simple bar chart, a classic visualisation useful for visualising exact values.
Show the code
# Some wrangling is required first:
heritage %>%
pivot_longer(cols=-country) %>%
mutate(year=name) %>%
select(country,year,value) -> df
df %>% select(country, year) |>
group_by(name = country) |>
summarise(categories = list(year)) |>
list_parse()->df_grouped
hchart(df, "column",
name = "Count",
hcaes(y = value), color = "black") |>
hc_xAxis(
# specify the grouped categories
categories = df_grouped,
# styling a little bit
labels = list(style = list(fontSize = "9px"))
)|>
hc_tooltip(formatter = JS("function() {
// Custom tooltip format: 'Country: Value'
return this.point.category + ': ' + this.y;
}")) |>
hc_add_dependency("plugins/grouped-categories.js") %>% hc_xAxis(title="count") %>% hc_yAxis(title=list(text="")) %>%
hc_title(text="UNESCO World Heritage Sites") %>% hc_subtitle(text="Count of sites by country") %>% hc_legend(enabled=FALSE)
Sweden has the highest number of sites, followed by Norway and Denmark. Norway experienced the highest number of new sites in 2022. Let's keep exploring the bar chart visualisation, however this time with stacking:
Show the code
heritage %>%
pivot_longer(cols = -country) %>%
pivot_wider(names_from = country) %>%
e_charts(name) %>%
e_bar(Denmark,stack="grp") %>%
e_bar(Sweden,stack="grp") %>%
e_bar(Norway,stack="grp") %>%
e_y_axis(name="Count") %>%
e_x_axis(name="Year") %>%
e_title("UNESCO World Heritage Sites", textStyle = list(fontSize = 10))Stacked bar charts are good for visualising proportions of total values in absolute terms. In this case, we want to see the proportion of sites in each country for the two years. In both years Sweden makes up the largest portion of the number of sites.
Another way to visualise the magnitude of change across categories (in this case between years) is with a slope chart. The chart below takes the country as the starting point and the number of sites in 2022 as the end point with the intermediary being 2004:
Show the code
heritage %>%
e_charts() %>%
e_parallel(country,`2004`,`2022`) %>%
e_title("UNESCO World Heritage Sites")The final visualisation takes a different approach to the previous ones. It takes the proportions and projects them into a semi-circle, in the style of a parliamentary structure. In addition, it is interactive, looking only at 2022:
Show the code
# Some data prep first:
heritage %>%
pivot_longer(cols = -country) %>%
filter(name==2022) %>%
select(country,value)->df2
hchart(df2,"item",hcaes(country,value, color=country),
name = "UNESCO Sites",
showInLegend = TRUE,
size = "90%",
center = list("50%", "75%"),
startAngle = -100,
endAngle = 100) %>%
hc_title(text="UNESCO World Heritage Sites") %>%
hc_subtitle(text="Count of sites by country (2022)") %>%
hc_legend(layout="vertical",align="right",verticalAlign="middle",x=0,y=0) %>%
hc_tooltip(pointFormat = "{series.name}: <b>{point.y}</b>") %>%
hc_plotOptions(pie = list(dataLabels = list(enabled = TRUE, format = '<b>{point.name}</b>: {point.y}'))) %>%
hc_exporting(enabled=TRUE)
That's it for this week's blog post. I hope you enjoyed the visualisations, what visualisations can you come up with?