Skip to content

Instantly share code, notes, and snippets.

@ericpgreen
Created August 1, 2018 15:06
Show Gist options
  • Select an option

  • Save ericpgreen/21e89768f5923034d3cbb5856cc591d2 to your computer and use it in GitHub Desktop.

Select an option

Save ericpgreen/21e89768f5923034d3cbb5856cc591d2 to your computer and use it in GitHub Desktop.
# convert back to wide for alluvial
datW <-
datL %>%
# long to wide
select(ID, w1group, time, depress, dep16) %>%
gather(key = variable, value = value, -ID, -w1group, -time) %>%
unite(variableT, variable, time, sep=".") %>%
spread(variableT, value) %>%
# alluvial prep
filter(complete.cases(.)) %>%
group_by(dep16.1, dep16.2, dep16.3, dep16.4) %>%
summarise(n = n()) %>%
ungroup() %>%
mutate_at(vars(-n), funs(factor(., levels=c(0, 1), labels=c("not depressed",
"depressed"))))
# alluvial diagram
library(alluvial)
alluvial(datW[,1:4], freq=datW$n,
col = ifelse(datW$dep16.4 == "depressed", "#ff0000", "#D3D3D3"),
axis_labels = c("Year 1", "Year 2", "Year 3", "Year 4"),
cex = 0.7)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment