Visualising connection in R

Rita Giordano

rgiordano@gmx.com

Workshop for Ukraine | 09th November 2023

About me

  • Independent data visualisation consultant

  • PhD in statistics for crystallography

Agenda of the day

  • Introduction to {circlize} and circular visualization
  • Create chord diagram with {circlize}
  • Introduction to {networkd3}
  • Sankey diagram

{circlize} package

circlize

Coordinates systems

Cartesian

Polar

Canvas coordinates

Sector, tracks and cells

Add the track the chart

library(circlize)
sectors = c("d", "f", "e", "c", "g", "b", "a")
s1 = factor(sectors)
circos.initialize(s1, xlim = c(0,1))
circos.track(ylim = c(0, 1))

Get info

library(circlize)
sectors = c("d", "f", "e", "c", "g", "b", "a")
s1 = factor(sectors)
circos.initialize(s1, xlim = c(0,1))
circos.track(ylim = c(0, 1))
circos.info()
All your sectors:
[1] "d" "f" "e" "c" "g" "b" "a"

All your tracks:
[1] 1

Your current sector.index is a
Your current track.index is 1

Add labels

circos.track(ylim = c(0, 1),
  panel.fun = function(x,y) {
  circos.text(CELL_META$xcenter,
              CELL_META$cell.ylim[2] + mm_y(6),
              CELL_META$sector.index)})

Create your own track

Over to you!

  • Exercise 1
05:00

Chord diagram

Sankey diagram

Type of data

  • {circlize} accept the data in two formats

Matrix

A B C
a 1 4 7
b 2 5 8
c 3 6 9

Data frame

from to value
a A 1
b B 2
c C 3

Data for the chord diagram

coo_name coa_name refugees
Syrian Arab Rep. Türkiye 3535898
Afghanistan Iran 3413249
Afghanistan Pakistan 1743278
Ukraine Other 1275315
Ukraine Germany 1002999
Ukraine Poland 958147

Data for the chord diagram

from to value
Syrian Arab Rep. Türkiye 3535898
Afghanistan Iran 3413249
Afghanistan Pakistan 1743278
Ukraine Other 1275315
Ukraine Germany 1002999
Ukraine Poland 958147

Create a chord diagram

chordDiagram(data)

Add layout: circos.par()

circos.par(start.degree = 30)
chordDiagram(data)
circos.clear()

Add layout: circos.par()

circos.par(start.degree = 30, gap.degree = 2)
chordDiagram(data)
circos.clear()

Chord diagram’s layout

Over to you!

  • Exercise 2
05:00

Create colour palette

Create colour palette

coo_colour <- structure(
  c("#1B9E7750", "#A04E2350", "#FFD700"),
  names = c("Syrian Arab Rep.", "Afghanistan", "Ukraine")
  )
Syrian Arab Rep.      Afghanistan          Ukraine 
     "#1B9E7750"      "#A04E2350"        "#FFD700" 
  • Change colour: grid.col

Modify the chord diagram

  • Increase gap between the 2 sets of sectors: big.gap

  • Increase gap between each sectors: small.gap

Change other parameters

  • Remove default label: annotationTrack = "grid"

  • Add pre-allocated empty tracks: preAllocateTracks

preAllocateTracks = list(
               track.height = max(strwidth(unlist(dimnames(data))))
             )
  • Add label using circos.text()

Title and footnote

  • title with title()

  • footnote with text()

2 minutes breaks

  • Stretch you leg

  • Move around the room

02:00

{networkD3}

  • {networkd3} create d3 javascript graphs, such as Sankey diagram, tree, dendrogram and network

  • It use d3.js function to customise the graphs

Sankey diagram

Prepare the data: nodes

nodes <- data.frame(
  name = unique(c(links$source, links$target))
)
source target value name
A C 2 A
A D 2 B
B C 1 C
B E 1 D
B D 3 E

Prepare the data: assign ID

links <- links %>% 
mutate(
    IDsource = match(source, nodes$name)-1,
    IDtarget = match(target, nodes$name)-1
    )
source target value IDsource IDtarget
A C 2 0 2
A D 2 0 3
B C 1 1 2
B E 1 1 4
B D 3 1 3

Sankey diagram

sankeyNetwork(Links = links,
              Nodes = nodes,
              Source = "IDsource", 
              Target = "IDtarget",
              Value = "value", 
              NodeID = "name",
              LinkGroup = "source",
              sinksRight = FALSE,
              fontSize = 18)

Change colours with d3

ref_col <- 'd3.scaleOrdinal()
                .range(["#1B9E7750", "#A04E2350", "#FFD700", "#418FDE",
                        "#418FDE", "#418FDE", "#418FDE", "#418FDE",
                        "#418FDE", "#418FDE", "#418FDE", "#418FDE"]);'

Add title and footnotes

library(htmltools)
library(htmlwidgets)
sd <- prependContent(sd, tags$h2("Refugees in 2022"))
sd <- appendContent(sd, tags$p("Source: UNHCR"))

Further resources

  • https://jokergoo.github.io/circlize_book/book/

  • https://christophergandrud.github.io/networkD3/

  • https://d3js.org/api

  • https://r-graph-gallery.com/sankey-diagram.html

Thank you for your attention

Bonus