Giving a Thematic Touch to your Interactive Chart

Sometimes you need to express yourself.
data-visualization
highcharts
Author

Joshua Kunst Fuentes

Published

March 3, 2017

Preliminars

Usually (mainly at work) I made a chart and when I present it nobody cares about the style, if the chart comes from an excel spreadsheet, paint or intercative chart, or colors, labels, font, or things I like to care. That’s sad for me but it’s fine: the data/history behind and how you present it is what matters. And surely I’m overreacting.

But hey! That’s not implies you only must do always clean chart or tufte style plots. Sometimes you can play with the topic of your chart and give some thematic touch.

The first example that come to my mind is the Iraq’s bloody toll visualization:

Iraq’s bloody toll

So. We’ll use some resources to try:

  • Add some context of the topic before the viewer read something.
  • Hopefully keep in the viewer’s memory :) in a gooood way.

Keeping the message intact, ie, don’t abuse adding many element so the user don’t lose the main point of the chart.

Example I: Oil Spills

We can reuse the bloody toll effect, using with Oil Spills data.

The ourworldindata.org website have a descriptive study Max Roser.

Max Roser (2016) - ‘Oil Spills’. Published online at OurWorldInData.org. Retrieved from: https://ourworldindata.org/oil-spills/ [Online Resource]

They start with:

Over the past 4 decades - the time for which we have data - oil spills decreased dramatically. Although oil spills also happen on land, marine oil spills are considered more serious as the spilled oil is less containable

Let’s load the data and make the basic chart.

Code
library(tidyverse) 
library(jsonlite)

json <- read_lines("https://ourworldindata.org/wp-content/uploads/nvd3/nvd3_multiBarChart_Oil/multiBarChart_Oil.html")
json <- json[seq(
  which(str_detect(json, "var xxx")),
  first(which(str_detect(json, "\\}\\]\\;")))
)]

json <- fromJSON(str_replace_all(json, "var xxx = |;$", ""))
json <- transpose(json)

str(json)
List of 2
 $ :List of 2
  ..$ values:'data.frame':  43 obs. of  2 variables:
  .. ..$ x: num [1:43] 0.00 3.16e+10 6.31e+10 9.47e+10 1.26e+11 ...
  .. ..$ y: int [1:43] 30 14 27 31 27 20 26 16 23 32 ...
  ..$ key   : chr ">700 Tonnes"
 $ :List of 2
  ..$ values:'data.frame':  43 obs. of  2 variables:
  .. ..$ x: num [1:43] 0.00 3.16e+10 6.31e+10 9.47e+10 1.26e+11 ...
  .. ..$ y: int [1:43] 7 18 48 28 90 96 67 69 59 60 ...
  ..$ key   : chr "7-700 Tonnes"
Code
dspills <- map_df(json, function(x) {
  df <- as.data.frame(x[["values"]])
  df$key <- x[["key"]]
  tbl_df(df)
  df
})

glimpse(dspills)
Rows: 86
Columns: 3
$ x   <dbl> 0.00000e+00, 3.15569e+10, 6.31138e+10, 9.46707e+10, 1.26228e+11, 1…
$ y   <int> 30, 14, 27, 31, 27, 20, 26, 16, 23, 32, 13, 7, 4, 13, 8, 8, 7, 10,…
$ key <chr> ">700 Tonnes", ">700 Tonnes", ">700 Tonnes", ">700 Tonnes", ">700 …

The data is ready. So we can make an staked area chart. I used areaspline here to make a liquid effect.

Code
library(highcharter)

hcspills <- hchart(dspills, "areaspline", hcaes(x, y, group = "key")) %>% 
  hc_plotOptions(series = list(stacking = "normal")) %>% 
  hc_xAxis(type = "datetime") %>% 
  hc_title(text = "Number of Oil Spills Over the Past 4 Decades")

hcspills

Yay, the spills are decreasing over time. So we can do:

  • Add a deep sea background.
  • Reverse the yAxis to the give the fall effect.
  • Add a dark colors to simulate the oil.
  • Add the credits for give the serious (? ;) ) touch.
