Skip to content

Instantly share code, notes, and snippets.

@anghyflawn
Last active August 29, 2015 14:22
Show Gist options
  • Save anghyflawn/c794887b637d0af74702 to your computer and use it in GitHub Desktop.
Save anghyflawn/c794887b637d0af74702 to your computer and use it in GitHub Desktop.
Example of visreg output plotted with ggplot
# library(visreg)
# library(mgcv)
# library(dplyr)
fit <- gam(v1h.v2h.ratio ~ s(v2h.dur, by=v1, k=5) +
v1 + v1.is.long + s(speaker, bs='re') + s(word, bs='re'),
data=sw.data)
v <- visreg(fit, xvar='v2h.dur', by='v1')
# Some advantages of using ggplot2 here:
# - Consistency with other plots
# - Control over display (as in the plotting of residuals here)
# - Control over what gets shown (here the model includes a set of observations where
# v1 is \\sw but don't really want that plotted)
ggplot(filter(v$fit, v1 != '\\sw'), aes(v2h.dur, visregFit))+
geom_line(colour='blue', size=1)+
geom_ribbon(aes(ymin=visregLwr, ymax=visregUpr), alpha=.3)+
geom_point(data=filter(v$res, v1 != '\\sw'), aes(v2h.dur, visregRes), size=1, alpha=.3)+
xlab('Duration of post-tonic vowel')+
ylab('Effect on V1/V2 ratio')+
facet_wrap(~v1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment