By hrbrmstr
library(tidyr)
library(ggplot2)
library(ggthemes)
library(scales)
library(dplyr)# Easiest way to transcribe the PDF table
# The slope calculation will enable us to color the lines/points based on up/down
dat data_frame(`2014-11-01`=c(0.11, 0.22, 0.35, 0.31, 0.01),
`2015-12-01`=c(0.17, 0.30, 0.30, 0.23, 0.00),
slope=factor(sign(`2014-11-01` - `2015-12-01`)),
fear_level=c("Very worried", "Somewhat worried", "Not too worried",
"Not at all", "Don't know/refused"))# Transform that into something we can use
dat gather(dat, month, value, -fear_level, -slope)# We need real dates for the X-axis manipulation
dat mutate(dat, month=as.Date(as.character(month)))# Since 2 categories have the same ending value, we need to
# take care of that (this is one of a few "gotchas" in slopegraph preparation)
end_lab dat %>%
filter(month==as.Date("2015-12-01")) %>%
group_by(value) %>%
summarise(lab=sprintf("%s", paste(fear_level, collapse=", ")))gg ggplot(dat)
# line
gg gg + geom_line(aes(x=month, y=value, color=slope, group=fear_level), size=1)
# points
gg gg + geom_point(aes(x=month, y=value, fill=slope, group=fear_level),
color="white", shape=21, size=2.5)# left labels
gg gg + geom_text(data=filter(dat, month==as.Date("2014-11-01")),
aes(x=month, y=value, label=sprintf("%s — %s ", fear_level, percent(value))),
hjust=1, size=3)
# right ...read moreSource:: r-bloggers.com