Last active
December 29, 2017 15:14
-
-
Save mages/2a2035cd5fb9463fcc1f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Original code by Max Gordon | |
# Source: http://gforge.se/2013/06/visualizing-transitions-with-the-transitionplot-function/ | |
# Create the transition matrix | |
library(Gmisc) | |
library(grid) | |
library(RColorBrewer) | |
set.seed(9730) | |
b4 <- sample(1:3, replace = TRUE, size = 500, prob = c(0.1, 0.4, 0.5)) | |
after <- sample(1:3, replace = TRUE, size = 500, prob = c(0.3, 0.5, 0.2)) | |
b4 <- factor(b4, labels = c("None", "Moderate", "Major")) | |
after <- factor(after, labels = c("None", "Moderate", "Major")) | |
# Create the transition matrix | |
transition_mtrx <- table(b4, after) | |
# SVG output | |
svg(filename = "transitionPlot.svg",width = 8, height = 10.64) | |
transitionPlot(transition_mtrx, | |
box_prop = cbind(c(0.3, 0.7, 0.5), c(0.5, 0.5, 0.4)), | |
txt_start_clr = c("black", "white"), | |
txt_end_clr = c("black", "white"), | |
fill_start_box = brewer.pal(n = 3, name = "Paired")[1:2], | |
fill_end_box = brewer.pal(n = 3, name = "Paired")[1:2], | |
overlap_add_width = 1.3, type_of_arrow = "gradient", | |
min_lwd = unit(1, "mm"), max_lwd = unit(20, "mm")) | |
dev.off() | |
# PNG output | |
png("transitionPlot.png",width = 800, height = 1064) | |
transitionPlot(transition_mtrx, | |
box_prop = cbind(c(0.3, 0.7, 0.5), c(0.5, 0.5, 0.4)), | |
txt_start_clr = c("black", "white"), | |
txt_end_clr = c("black", "white"), | |
fill_start_box = brewer.pal(n = 3, name = "Paired")[1:2], | |
fill_end_box = brewer.pal(n = 3, name = "Paired")[1:2], | |
overlap_add_width = 1.3, type_of_arrow = "gradient", | |
min_lwd = unit(1, "mm"), max_lwd = unit(20, "mm")) | |
dev.off() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment