Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Last active September 28, 2021 13:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/c73fe44ffff242f80210d5b8841ab9e2 to your computer and use it in GitHub Desktop.
Save timelyportfolio/c73fe44ffff242f80210d5b8841ab9e2 to your computer and use it in GitHub Desktop.
trelliscope and sparkline example
license: mit

Thanks so much to Ryan Hafen for his R and JS work on trelliscopejs.

I did not have enough time to figure out how to save the trelliscopejs widget as html, so I took the easy route with an animated GIF. Here is the code to reproduce this simple example.

Code

library(trelliscopejs)
library(dplyr)
library(tidyr)
library(sparkline)

mpg %>%
  group_by(manufacturer, class) %>%
  summarise(
    mean_city_mpg = cog(mean(cty), desc = "Mean city mpg"),
    mean_hwy_mpg = cog(mean(hwy), desc = "Mean highway mpg"),
    panel = panel(
      sparkline(
        unname(c(
          mean(.$cty),
          mean(cty),
          rev(quantile(.$cty,probs=c(1/3,2/3,1)))
        )),
        type="bullet",
        chartRangeMin=0,
        chartRangeMax=max(.$cty)
      )
    )
  ) %>%
  trelliscope(name = "city_vs_highway_mpg", nrow = 4, ncol = 8)

<!DOCTYPE html>
<head>
<meta charset="utf-8">
<style>
body { margin:0;position:fixed;top:0;right:0;bottom:0;left:0; }
</style>
</head>
<body>
<h1>Animated GIF of trelliscopejs and sparkline</h1>
<img alt="animated screenshot of trelliscope and sparkline" src="http://imgur.com/download/I0xOptL" width=960></img>
</body>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment