MATLAB style stem plot with R

November 9, 2009

(This article was first published on » R, and kindly contributed to R-bloggers)

Recently I wanted to plot an impulse response function with R and missed the MATLAB style stem plot for doing it. I couldn’t find an R function for it with a quick Google search so I made my own. So here is the function and a small example :

#The function
stem <- function(x,y,pch=16,linecol=1,clinecol=1,...){
if (missing(y)){
    y = x
    x = 1:length(x) }
    for (i in 1:length(x)){
       lines(c(x[i],x[i]), c(0,y[i]),col=linecol)
    lines(c(x[1]-2,x[length(x)]+2), c(0,0),col=clinecol)

#An example
x <- seq(0, 2*pi, by = 0.2)
stem(x,sin(x), main = 'Default style')
stem(x,sin(x),main='With MATLAB colors', col=4,linecol=4,clinecol=2)


To leave a comment for the author, please follow the link and comment on their blog: » R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...


Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)