Miscellaneous Ggplot2 Extensions
Assorted experimental ggplot2
extensions. This package is partly a learning
exercise for myself, so not all contents will be useful to others.
A position adjustment that both stacks and dodges bars to create waterfall
charts:
set.seed(1)
p <- ggplot(data.frame(x=1:20, y=rnorm(20)), aes(x=x, y=y, fill=y > 0))
p + geom_col()
p + geom_col(position='waterfall')
It is primarily intended for geom_col
, but can be used with arbitrary geoms:
p + geom_point()
p + geom_point(position=position_waterfall(vjust=1))
If you use arbitrary geoms you may need to adjust position with vjust
as we do
here as otherwise the graphical elements are centered between the end of the
previous cumulative value and the beginning of the next. Please see?position_watefall
for details.
It is can also be used with stats:
dat.norm <- data.frame(x=rnorm(1000))
ggplot(dat.norm, aes(x=x)) + stat_bin()
ggplot(dat.norm, aes(x=x)) + stat_bin(position='waterfall')
For more examples try example(position_waterfall, package='ggbg')
.
Plot cars! This geom was implemented on a lark as an answer to an SO
Question.
ggplot(
geom.car.data, # ggbg data set
aes(x=x, y=y, length=length, width=width, fill=label)
) +
geom_hline(yintercept=seq(5, 35, by=10), color="white", size=2, linetype=2) +
geom_car() +
coord_equal() +
theme(panel.background = element_rect(fill="#555555"),
panel.grid.major = element_blank(),
panel.grid.minor = element_blank())
This package is currently github-only. You can get it withdevtools::install_github('brodieg/ggbg')
or:
f.dl <- tempfile()
f.uz <- tempfile()
github.url <- 'https://github.com/brodieG/ggbg/archive/master.zip'
download.file(github.url, f.dl)
unzip(f.dl, exdir=f.uz)
install.packages(file.path(f.uz, 'ggbg-master'), repos=NULL, type='source')
unlink(c(f.dl, f.uz))
ggplot2
, and in particular