Code
hcspills2 <- hcspills %>% 
  hc_colors(c("#000000", "#222222")) %>% 
  hc_title(align = "left", style = list(color = "black")) %>% 
  hc_plotOptions(series = list(marker = list(enabled = FALSE))) %>% 
  hc_tooltip(sort = TRUE, table = TRUE) %>% 
  hc_legend(align = "right", verticalAlign = "top", layout = "horizontal") %>% 
  hc_credits(
    enabled = TRUE,
    text = "Data from ITOPF.com",
    href = "http://www.itopf.com/knowledge-resources/data-statistics/statistics/"
  ) %>% 
  hc_chart(
    divBackgroundImage = "https://images-na.ssl-images-amazon.com/images/I/71EUEG8orVL._SL1500_.jpg",
    backgroundColor = hex_to_rgba("white", 0.50)
  ) %>% 
  hc_xAxis(
    opposite = TRUE,
    gridLineWidth = 0,
    title = list(text = "Time", style = list(color = "black")),
    lineColor = "black", tickColor = "black",
    labels = list(style = list(color = "black"))
    ) %>% 
  hc_yAxis(
    reversed = TRUE, 
    gridLineWidth = 0, 
    lineWidth = 1,
    lineColor = "black",
    tickWidth = 1,
    tickLength = 10, 
    tickColor = "black",
    title = list(text = "Oil Spills", style = list(color = "black")),
    labels = list(style = list(color = "black"))
    ) %>% 
  hc_add_theme(hc_theme_elementary())
Code
hcspills2

Example II: Winter Olympic Games

Here we will take the data and chart the participating nations over the years.

Code
library(rvest)

tables <- read_html("https://en.wikipedia.org/wiki/Winter_Olympic_Games") %>% 
  html_table(fill = TRUE)

# dgames <- tables[[6]]
dgames <- tables[[5]]
dgames <- janitor::clean_names(dgames)
dgames <- tbl_df(dgames)

dgames <- mutate_if(dgames, is.character, str_trim)

dgames <- filter(dgames, !no %in% c("1940", "1944"))
dgames <- filter(dgames, !year %in% seq(2018, by = 4, length.out = 4))
dgames <- filter(dgames, row_number() != 1)

dgames <- dgames |> 
  mutate(year = as.numeric(str_extract(year, "[0-9]{4}"))) |> 
  filter(!is.na(year))

Not sure how re-read data to get the right column types. So a dirty trick.

Code
tf <- tempfile(fileext = ".csv")

write_csv(dgames, tf)

dgames <- read_csv(tf)

dgames <- mutate(dgames,
                 # nations = n,
                 nations = str_extract(nations, "\\d+"),
                 nations = as.numeric(nations))

glimpse(dgames)
Rows: 27
Columns: 11
$ year                  <dbl> 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, …
$ no                    <chr> "I", "II", "III", "IV", "[C]", "[C]", "V", "VI",…
$ host                  <chr> "Chamonix", "St. Moritz", "Lake Placid", "Garmis…
$ games_dates_opened_by <chr> "25 January – 5 February 1924Gaston Vidal", "11–…
$ sports_disciplines    <chr> "6 (9)", "4 (8)", "4 (7)", "4 (8)", "Awarded to …
$ competitors           <chr> "258", "464", "252", "646", "Awarded to Japan (S…
$ competitors_2         <chr> "247", "438", "231", "566", "Awarded to Japan (S…
$ competitors_3         <chr> "11", "26", "21", "80", "Awarded to Japan (Sappo…
$ events                <chr> "16", "14", "14", "17", "Awarded to Japan (Sappo…
$ nations               <dbl> 16, 25, 17, 28, NA, NA, 28, 30, 32, 30, 36, 37, …
$ top_nation            <chr> "Norway (NOR)", "Norway (NOR)", "United States (…

Let’s see the first chart:

Code
hcgames <- hchart(dgames, "areaspline", hcaes(year, nations, name = host), name = "Nations") %>% 
  hc_title(text = "Number of Participating Nations in every Winter Olympic Games") %>%
  hc_xAxis(title = list(text = "Time")) %>% 
  hc_yAxis(title = list(text = "Nations"))

hcgames

With that increase of nations in 1980 we can:

  • Use a white color to simulate a big snowed mountain.
  • Put a relevant background.
  • Put some flags for each host.
  • And work on the tooltip to show more information.
Code
library(countrycode)

urlico <- "url(https://raw.githubusercontent.com/tugmaks/flags/2d15d1870266cf5baefb912378ecfba418826a79/flags/flags-iso/flat/24/%s.png)"

