robservable Gallery

Julien Barnier, Kenton Russell

2020-09-28

The goal of this vignette is to show some examples of (hopefully) useful, interesting or fun notebooks usable with robservable.

Draggable Pie/Donut Chart

Yes, I know, pie charts are mostly bad. But the following notebook allows the creation of interactive pie or ‘donut’ charts, with slices optionally ‘draggable’ to rearrange their order.

https://observablehq.com/@juba/draggable-pie-donut-chart

Here is a small example. To display the chart we have to include both the chart and draw cells, and we hide draw as it is only useful to render the plot. We pass our data as a data frame with name and value columns.

df <- data.frame(
  name = rownames(USPersonalExpenditure), 
  value = USPersonalExpenditure[,"1960"]
)
robservable(
  "https://observablehq.com/@juba/draggable-pie-donut-chart",
  include = c("chart", "draw"),
  hide = "draw",
  input = list(data = df),
  width = 700
)

Bar chart race

The following notebook generates animated “bar chart race” charts.

https://observablehq.com/@juba/bar-chart-race

To use it from robservable you have to place your data in a data frame with the following columns :

Optionally, if you want the displayed date value to be different than the one used in your dataset (for example if you iterate over monthly data but prefer to only display the year), you can add a corresponding date_label column.

library(readr)
library(dplyr)
library(tidyr)

## Load Covid-19 data from Johns Hopkins Github repository
d <- read_csv("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv")

## Reformat data 
d <- d %>%
  select(-`Province/State`, -Lat, -Long) %>%
  rename(id = `Country/Region`) %>%
  group_by(id) %>%
  summarise(across(everything(), sum)) %>%
  pivot_longer(-id, names_to = "date") %>%
  mutate(date = as.character(lubridate::mdy(date)))

We can then generate the chart with the following robservable call. Note that we have to include several cells : the chart itself, the draw cell which updates it, the date play/pause control, and the CSS styles.

## Generate chart
robservable(
  "https://observablehq.com/@juba/bar-chart-race",
  include = c("viewof date", "chart", "draw", "styles"),
  hide = "draw",
  input = list(
    data = d,
    title = "COVID-19 deaths",
    subtitle = "Cumulative number of COVID-19 deaths by country",
    source = "Source : Johns Hopkins University"
  ),
  width = 700,
  height = 710
)