Conditionally Formatted State Transition Matrices - Table Contest Submission

Conditionally Formatted State Transition Matrices

Authors: Michael Thomas (Ketchbrook Analytics)

Abstract: State Transition Matrices are a powerful type of data visualization commonly found in financial risk management. In this tutorial, we demonstrated how to calculate this type of matrix using the {migrate} package and develop a beautiful, presentation-ready table representing your state transition matrix using the {gt} package.

Full Description: For this year's contest, we decided to make a table submission that aligned with the recent v0.4.0 release of the {migrate} R package for building state transition matrices. If you are not familiar with state transition matrices, they are a common type of data visualization used in risk management (we recommend checking out {migrate}'s GitHub repository to learn more).

You can view our blog post about how we built beautiful state transition matrices (and applied some tricky conditional formatting) using {migrate} + {gt}:

https://mthomas-ketchbrook.github.io/migrate_table/

Unique Aspects of this Project

There are a few unique aspects of this project that may be of interest to those working with the {gt} package:

  • Custom Conditional Formatting
  • Embedding Images in Table Header
  • Accessing the Socrata API via {RSocrata}

Custom Conditional Formatting

A somewhat complicated aspect to conditionally formatting these transition matrices is that the formatting needs to be applied to a varying number of cells across all columns. We developed a custom function fmt_migrate() to make the formatting logic agnostic and flexible.

Embedding Images in Table Header

To spruce up our {gt} tables, we decorated the table headers with images embedded with clickable hyperlinks. You can see how we went about doing that in our blog post.

Accessing the Socrata API via {RSocrata}

Lastly, we concluded our analysis by analyzing Moody's bond rating data for each town in the State of Connecticut. This open dataset served as a great use case for applying the power of {migrate} + {gt} together.

The {RSocrata} package provides an easy way to query data from the Socrata API, which backs many government open data repositories (including Connecticut's).


Table Type: static-HTML
Submission Type: Tutorial
Table: tables_contest_2021_blog_post.knit
Repo: https://github.com/mthomas-ketchbrook/migrate_table
RStudio Cloud:
DT package used: false
gt package used: true
reactable package used: false
flextable package used: false
huxtable package used: false
kableExtra package used: false
Other packages: {migrate}, {tibble}, {dplyr}, {scales}, {RSocrata}

2 Likes