Skip to content

Instantly share code, notes, and snippets.

@ifitzpat
Created March 24, 2016 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ifitzpat/6cf6a5cff2f4e526fbff to your computer and use it in GitHub Desktop.
Save ifitzpat/6cf6a5cff2f4e526fbff to your computer and use it in GitHub Desktop.
#function adapted from http://mpastell.com/2009/09/11/matlab-style-stem-plot-with-r/
stem = function(x,y,slope,intercept,pch=16,linecol=2,clinecol=1,lty=2,from_mean=FALSE,...){
if (missing(y)){
y = x
x = 1:length(x)
}
#this plots the points
plot(x,y,pch=pch,...)
#this plots the line
abline(intercept,slope)
if (from_mean) {
#plot the mean line
abline(mean(y),0,lty=1,col=1)
#this plots the stem lines to the mean line
for (i in 1:length(x)){
lines(c(x[i],x[i]), c(mean(y),slope*x[i]+intercept),col=linecol,lty=lty)
}
}
else {
#this plots the stem lines to each point
for (i in 1:length(x)){
lines(c(x[i],x[i]), c(y[i],slope*x[i]+intercept),col=linecol,lty=lty)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment