Here it is
#load packages
library(tidyverse)
library(networkD3)
library(haven)
library(readr)
#import data
bcsreprex <- read_csv("https://app.box.com/s/d4ofbn0sfl76ye8nw21xeddzhvddpv4j")
View(bcsreprex)
d <- bcsreprex
str(d)
#as factors
d <- as.data.frame(unclass(d),
stringsAsFactors = TRUE)
# data viz
(my_levels <- expand.grid(a =c("Not stated","Multicode","Incomplete","Unaffiliated",
"Christian (no denomination)","Roman Catholic",
"Church of England/Anglican","United Reform Church/Congregational",
"Baptist","Methodist","Presbyterian/Church of Scotland",
"Other Christian","Hindu","Jewish","Muslim","Sikh",
"Buddhist","Other"),
b = c("Childhood","Age 42")) %>%
mutate(l = paste(a,b)) %>% pull(l))
(my_readable_data <- tibble::tribble(
~source, ~target, ~value,
## These are artificial values that I want to replace with the actual values
## for the full list above.
"Christian (t1)", "Christian (t2)", 60,
"Muslim (t1)" , "Muslim (t2)" , 30,
"Christian (t1)", "Muslim (t2)" , 15,
"Muslim (t1)" , "Christian (t2)", 5
))
(rel <- list(
links_readable = my_readable_data,
links = my_readable_data %>% mutate_if(is.character,
~as.integer(factor(.x,levels=my_levels))-1),
nodes =data.frame(name = my_levels)
))
sankeyNetwork(Links = rel$links, Nodes = rel$nodes, Source = "Childhood",
Target = "Age 42", Value = "Number", NodeID = "Religion",
units = "People", fontSize = 40, nodeWidth = 50)