Skip to content

Instantly share code, notes, and snippets.

@fauxneticien
Last active April 10, 2018 06:17
Show Gist options
  • Save fauxneticien/050711c9b237bd7b2dbf8271dd821951 to your computer and use it in GitHub Desktop.
Save fauxneticien/050711c9b237bd7b2dbf8271dd821951 to your computer and use it in GitHub Desktop.
Plot mean durations of various components across multiple consonant types
library(tidyverse)
# Start with likely output of a group_by(...) %>% summarise(...) on raw data
# which computes the mean and error on various distributions of durations
# name each component-measure combination variable in a dot-separated form, e.g.
# clo.dur, clo.err (mean closure duration, standard error for closure duration)
tibble(
consonant = c("Pre-stopped nasal", "Nasal", "Oral stop"),
clo.dur = c(25, NA, 30),
clo.err = c(5, NA, 6),
bur.dur = c(10, NA, 15),
bur.err = c(3, NA, 4),
son.dur = c(50, 60, NA),
son.err = c(6, 5, NA)
) %>%
gather(measure, duration, -consonant) %>%
separate(measure, into = c("component", "measure"), sep = "\\.") %>%
spread(key = measure, value = duration) %>%
mutate(component = factor(component,
levels = c("clo", "bur", "son"),
labels = c("Closure", "Burst", "Sonorance"),
ordered = TRUE)) %>%
# Since we want to make a stacked bar chart with error bars, we need to
# calculate where the error bars should be centered, since a stacked bar
# by definition does not start at y = 0
arrange(consonant, component) %>%
group_by(consonant) %>%
mutate(err_centre = lag(dur) %>% ifelse(is.na(.), 0, .) %>% cumsum() + dur) %>%
# Ready to plot!
ggplot(aes(x = consonant, y = dur, fill = component)) +
geom_bar(stat = "identity",
position = position_stack(reverse = TRUE),
color = "black",
width = 0.5) +
scale_fill_manual(values = c("#636363", "#f0f0f0", "#ffffff")) +
geom_linerange(aes(ymin = err_centre - err, ymax = err_centre + err), position = "identity") +
coord_flip() +
theme_bw(base_size = 12) +
xlab("Cosonant type") +
ylab("Duration (ms)") +
ggtitle("Mean durations of components within 3 types of consonants")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment