Using sparklines in R projects

[This article was first published on Appsilon Data Science - R language, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

First post on our blog! We welcome all data enthusiasts!

Today we are going to show how to use tinny but useful library called sparkline. Sparkline itself is a small widget, using jQuery under the hood1.

Both technologies – jQuery and the widget itselt are implemented in the JavaScript. When it comes to visualizations JavaScript has superpowers and almost always exceeds anything we can get with other stuff.

That’s the main reason why people using Python, R or anything else want to use JavaScript. In case of R it is possible thanks to htmlwidgets2 package, which we are going to describe in another article.


Lets get straight to the point and look at some examples.

Install: As package is not available at CRAN we need to install it with devtools3:


We need some data to play with. In this case we’re going to use weather forecasts for New York:

warsawWeather <- getWeatherForecast(getOption("DarkSkyApiKey"), city = 'New York')
nextWeekTemperature <- warsawWeather$$temperatureMaxCelsius

Types of charts

Sparkline has several flavors.

  1. Bar chart where we can adjust bar colors
  2. Boxplot:
  3. Bullet graph:
  4. And of course pie chart:

You can also use two types tristate and discrete, however I’ve never used those two until this time (check out documentation if you curious!).

In examples I’ve shown how you can pass arguments to plots. Precise explaination of all options and configuration is available in jQuery plugin documentation.


It gets even better if we embed sparkline within table with data. Familiar with formattable? You will be! :cake:

Large small Rank
A d
B c
C b
D a

  data.frame(Large = LETTERS[1:4], small = letters[4:1], Rank = NA),
    "Rank"=function(z) {
        paste0(text='`r sparkline(sample(5))`')
        ,function(md) knitr::knit(text=md, quiet=T)

I had to use small trick here. Who knows what I did?

Formattable lets you do that and a lot more when it comes to presenting your data in readable form. It has its own htmlwidget as well.

What next?

For those of you who think that charts in the browser are mainstream I recommend working with command line. spark Not the best choice when it comes to the name of the library. is a simillar package to sparkline, but it renders charts straight in the command line!

## ▄▂▁▃▃▁▅▆▅▇▇▆▅▃▄▄▅▃▂▁▁▂▃▃▃▃▂▅▅▅▄████
## ▁▃▁▂▂▁▂▄▄▆▇▇▇██▇█▇▇▆▆▇▇▇█▇▆▇▇▇▆▇██▇
## ▅▄▁▁▂▁▂▄▄▄▄▄▄▄▄▄▅▅▆▅▄▄▄▅▅▄▄▆▆▆▆████
## ▁▁▁▂▂▂▂▃▄▃▅▆▆▅▅▆▇▇▇▇▇▇▇▇█▇▆▇█▆▆▇███
  1. jQuery is an old but still very popular library that allows to quickly implement various actions on websites (like animations, DOM manipulations, async calls). jQuery is replaced more and more often with some modern tools, but for some basic projects this is still good way to go.

  2. Not the patient type of person? htmlwidgets

  3. I assume you have devtools installed. If not run install.packages(“devtools”). If just like me you prefer to use most recent version of packages look at the in github repository devtools.

To leave a comment for the author, please follow the link and comment on their blog: Appsilon Data Science - R language. offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)