Dataviz with R




Sophie E. Hill

May 4, 2021

CHIPS Public Seminar: Advancing Health Policy Through Data Visualizations

Pandemic Dataviz: The Good

Pandemic Dataviz: The Bad



Pandemic Dataviz: The Ugly


The Challenge



How do we create data visualizations that are useful, accessible, and true to the data?



  • This is a hard question!
  • But here’s an example of how I use this to guide my own work…

My Little Crony

My Little Crony… in parliament!


My Little Crony

My Little Crony



This seemed like a good candidate for a data visualization.

  • Complex story
  • Public interest
  • Networks are much easier to draw than to describe!

Dataviz with R



  • Free
  • Open-source
  • Has a thriving support community
  • Packages
    • For network visualization: igraph, network, sna, ggraph, visNetwork, threejs, networkD3, ndtv




Rule 1: Don’t reinvent the wheel

Gathering the data: Nodes

Gathering the data: Edges

Code

library(tidyverse)
library(visNetwork)

# load network data
people <- read_csv("people_ex1.csv")
connections <- read_csv("connections_ex1.csv")

# add attributes
people$label <- people$id
connections$title <- paste0("<p>", connections$detail, "</p>")

# create network graph
visNetwork(people, connections) %>%
  visInteraction(hover=TRUE, zoomView = TRUE)

Output




Rule 2: Maximize the data-to-ink ratio

Augmenting the data: Nodes

Code

# add icons
people <- people %>% mutate(shape = "icon",
                            icon.code = case_when(type=="person" ~ "f007",
                                                  type=="firm" ~ "f1ad",
                                                  type=="political party" ~ "f0c0",
                                                  type=="government" ~ "f19c"),
                            icon.face = "FontAwesome",
                            icon.weight = "bold")

# create network graph
visNetwork(people, connections) %>%
  visInteraction(hover=TRUE, zoomView = TRUE) %>%
  addFontAwesome() # add FontAwesome library for icons

Output

Augmenting the data: Edges

Code

connections <- connections %>% 
                       # color edges by type
                mutate(color.color = case_when(type=="contract" ~ "#fc9a9a",
                                               type=="donor" ~ "#95bbf0",
                                               TRUE ~ "#dbd9db"),
                       # scale edges by amount
                       value = case_when(is.na(amount) ~ 100000,
                                         !is.na(amount) ~ amount))

# create network graph
visNetwork(people, connections) %>%
  visInteraction(hover=TRUE, zoomView = TRUE) %>%
  addFontAwesome() %>%# add FontAwesome library for icons
  visSave(file = "crony_ex3.html")

Output




Rule 3: Focus on the user experience

Accessibility

Accessibility

Output

My Little Crony




Rule 1: Don’t reinvent the wheel


Rule 2: Maximize the data-to-ink ratio


Rule 3: Focus on the user experience

Thank you!





        www.mylittlecrony.com
        @sophie_e_hill
        www.sophie-e-hill.com