Created
March 7, 2022 12:28
-
-
Save meghall06/92852ba12ce59c5848ca695d794683fb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
library(googlesheets4) | |
library(tidyverse) | |
library(showtext) | |
library(lubridate) | |
library(gt) | |
library(cowplot) | |
# add some google fonts via showtext | |
font_add_google(name = "Karla", | |
family = "karla") | |
font_add_google(name = "Righteous", | |
family = "righteous") | |
showtext_auto() | |
# read in data | |
# this has one observation per book with five variables: title, | |
# author, date_finished, genre, subgenre | |
data <- googlesheets4::read_sheet("your-url-here", | |
sheet = "2022") | |
todays_theme <- function () { | |
theme_linedraw(base_size=11, base_family="karla") %+replace% | |
theme( | |
panel.background = element_blank(), | |
plot.background = element_rect(fill = "transparent", color = NA), | |
legend.background = element_rect(fill = "transparent", color = NA), | |
legend.key = element_rect(fill = "transparent", color = NA), | |
axis.ticks = element_blank(), | |
panel.grid.major = element_line(color = "grey90", size = 0.3), | |
panel.grid.minor = element_blank() | |
) | |
} | |
plot <- data %>% | |
mutate(genre = ifelse(genre == "other fiction", "other\nfiction", genre)) %>% | |
ggplot(aes(x = ymd(date_finished), y = genre, color = genre)) + | |
geom_point(size = 4) + | |
scale_color_brewer(palette = "Dark2", guide = "none") + | |
scale_x_date(date_labels = "%b", | |
date_breaks = "1 month", | |
limits = as.Date(c("2021-01-01", "2021-12-31")), | |
expand = expansion(mult = c(0, 0))) + | |
labs(title = "Books I finished in 2021 by date and genre", | |
subtitle = "#TidyTuesday @MeghanMHall") + | |
geom_vline(xintercept = ymd("2021-05-10"), linetype = "dashed") + | |
geom_vline(xintercept = ymd("2021-07-04"), linetype = "dashed") + | |
annotate("text", y = 5, | |
x = ymd("2021-06-07"), | |
label = "way too\nbusy with\nwork :(", size = 3.5, | |
family = "karla") + | |
annotate("text", y = 1, | |
x = ymd("2021-03-08"), | |
label = "not in a nonfiction\nmood I guess??", size = 3.5, | |
family = "karla") + | |
annotate("text", y = 4.3, | |
x = ymd("2021-11-10"), | |
label = "fall: 'tis the season\nfor thrillers", size = 3.5, | |
family = "karla") + | |
todays_theme() + | |
theme(axis.title = element_blank(), | |
panel.grid.major = element_blank(), | |
axis.text = element_text(size = 12), | |
plot.title = element_text(family = "righteous", size = 22)) | |
# this has one observation per book with three variables: | |
# title, genre, comment | |
notes_raw <- googlesheets4::read_sheet("your-url-here", | |
sheet = "2022_top") | |
notes <- notes_raw %>% | |
select(genre, everything()) %>% | |
arrange(desc(genre)) %>% | |
# you can find the hex codes for a built-in palette (I used Dark2 from | |
# ColorBrewer) with ggplot_build(plot)$data | |
mutate(genre = case_when(genre == "other fiction" ~ "#D95F02", | |
genre == "thriller" ~ "#E7298A", | |
genre == "romcom" ~ "#7570B3", | |
genre == "YA" ~ "#66A61E", | |
genre == "nonfiction" ~ "#1B9E77")) | |
# create a very basic table | |
tbl <- notes %>% | |
gt() | |
# make a function to create the colored circles for the genre indicators | |
genre_ind <- function(x) { | |
output <- vector("character", nrow(notes)) | |
for (i in seq_along(notes$genre)) { | |
output[i] <- fontawesome::fa("circle", fill = gt_index(tbl, genre)[i], | |
height = "20px", a11y = "sem") %>% | |
gt::html() | |
} | |
output | |
} | |
table <- tbl %>% | |
# apply my function to create the colored circles in the first column | |
text_transform(locations = cells_body(columns = genre), | |
fn = genre_ind) %>% | |
tab_header(title = md("Non-exhaustive list of books I really enjoyed"), | |
subtitle = "in no particular order") %>% | |
# bold and enlarge the book title column | |
tab_style(style = cell_text(weight = "bold", size = "large"), | |
locations = cells_body(columns = title)) %>% | |
opt_table_font(font = google_font(name = "Karla")) %>% | |
# change the font and size for the title | |
tab_style(style = cell_text(font = google_font(name = "Righteous"), | |
size = "x-large"), | |
locations = cells_title(groups = "title")) %>% | |
tab_options(column_labels.hidden = TRUE) %>% | |
# get rid of allll lines | |
opt_table_lines(extent = "none") | |
table %>% | |
gtsave("table.png") | |
tbl_img <- | |
ggdraw() + | |
draw_image("table.png", scale = 1) | |
plot_grid(plot, tbl_img, | |
rel_widths = c(1.5, 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment