Last updated: 2022-04-18
Checks: 7 0
Knit directory: myTidyTuesday/
This reproducible R Markdown analysis was created with workflowr (version 1.7.0). The Checks tab describes the reproducibility checks that were applied when the results were created. The Past versions tab lists the development history.
Great! Since the R Markdown file has been committed to the Git repository, you know the exact version of the code that produced these results.
Great job! The global environment was empty. Objects defined in the global environment can affect the analysis in your R Markdown file in unknown ways. For reproduciblity it’s best to always run the code in an empty environment.
The command set.seed(20210907)
was run prior to running
the code in the R Markdown file. Setting a seed ensures that any results
that rely on randomness, e.g. subsampling or permutations, are
reproducible.
Great job! Recording the operating system, R version, and package versions is critical for reproducibility.
Nice! There were no cached chunks for this analysis, so you can be confident that you successfully produced the results during this run.
Great job! Using relative paths to the files within your workflowr project makes it easier to run your code on other machines.
Great! You are using Git for version control. Tracking code development and connecting the code version to the results is critical for reproducibility.
The results in this page were generated with repository version 260b95d. See the Past versions tab to see a history of the changes made to the R Markdown and HTML files.
Note that you need to be careful to ensure that all relevant files for
the analysis have been committed to Git prior to generating the results
(you can use wflow_publish
or
wflow_git_commit
). workflowr only checks the R Markdown
file, but you know if there are other scripts or data files that it
depends on. Below is the status of the Git repository when the results
were generated:
Ignored files:
Ignored: .Rhistory
Ignored: .Rproj.user/
Ignored: catboost_info/
Ignored: data/.Rhistory
Ignored: data/CNHI_Excel_Chart.xlsx
Ignored: data/Chicago.rds
Ignored: data/CommunityTreemap.jpeg
Ignored: data/Community_Roles.jpeg
Ignored: data/SeriesReport-20220414171148_6c3b18.xlsx
Ignored: data/Weekly_Chicago_IL_Regular_Reformulated_Retail_Gasoline_Prices.csv
Ignored: data/YammerDigitalDataScienceMembership.xlsx
Ignored: data/YammerMemberPage.rds
Ignored: data/YammerMembers.rds
Ignored: data/acs_poverty.rds
Ignored: data/austinHomeValue.rds
Ignored: data/austinHomeValue2.rds
Ignored: data/df.rds
Ignored: data/fmhpi.rds
Ignored: data/grainstocks.rds
Ignored: data/hike_data.rds
Ignored: data/nber_rs.rmd
Ignored: data/netflixTitles2.rds
Ignored: data/raw_weather.RData
Ignored: data/us_states.rds
Ignored: data/us_states_hexgrid.geojson
Ignored: data/weatherstats_toronto_daily.csv
Untracked files:
Untracked: analysis/2022_03_25_TimeSeriesShowcase.Rmd
Untracked: code/YammerReach.R
Untracked: code/chicago.R
Untracked: code/googleCompute.R
Untracked: code/work list batch targets.R
Untracked: environment.yml
Unstaged changes:
Deleted: analysis/2022_02_11_tabular_playground.Rmd
Deleted: analysis/2022_04_18.qmd
Modified: code/_common.R
Note that any generated files, e.g. HTML, png, CSS, etc., are not included in this status report because it is ok for generated content to have uncommitted changes.
These are the previous versions of the repository in which changes were
made to the R Markdown (analysis/2022_04_18.Rmd
) and HTML
(docs/2022_04_18.html
) files. If you’ve configured a remote
Git repository (see ?wflow_git_remote
), click on the
hyperlinks in the table below to view the files as they were in that
past version.
File | Version | Author | Date | Message |
---|---|---|---|---|
Rmd | 260b95d | opus1993 | 2022-04-18 | Supreme Court |
The #30DayChartChallenge is running once again over on Twitter with lots of inspirational data visualization content. One of the pros to follow is Tanya Shapiro. She poses a question here that prompted my short post today.
I think ggiraph is my new favorite #RStats library. Easy way to make your ggplots interactive. "Interactified" last week's pictogram of Supreme Court Justices. 🧑⚖️
— Tanya Shapiro (@tanya_shapiro) April 15, 2022
Anyone have any a work-around for creating tooltip hovers for geom_image?https://t.co/6JdRTFItBN
I had attempted to work on her challenge with the new Quarto document
framework, github pages, and the workflowr
site management
system. More on what I discovered at the end. First, let’s load the open
source packages:
suppressPackageStartupMessages({
library(rvest)
library(tidyverse)
library(ggimage)
library(ggiraph)
})
Her graphic depicts seated justices through the history of the United States Supreme Court. The visual includes Justice Ketanji Brown Jackson, who was recently confirmed on April 7th, 2022. Once sworn in, Jackson will be the 116th justice and the first Black woman to sit on the Supreme Court.
Emojis are used to denote gender and race of each justice. Colored points are based on nominating president party affiliation (e.g. red for Republican).
Let’s make a link to the data source at wikipedia:
url <- 'https://en.wikipedia.org/wiki/List_of_justices_of_the_Supreme_Court_of_the_United_States'
Scraping the page html table from the wikipedia page is easy with
rvest
:
sc_justices_raw <- url |>
read_html() |>
html_elements(".wikitable") |>
html_table(trim = TRUE, fill = TRUE) |>
pluck(1)
names(sc_justices_raw) <-
c(
"index",
"blank",
"justice",
"state",
"position",
"succeeded",
"confirmed",
"tenured",
"length",
"nominated_by"
)
sc_justices_raw <- sc_justices_raw |>
mutate(id = row_number()) |>
head(-1) # remove the last row of the dataframe
We need to clean the dataset a little to parse the dates and categorize justices.
justices <- sc_justices_raw |>
#some justices were promoted to chief justice later, get distinct justices
distinct(justice,
.keep_all = TRUE) |>
select(-index, -blank) |>
separate(justice, into = c("name", "born_died"), sep = "\\(") |>
mutate(
index = row_number(),
born_died = str_replace(born_died, "\\)", ""),
position = str_replace(position, "\\s*\\[[^\\)]+\\]", ""),
position = case_when(
position == "ChiefJustice" ~ "Chief Justice",
position == "AssociateJustice" ~ "Associate Justice"
),
confirmed = str_replace(confirmed, "\\([^()]*\\)", ""),
confirmed = str_replace(confirmed, "\\s*\\[[^\\)]+\\]", ""),
confirmed = as.Date(confirmed, '%B %d, %Y'),
demo = case_when(
name %in% c('Thurgood Marshall', 'Clarence Thomas') ~ 'black_male',
name %in% c(
"Sandra Day O'Connor",
"Ruth Bader Ginsburg",
"Elena Kagan",
"Amy Coney Barrett"
) ~ 'white_female',
name == 'Sonia Sotomayor' ~ 'hispanic_female',
TRUE ~ 'white_male'
)
)
Similarly, getting a list of presidents with party affiliation is
easy with rvest
and wikipedia:
pres_url <- 'https://en.wikipedia.org/wiki/List_of_presidents_of_the_United_States'
#scrape president data
pres_raw <- pres_url |>
read_html() |>
html_elements(".wikitable") |>
html_table(trim = TRUE, fill = TRUE) |>
pluck(1) |>
head(-1)
names(pres_raw) <-
c(
"index",
"portrait",
"name_birth_death",
"term",
"party_misc",
"party",
"election",
"vp"
)
As above, the presidents data needs a little bit of cleaning for party affiliation.
presidents <- pres_raw |>
select(name_birth_death, party) |>
separate(name_birth_death, into = c("name","birth_death"), sep = "\\(") |>
mutate(party = str_replace(party, "\\s*\\[[^\\)]+\\]",""),
name = str_replace(name, "\\s*\\[[^\\)]+\\]","")
) |>
distinct(name,party) |>
mutate(index = row_number()) |>
distinct(index,
.keep_all = TRUE) |>
select(-index)
A this point, we join presidents to justices and add Justice Jackson.
justices <- justices |>
left_join(presidents, by = c("nominated_by" = "name")) |>
bind_rows(
data.frame(
name = "Ketanji Brown Jackson",
born_died = NA,
state = NA,
position = "Associate Justice",
succeeded = "Breyer",
confirmed = as.Date('2022-04-07'),
tenured = NA,
length = NA,
nominated_by = "Joe Biden",
id = NA,
index = 116,
demo = "black_female",
party = "Democratic"
)
)
To build the plot, position variables will be needed to give each justice a tile on a grid.
justices <- justices |>
mutate(y = (index - 1) %/% 10, # truncated division wihtout remainder
x = (index - 1) %% 10) # the remainder
Credit for the trick to get png images for emojis goes to Emil Hvitfeldt, here
white_male_icon <- 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/325/man-judge-light-skin-tone_1f468-1f3fb-200d-2696-fe0f.png'
black_male_icon <- 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/325/man-judge-medium-dark-skin-tone_1f468-1f3fe-200d-2696-fe0f.png'
white_female_icon_2 <- 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/325/woman-judge-medium-light-skin-tone_1f469-1f3fc-200d-2696-fe0f.png'
white_female_icon <- 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/325/woman-judge-light-skin-tone_1f469-1f3fb-200d-2696-fe0f.png'
hispanic_female_icon <- 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/325/woman-judge-medium-skin-tone_1f469-1f3fd-200d-2696-fe0f.png'
black_female_icon <- 'https://emojipedia-us.s3.dualstack.us-west-1.amazonaws.com/thumbs/120/apple/325/woman-judge-medium-dark-skin-tone_1f469-1f3fe-200d-2696-fe0f.png'
Last, lets create the new variable in the justices data frame to match up with the icon html information.
justices <- justices |>
mutate(
icon = case_when(
name == "Sandra Day O'Connor" ~ white_female_icon_2,
demo == "white_male" ~ white_male_icon,
demo == "white_female" ~ white_female_icon,
demo == "black_male" ~ black_male_icon,
demo == "black_female" ~ black_female_icon,
demo == "hispanic_female" ~ hispanic_female_icon
),
party_group = as_factor(case_when(
party %in% c(
"Federalist",
"Whig",
"Unaffiliated",
"National Union",
"National Republican"
) ~ "Other",
TRUE ~ party
)),
party_group = fct_relevel(party_group,
c("Democratic-Republican",
"Democratic",
"Republican",
"Other"))
)
Building out the ggplot is simple. Shapiro uses a Google font in her
work that I have not downloaded. I’ve added a
geom_point_interactive()
tooltip from the
ggiraph
package.
gg <- ggplot(data = justices,
mapping = aes(x = x + 1, y = y + 1)) +
geom_point(size = 12, aes(color = party_group), alpha = 0.9) +
scale_color_manual(
values = c("#EBB027", "#266DD3", "#CC3333", "grey"),
guide = guide_legend(
title.position = "top",
title.hjust = 0.5,
override.aes = list(size = 3)
)
) +
geom_image(aes(image = icon)) +
geom_text(aes(label = index), vjust = 4.8, size = 2) +
geom_point_interactive(aes(tooltip = glue::glue(
"{ name } \n { tenured } \n Nominated by { nominated_by }")
),
alpha = 0.002) +
scale_y_reverse(limits = c(12.5, 1)) +
labs(
title = "United States Supreme Court Justices",
subtitle = "Emoji Portraits of all 116 Justices",
color = "NOMINATING PRESIDENT PARTY AFFILIATION",
caption = "Data from Wikipedia | Original Chart by @tanya_shapiro"
) +
theme(
legend.position = "top",
legend.title = element_text(size = 8),
legend.key = element_rect(fill = NA),
plot.margin = margin(
r = 20,
l = 20,
t = 10,
b = 5
),
plot.title = element_text(hjust = 0.5, face = "bold"),
plot.subtitle = element_text(hjust = 0.5),
plot.background = element_blank(),
panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text = element_blank(),
axis.title = element_blank()
)
girafe(ggobj = gg,
width_svg = 6,
height_svg = 8)
Thank you to
workflowr
, who have more work to
do make wflow_publish()
work with quarto.
sessionInfo()
R version 4.1.2 (2021-11-01)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 10 x64 (build 22000)
Matrix products: default
locale:
[1] LC_COLLATE=English_United States.1252
[2] LC_CTYPE=English_United States.1252
[3] LC_MONETARY=English_United States.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United States.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] ggiraph_0.8.2 ggimage_0.3.0 forcats_0.5.1 stringr_1.4.0
[5] dplyr_1.0.8 purrr_0.3.4 readr_2.1.2 tidyr_1.2.0
[9] tibble_3.1.6 ggplot2_3.3.5 tidyverse_1.3.1 rvest_1.0.2
[13] workflowr_1.7.0
loaded via a namespace (and not attached):
[1] httr_1.4.2 sass_0.4.1 jsonlite_1.8.0 modelr_0.1.8
[5] bslib_0.3.1 assertthat_0.2.1 getPass_0.2-2 yulab.utils_0.0.4
[9] selectr_0.4-2 cellranger_1.1.0 yaml_2.3.5 pillar_1.7.0
[13] backports_1.4.1 glue_1.6.2 uuid_1.0-4 digest_0.6.29
[17] promises_1.2.0.1 colorspace_2.0-3 ggfun_0.0.6 htmltools_0.5.2
[21] httpuv_1.6.5 pkgconfig_2.0.3 tweetrmd_0.0.9 broom_0.8.0
[25] haven_2.4.3 magick_2.7.3 scales_1.2.0 processx_3.5.3
[29] ggplotify_0.1.0 whisker_0.4 later_1.3.0 tzdb_0.3.0
[33] git2r_0.30.1 farver_2.1.0 generics_0.1.2 ellipsis_0.3.2
[37] withr_2.5.0 cli_3.2.0 magrittr_2.0.3 crayon_1.5.1
[41] readxl_1.4.0 evaluate_0.15 ps_1.6.0 fs_1.5.2
[45] fansi_1.0.3 xml2_1.3.3 tools_4.1.2 hms_1.1.1
[49] lifecycle_1.0.1 munsell_0.5.0 reprex_2.0.1 callr_3.7.0
[53] compiler_4.1.2 jquerylib_0.1.4 systemfonts_1.0.4 gridGraphics_0.5-1
[57] rlang_1.0.2 grid_4.1.2 rstudioapi_0.13 htmlwidgets_1.5.4
[61] labeling_0.4.2 rmarkdown_2.13 gtable_0.3.0 DBI_1.1.2
[65] curl_4.3.2 R6_2.5.1 lubridate_1.8.0 knitr_1.38
[69] fastmap_1.1.0 utf8_1.2.2 rprojroot_2.0.3 stringi_1.7.6
[73] Rcpp_1.0.8.3 vctrs_0.4.0 dbplyr_2.1.1 tidyselect_1.1.2
[77] xfun_0.30