Skip to content

Instantly share code, notes, and snippets.

@hrbrmstr
Created December 31, 2014 13:43
Show Gist options
  • Save hrbrmstr/88f7faa3f452d6d747a7 to your computer and use it in GitHub Desktop.
Save hrbrmstr/88f7faa3f452d6d747a7 to your computer and use it in GitHub Desktop.
using tidyr expand to get all the levels, fill NA with 0 and make a heatmap with colors (and text that changes too)
library(dplyr)
library(scales)
library(ggplot2)
library(RColorBrewer)
dat_sum <- dat %>%
mutate(A=factor(A),
B=factor(B)) %>%
group_by(B, A, Outcome) %>%
summarise(outcome_total=n()) %>%
mutate(freq=outcome_total/sum(outcome_total)) %>%
merge(expand(., B, A, Outcome), all.y=TRUE) %>%
mutate(outcome_total=ifelse(is.na(outcome_total), 0, outcome_total),
freq=ifelse(is.na(freq), 0, freq),
fill_col=cut(freq,
breaks=seq(0,1,.1),
include.lowest=TRUE))
dat_sum %>%
ggplot(aes(x=B, y=A)) +
geom_tile(aes(fill=fill_col), color="#7f7f7f") +
geom_text(aes(label=percent(freq), color=fill_col)) +
scale_fill_manual(values=c("white", brewer.pal(n=9, name="BuGn"))) +
scale_color_manual(values=c(rep("black", 6), rep("white", 3))) +
facet_wrap(~Outcome, ncol=3) +
theme_bw() +
theme(legend.position="none") +
theme(panel.grid=element_blank()) +
theme(strip.background=element_blank())
dat <- read.table(text="ID,A,B,Outcome
1,5,4,Died in Hospital
2,4,4,Transferred to other acute hospital
3,5,4,Transferred to other acute hospital
4,2,4,Transferred to other acute hospital
5,5,4,Died in Hospital
6,5,4,Died in Hospital
7,5,4,Transferred to other acute hospital
8,4,3,Died in Hospital
9,5,4,Died in Hospital
10,2,4,Transferred to other acute hospital
11,5,4,Transferred to other acute hospital
12,1,3,Discharged Home
13,5,4,Died in Hospital
14,5,4,Died in Hospital
15,5,4,Discharged Home
16,5,4,Died in Hospital
17,5,4,Transferred to other acute hospital
18,1,4,Transferred to other acute hospital
19,2,4,Died in Hospital
20,3,4,Transferred to other acute hospital
21,4,4,Died in Hospital
22,5,4,Discharged Home
23,5,4,Discharged Home
24,4,4,Transferred to other acute hospital
25,5,4,Transferred to other acute hospital
26,5,4,Transferred to rehab facility
27,2,4,Transferred to other acute hospital
28,2,4,Died in Hospital
29,5,4,Discharged Home
30,4,4,Transferred to other acute hospital
31,5,4,Died in Hospital
32,5,4,Transferred to other acute hospital
33,5,4,Died in Hospital
34,5,4,Transferred to other acute hospital
35,4,4,Transferred to other acute hospital
36,3,4,Discharged Home
37,4,4,Transferred to other acute hospital
38,5,4,Died in Hospital
39,2,4,Died in Hospital
40,5,4,Transferred to other acute hospital
41,5,4,Died in Hospital
42,2,4,Discharged Home
43,5,4,Died in Hospital
44,5,4,Died in Hospital
45,5,4,Died in Hospital
46,5,4,Died in Hospital
47,5,4,Discharged Home
48,2,4,Discharged Home
49,5,4,Died in Hospital
50,3,4,Discharged Home
51,5,4,Died in Hospital
52,4,4,Transferred to other acute hospital
53,3,4,Transferred to other acute hospital
54,1,3,Transferred to other acute hospital
55,4,4,Discharged Home
56,2,4,Discharged Home
57,5,4,Transferred to other ICU
58,2,4,Discharged Home
59,5,4,Died in Hospital
60,4,4,Discharged Home
61,5,4,Transferred to other acute hospital
62,5,4,Discharged Home
63,1,4,Discharged Home
64,3,4,Died in Hospital
65,2,3,Discharged Home
66,4,4,Discharged Home
67,3,4,Discharged Home
68,3,4,Discharged Home
69,1,4,Discharged Home
70,2,4,Discharged Home
71,1,3,Discharged Home
72,1,2,Discharged Home
73,1,3,Discharged Home
74,1,3,Discharged Home
75,2,4,Discharged Home
76,3,4,Transferred to other acute hospital
77,4,3,Discharged Home", header=TRUE, stringsAs=FALSE, sep=",")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment