Ggalt
:earth_americas: Extra Coordinate Systems, Geoms, Statistical Transformations & Scales for 'ggplot2'
Install / Use
/learn @hrbrmstr/GgaltREADME
ggalt : Extra Coordinate Systems, Geoms, Statistical Transformations,
Scales & Fonts for ‘ggplot2’
A compendium of ‘geoms’, ‘coords’, ‘stats’, scales and fonts for ‘ggplot2’, including splines, 1d and 2d densities, univariate average shifted histograms, a new map coordinate system based on the ‘PROJ.4’-library and the ‘StateFace’ open source font ‘ProPublica’.
The following functions are implemented:
-
geom_ubar: Uniform width bar charts -
geom_horizon: Horizon charts (modified from https://github.com/AtherEnergy/ggTimeSeries) -
coord_proj: Likecoord_map, only better (prbly shld use this withgeom_cartogramasgeom_map’s new defaults are ugh) -
geom_xspline: Connect control points/observations with an X-spline -
stat_xspline: Connect control points/observations with an X-spline -
geom_bkde: Display a smooth density estimate (usesKernSmooth::bkde) -
geom_stateface: Use ProPublica’s StateFace font in ggplot2 plots -
geom_bkde2d: Contours from a 2d density estimate. (usesKernSmooth::bkde2D) -
stat_bkde: Display a smooth density estimate (usesKernSmooth::bkde) -
stat_bkde2d: Contours from a 2d density estimate. (usesKernSmooth::bkde2D) -
stat_ash: Compute and display a univariate averaged shifted histogram (polynomial kernel) (usesash::ash1/ash::bin1) -
geom_encircle: Automatically enclose points in a polygon -
byte_format: + helpers. e.g. turn10000into10 Kb -
geom_lollipop(): Dead easy lollipops (horizontal or vertical) -
geom_dumbbell(): Dead easy dumbbell plots -
stat_stepribbon(): Step ribbons -
annotation_ticks(): Add minor ticks to identity, exp(1) and exp(10) axis scales independently of each other. -
geom_spikelines(): Instead of geom_vline and geom_hline a pair of segments that originate from same c(x,y) are drawn to the respective axes. -
plotly integration for a few of the ^^ geoms
Installation
# you'll want to see the vignettes, trust me
install.packages("ggplot2")
install.packages("ggalt")
# OR: devtools::install_github("hrbrmstr/ggalt")
Usage
library(ggplot2)
library(gridExtra)
library(ggalt)
# current verison
packageVersion("ggalt")
## [1] '0.6.1'
set.seed(1492)
dat <- data.frame(x=c(1:10, 1:10, 1:10),
y=c(sample(15:30, 10), 2*sample(15:30, 10), 3*sample(15:30, 10)),
group=factor(c(rep(1, 10), rep(2, 10), rep(3, 10)))
)
Horzon Chart
Example carved from: https://github.com/halhen/viz-pub/blob/master/sports-time-of-day/2_gen_chart.R
library(hrbrthemes)
library(ggalt)
library(tidyverse)
sports <- read_tsv("https://github.com/halhen/viz-pub/raw/master/sports-time-of-day/activity.tsv")
sports %>%
group_by(activity) %>%
filter(max(p) > 3e-04,
!grepl('n\\.e\\.c', activity)) %>%
arrange(time) %>%
mutate(p_peak = p / max(p),
p_smooth = (lag(p_peak) + p_peak + lead(p_peak)) / 3,
p_smooth = coalesce(p_smooth, p_peak)) %>%
ungroup() %>%
do({
rbind(.,
filter(., time == 0) %>%
mutate(time = 24*60))
}) %>%
mutate(time = ifelse(time < 3 * 60, time + 24 * 60, time)) %>%
mutate(activity = reorder(activity, p_peak, FUN=which.max)) %>%
arrange(activity) %>%
mutate(activity.f = reorder(as.character(activity), desc(activity))) -> sports
sports <- mutate(sports, time2 = time/60)
ggplot(sports, aes(time2, p_smooth)) +
geom_horizon(bandwidth=0.1) +
facet_grid(activity.f~.) +
scale_x_continuous(expand=c(0,0), breaks=seq(from = 3, to = 27, by = 3), labels = function(x) {sprintf("%02d:00", as.integer(x %% 24))}) +
viridis::scale_fill_viridis(name = "Activity relative to peak", discrete=TRUE,
labels=scales::percent(seq(0, 1, 0.1)+0.1)) +
labs(x=NULL, y=NULL, title="Peak time of day for sports and leisure",
subtitle="Number of participants throughout the day compared to peak popularity.\nNote the morning-and-evening everyday workouts, the midday hobbies,\nand the evenings/late nights out.") +
theme_ipsum_rc(grid="") +
theme(panel.spacing.y=unit(-0.05, "lines")) +
theme(strip.text.y = element_text(hjust=0, angle=360)) +
theme(axis.text.y=element_blank())
<img src="README_figs/README-horizon-1.png" width="912" />
Splines!
ggplot(dat, aes(x, y, group=group, color=group)) +
geom_point() +
geom_line()
<img src="README_figs/README-splines-1.png" width="672" />
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
geom_point() +
geom_line() +
geom_smooth(se=FALSE, linetype="dashed", size=0.5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
<img src="README_figs/README-splines-2.png" width="672" />
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
geom_point(color="black") +
geom_smooth(se=FALSE, linetype="dashed", size=0.5) +
geom_xspline(size=0.5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
<img src="README_figs/README-splines-3.png" width="672" />
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
geom_point(color="black") +
geom_smooth(se=FALSE, linetype="dashed", size=0.5) +
geom_xspline(spline_shape=-0.4, size=0.5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
<img src="README_figs/README-splines-4.png" width="672" />
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
geom_point(color="black") +
geom_smooth(se=FALSE, linetype="dashed", size=0.5) +
geom_xspline(spline_shape=0.4, size=0.5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
<img src="README_figs/README-splines-5.png" width="672" />
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
geom_point(color="black") +
geom_smooth(se=FALSE, linetype="dashed", size=0.5) +
geom_xspline(spline_shape=1, size=0.5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
<img src="README_figs/README-splines-6.png" width="672" />
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
geom_point(color="black") +
geom_smooth(se=FALSE, linetype="dashed", size=0.5) +
geom_xspline(spline_shape=0, size=0.5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
<img src="README_figs/README-splines-7.png" width="672" />
ggplot(dat, aes(x, y, group=group, color=factor(group))) +
geom_point(color="black") +
geom_smooth(se=FALSE, linetype="dashed", size=0.5) +
geom_xspline(spline_shape=-1, size=0.5)
## `geom_smooth()` using method = 'loess' and formula 'y ~ x'
<img src="README_figs/README-splines-8.png" width="672" />
Alternate (better) density plots
# bkde
data(geyser, package="MASS")
ggplot(geyser, aes(x=duration)) +
stat_bkde(alpha=1/2)
## Bandwidth not specified. Using '0.14', via KernSmooth::dpik.
<img src="README_figs/README-bkde_ash-1.png" width="672" />
ggplot(geyser, aes(x=duration)) +
geom_bkde(alpha=1/2)
## Bandwidth not specified. Using '0.14', via KernSmooth::dpik.
<img src="README_figs/README-bkde_ash-2.png" width="672" />
ggplot(geyser, aes(x=duration)) +
stat_bkde(bandwidth=0.25)
<img src="README_figs/README-bkde_ash-3.png" width="672" />
ggplot(geyser, aes(x=duration)) +
geom_bkde(bandwidth=0.25)
<img src="README_figs/README-bkde_ash-4.png" width="672" />
set.seed(1492)
dat <- data.frame(cond = factor(rep(c("A","B"), each=200)),
rating = c(rnorm(200),rnorm(200, mean=.8)))
ggplot(dat, aes(x=rating, color=cond)) + geom_bkde(fill="#00000000")
## Bandwidth not specified. Using '0.36', via KernSmooth::dpik.
## Bandwidth not specified. Using '0.31', via KernSmooth::dpik.
<img src="README_figs/README-bkde_ash-5.png" width="672" />
ggplot(dat, aes(x=rating, fill=cond)) + geom_bkde(alpha=0.3)
## Bandwidth not specified. Using '0.36', via KernSmooth::dpik.
## Bandwidth not specified. Using '0.31', via KernSmooth::dpik.
<img src="README_figs/README-bkde_ash-6.png" width="672" />
# ash
set.seed(1492)
dat <- data.frame(x=rnorm(100))
grid.arrange(ggplot(dat, aes(x)) + stat_ash(),
ggplot(dat, aes(x)) + stat_bkde(),
ggplot(dat, aes(x)) + stat_density(),
nrow=3)
## Estimate nonzero outside interval ab.
## Bandwidth not specified. Using '0.43', via KernSmooth::dpik.
<img src="README_figs/README-bkde_ash-7.png" width="672" />
cols <- RColorBrewer::brewer.pal(3, "Dark2")
ggplot(dat, aes(x)) +
stat_ash(alpha=1/3, fill=cols[3]) +
stat_bkde(alpha=1/3, fill=cols[2]) +
stat_density(alpha=1/3, fill=cols[1]) +
geom_rug() +
labs(x=NULL, y="density/estimate") +
scale_x_continuous(expand=c(0,0)) +
theme_bw() +
theme(panel.grid=element_blank()) +
theme(panel.border=element_blank())
## Estimate nonzero outside interval ab.
## Bandwidth not specified. Using '0.43', via KernSmooth::dpik.
<img src="README_figs/README-bkde_ash-8.png" width="672" />
Alternate 2D density plots
m <- ggplot(faithful, aes(x = eruptions, y = waiting)) +
geom_point() +
xlim(0.5, 6) +
ylim(40, 110)
m + ge
