sparklines for R

April 10, 2015

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

I’ve always liked the jQuery Sparklines library and today I had a use case for implementing these in one of my Rmarkdown reports.

While it wouldn’t be too difficult to staticly include a javascript based chart, ideally I would simply want to dynamically generate the sparkline using values computed in R. Luckily we now have htmlwidgets for R. This library makes it stupidly simple to integrate javascript libraries in R code. It simply up to one who has a use case for integrating a javascript library with R to insert some glue code: and so sparklines was born.

Usage is quite straightforward. I’ll retrieve some weatherdata for Brussels to populate a few sparkline charts.

weatherset <- getWeatherForDate("BRU", start_date = "2015-03-15", end_date = "2015-04-09", opt_verbose=F)
weather_values <- as.vector(weatherset$Mean_TemperatureC)

Let’s have a look at the temperature data.

##  [1]  4  5  8  6  6  6  6  4  4  5  4  3  6  7 10  7  9  7  6  4  6  5  4
## [24]  6  8 10

Now for the sparklines:

sparkline(weather_values, "line")

sparkline(weather_values, "bar")

sparkline(weather_values, "tristate")

sparkline(weather_values, "box")

Almost all configuration options and chart types as specified at are available. And, as a nice side note, it is also possible to use it in combination with Shiny 🙂

Installation instructions, configuration options, sourcecode and other documentation is available at

To leave a comment for the author, please follow the link and comment on their blog: FishyOperations. 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.


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)