Creating Flourish graphs in R

library(dplyr)
library(tidyr)
library(flourishcharts)

Flourish primarily depends on two main functions in R/Python. They are flourish and a bind_..._data() function. The ... depends on the chart type. An R user can specify their preferred chart type and bind data columns to the graph. In R, A list of Flourish functions can be found by running flourishcharts::, typing ?flourishcharts in your console, or heading over to the references page in this site.

The following chunks show some example charts using Gapminder data, with my accompanying notes.

The flourishcharts package provides the data flourish_api_documentation frame with metadata about each graph (template ID, version, a simplified chart name for end users, and a URL to Flourish’s API documentation).

R

head(flourish_api_documentation)
##    template_name    chart_type
## 1          audio         audio
## 2 bar chart race      bar_race
## 3   bubble chart        bubble
## 4     calculator    calculator
## 5          cards         cards
## 6  chord diagram chord_diagram
##                                                               url
## 1           https://app.flourish.studio/@flourish/audio/1.6.0#api
## 2 https://app.flourish.studio/@flourish/bar-chart-race/16.1.2#api
## 3    https://app.flourish.studio/@flourish/bubble-chart/3.1.2#api
## 4      https://app.flourish.studio/@flourish/calculator/1.7.1#api
## 5          https://app.flourish.studio/@flourish/cards/11.1.2#api
## 6   https://app.flourish.studio/@flourish/chord-diagram/8.6.1#api

flourishcharts works by requiring two functions to plot Flourish graphs:

  • flourish() in R or Flourish() in Python:
    • chart_type (mandatory arg) – a string that must match a value in flourish_api_documentation["chart_type"].
    • api_key (mandatory arg) – Flourish API key, defaults to Sys.getenv("FLOURISH_API_KEY") or os.environ.get("FLOURISH_API_KEY").
    • chart_description (optional arg) – a string with the chart description for screen readers.

And secondly, a binding data function which performs the same function as in flourish.studio, whereby the user matches data columns to the graph properties.

To start with, a scatter plot is laid out as follows. If a user wants to add additional details they can by running set_..._details()–in this case, set_scatter_details().

Each function argument pipes into the other and takes on the additional arguments to the graph. The pipe can be the base pipe |> or the maggrittr pipe %>%. In Python, users can append new functions to the assigned graph.

Scatterplot

scatterplot <- flourish(
  chart_type = "scatter"
) |>
  bind_scatter_data(
    data = gapminder,
    x = "gdpPercap",
    y = "lifeExp",
    slider = "year",
    size = "pop",
    color = "continent",
    metadata = c("country", "year")
  )
scatterplot

Line graph

R

line_data <- gapminder |>
  filter(country %in% c(
    "Australia",
    "New Zealand",
    "United States",
    "Rwanda",
    "Sierra Leone",
    "Indonesia",
    "Brazil"
  )) |>
  select("country", "year", "lifeExp") |>
  pivot_wider(id_cols = "year", names_from = "country", values_from = "lifeExp")

line_chart <- flourish(
  chart_type = "line"
) |>
  bind_line_bar_pie_data(
    data = line_data,
    label = "year",
    value = colnames(line_data[, c(2:8)])
  ) |>
  set_line_bar_pie_details(
    chart_layout_title = "Life expectancy from the 1950s to 2007",
    chart_layout_subtitle = "Selected countries include Australia, New Zealand, the US, Rwanda, Indonesia, Sierra Leone, and Brazil."
  )
line_chart

Bar chart race

bcr_data <- gapminder |>
  filter(country %in% c(
    "Australia",
    "New Zealand",
    "United States",
    "Rwanda",
    "Sierra Leone",
    "Indonesia",
    "Brazil"
  )) |>
  select(c("country", "continent", "year", "lifeExp")) |>
  pivot_wider(id_cols = c("country", "continent"), names_from = "year", values_from = "lifeExp")

bcr <- flourish("bar_race") |>
  bind_bar_chart_race_data(
    data = bcr_data,
    label = "country",
    values = colnames(bcr_data[, c(3:14)]),
    category = "continent"
  ) |>
  set_bar_chart_race_details(
    chart_layout_title = "Life expectancy from the 1950s to 2007",
    chart_layout_subtitle = "Selected countries include Australia, New Zealand, the US, Rwanda, Indonesia, Sierra Leone, and Brazil."
  )
bcr