Article Image
Article Image
read

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

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

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

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.

Examples

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

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

library(devtools)
# install_github('htmlwidgets/sparkline')

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

library(SmarterPoland)
library(sparkline)
warsawWeather <- getWeatherForecast(getOption("DarkSkyApiKey"), city = "New York")
nextWeekTemperature <- warsawWeather$by.day$temperatureMaxCelsius
sparkline(nextWeekTemperature)

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!).

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

Boosting

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

Large small Rank
A d
B c
C b
D a
library(formattable)
library(sparkline)

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

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

Formattable lets you do that and 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? htmlwidgets 

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

Blog Logo

Filip Stachura


Published

Image

Appsilon Data Science Blog

How to create and use technology to deliver business results.

Back to the top