Assignment 4 - Tables

Make a dynamic figure. Describe the following:

Source: https://apps.bea.gov/iTable/index_regional.cfm

This data was sourced from the Bureau of Economic Analysis and looks to understand the trends in personal income per capita across the 50 states and Washington DC.

The data has been tabled in two ways. The first leverages KableExtra and serves as a brief snapshot of the actual for 2020 and the percent change vs. 2019. This features a lightbar scroll option, and is meant to be taken in quickly by the viewer.

To further contextualize this, I created a second table using Reactable which allows for sorting by metric and additional information such as 2019 and 2016 actuals and percent change vs. 2016. In short the first table is to be used as a point in time for short-term trends, while the second allows us to manipulate the data and understand both the short and long-term trends quickly with our sorting function.

Step 1: Load Packages

library(kableExtra)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6     ✔ purrr   0.3.4
## ✔ tibble  3.1.8     ✔ dplyr   1.0.9
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter()     masks stats::filter()
## ✖ dplyr::group_rows() masks kableExtra::group_rows()
## ✖ dplyr::lag()        masks stats::lag()
library(dplyr)
library(sparkline)
library(reactable)
library(DT)

Load and manipulate data

#load and mutate data
econ <- read.csv("Regional - Level and Percent2.csv")

eco1 <- econ %>%
  filter(Description == "  Real per capita personal income 4/")%>%
  mutate(p5y = ((X2020 - X2016)/X2016)*100)

Create Table(s)

t_eco <- eco1 %>%
  select(GeoName, Levels, Per_Change_2020)%>%
  kbl(col.name = c("State/Region", "2020 Actuals", "Percent Change from 2019"),
      align = c("l","r","r"),
      digits = 1) %>% #check on this
  kable_styling(font_size = 18, fixed_thead = T) %>%
  add_header_above(c(" ","Real Per Capita Personal Income" = 2), font_size = 14) %>%
  kable_paper(lightable_options = c("hover","condensed"), full_width = T, html_font = "helvetica", font_size = 12)

t_eco 
Real Per Capita Personal Income
State/Region 2020 Actuals Percent Change from 2019
Alabama 46963 4.3
Alaska 55470 1.9
Arizona 45193 6.6
Arkansas 47765 3.5
California 57347 7.2
Colorado 55911 2.9
Connecticut 68533 2.1
Delaware 51689 4.3
District of Columbia 70040 3.7
Florida 49853 3.1
Georgia 49392 5.4
Hawaii 47234 4.0
Idaho 48216 7.5
Illinois 56482 4.6
Indiana 50624 5.9
Iowa 52969 5.7
Kansas 54773 5.2
Kentucky 47551 7.0
Louisiana 49483 4.6
Maine 50516 6.1
Maryland 56578 3.3
Massachusetts 65853 4.9
Michigan 51071 8.2
Minnesota 56696 5.1
Mississippi 43284 6.5
Missouri 50404 5.1
Montana 52054 7.3
Nebraska 55891 6.0
Nevada 49914 5.3
New Hampshire 58342 3.7
New Jersey 59594 4.1
New Mexico 45637 8.2
New York 60936 4.6
North Carolina 49396 4.8
North Dakota 60286 4.9
Ohio 52758 7.5
Oklahoma 49254 1.2
Oregon 49485 7.1
Pennsylvania 57030 6.4
Rhode Island 53859 6.0
South Carolina 47252 5.0
South Dakota 58414 8.1
Tennessee 49955 2.8
Texas 49945 2.0
Utah 49388 7.5
Vermont 53726 5.3
Virginia 55333 3.6
Washington 56385 5.2
West Virginia 46130 5.2
Wisconsin 53798 5.4
Wyoming 60463 1.5
t_reac <- eco1 %>%
  select(GeoName,Levels, X2019, X2016, Per_Change_2020, p5y)%>%
  reactable(columns = list(
    GeoName = colDef(name = "State"),
    Levels = colDef(name = "2020 Per Capita Income"),
    X2019 = colDef(name = "2019 Per Capita Income"),
    X2016 = colDef(name = "2016 Per Capita Income"),
    Per_Change_2020 = colDef(name = "Percent Change from 2019", format = colFormat(digits = 1)),
    p5y = colDef(name = "Percent Change from 2016", format = colFormat(digits = 1))),
  defaultSorted="GeoName",
  style = list(fontSize = "1rem"))
t_reac