# dgames <- dgames %>% 
#   mutate(country = tn,
#     country = str_extract(host, ", .*$"),
#          country = str_replace(country, ", ", ""),
#          country = str_trim(country)) %>% 
#   mutate(countrycode = countrycode(country, origin = "country.name", destination = "iso2c")) %>% 
#   mutate(marker = sprintf(urlico, countrycode),
#          marker = map(marker, function(x) list(symbol = x)),
#          flagicon = sprintf(urlico, countrycode),
#          flagicon = str_replace_all(flagicon, "url\\(|\\)", "")) %>% 
#   rename(men = c_2, women = c_3)

glimpse(dgames)
Rows: 27
Columns: 11
$ year                  <dbl> 1924, 1928, 1932, 1936, 1940, 1944, 1948, 1952, …
$ no                    <chr> "I", "II", "III", "IV", "[C]", "[C]", "V", "VI",…
$ host                  <chr> "Chamonix", "St. Moritz", "Lake Placid", "Garmis…
$ games_dates_opened_by <chr> "25 January – 5 February 1924Gaston Vidal", "11–…
$ sports_disciplines    <chr> "6 (9)", "4 (8)", "4 (7)", "4 (8)", "Awarded to …
$ competitors           <chr> "258", "464", "252", "646", "Awarded to Japan (S…
$ competitors_2         <chr> "247", "438", "231", "566", "Awarded to Japan (S…
$ competitors_3         <chr> "11", "26", "21", "80", "Awarded to Japan (Sappo…
$ events                <chr> "16", "14", "14", "17", "Awarded to Japan (Sappo…
$ nations               <dbl> 16, 25, 17, 28, NA, NA, 28, 30, 32, 30, 36, 37, …
$ top_nation            <chr> "Norway (NOR)", "Norway (NOR)", "United States (…
Code
urlimg <- "https://i.pinimg.com/originals/1a/ad/f5/1aadf58a23d4928945e1dd793f129261.jpg"
ttvars <- c("year", "nations", "sports", "competitors", "women", "men", "events")
tt <- tooltip_table(
  ttvars,
  sprintf("{point.%s}", ttvars), img = tags$img(src="{point.flagicon}", style = "text-align: center;")
)

hcgames2 <- hchart(dgames, "areaspline", hcaes(year, nations, name = host), name = "Nations") %>% 
  hc_colors(hex_to_rgba("white", 0.8)) %>% 
  hc_title(
    text = "Number of Participating Nations in every Winter Olympic Games",
    align = "left",
    style = list(color = "white")
  ) %>% 
  hc_credits(
    enabled = TRUE,
    text = "Data from Wipiedia",
    href = "https://en.wikipedia.org/wiki/Winter_Olympic_Games"
  ) %>% 
  hc_xAxis(
    title = list(text = "Time", style = list(color = "white")),
    gridLineWidth = 0,
    labels = list(style = list(color = "white"))
  ) %>% 
  hc_yAxis(
    lineWidth = 1,
    tickWidth = 1,
    tickLength = 10,
    title = list(text = "Nations", style = list(color = "white")),
    gridLineWidth = 0,
    labels = list(style = list(color = "white"))
  ) %>% 
  hc_chart(
    divBackgroundImage = urlimg,
    backgroundColor = hex_to_rgba("black", 0.10)
    ) %>% 
  hc_tooltip(
    headerFormat = as.character(tags$h4("{point.key}", tags$br())),
    pointFormat = tt,
    useHTML = TRUE,
    backgroundColor = "transparent",
    borderColor = "transparent",
    shadow = FALSE,
    style = list(color = "white", fontSize = "0.8em", fontWeight = "normal"),
    positioner = JS("function () { return { x: this.chart.plotLeft + 15, y: this.chart.plotTop + 0 }; }"),
    shape = "square"
  ) %>% 
  hc_plotOptions(
    series = list(
      states = list(hover = list(halo = list(size  = 30)))
    )
  ) %>% 
  hc_add_theme(hc_theme_elementary())
Code
hcgames2

What do you think? I had fun, so for me this worth every background I used.

Reuse

Citation

BibTeX citation:
@online{kunstfuentes2017,
  author = {Joshua Kunst Fuentes},
  title = {Giving a {Thematic} {Touch} to Your {Interactive} {Chart}},
  date = {2017-03-03},
  url = {https://jkunst.com/blog/posts/2017-03-03-giving-a-thematic-touch-to-your-interactive-chart},
  langid = {en}
}
For attribution, please cite this work as:
Joshua Kunst Fuentes. 2017. “Giving a Thematic Touch to Your Interactive Chart.” March 3, 2017. https://jkunst.com/blog/posts/2017-03-03-giving-a-thematic-touch-to-your-interactive-chart.