Skip to content

Instantly share code, notes, and snippets.

@noamross
Last active March 5, 2018 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save noamross/68c75929d904a1e55fbcb93f7c4b8138 to your computer and use it in GitHub Desktop.
Save noamross/68c75929d904a1e55fbcb93f7c4b8138 to your computer and use it in GitHub Desktop.
# Noam's take on Bob's lobseter plot
library(stringi)
library(pdftools)
library(hrbrthemes) #devtools::install_github("hrbrmstr/hrbrthemes"); hrbrthemes::import_roboto_condensed()
library(tidyverse)
# A different file provided by the state includes fishing effort
lobster_tbl <- "https://www.maine.gov/dmr/commercial-fishing/landings/documents/lobster.table.pdf"
lobster_tbl_fil <- basename(lobster_tbl)
if (!file.exists(lobster_tbl_fil)) download.file(lobster_tbl, lobster_tbl_fil)
# read in the PDF
lobster_pgs <- pdftools::pdf_text(lobster_tbl_fil)
map(lobster_pgs, stri_split_lines) %>% # split each page into lines
flatten() %>%
flatten_chr() %>%
keep(stri_detect_fixed, "$") %>% # keep only lines with "$" in them
stri_trim_both() %>% # clean up white space %>%
stri_replace_all_regex("\\s*\\,\\s*", ",") %>%
`[`(41:119) %>%
stri_split_regex("\ +", simplify = TRUE) %>% # get the columns
as_data_frame() %>%
mutate_at(c("V6", "V8"), lucr::from_currency) %>% # turn the formatted text into numbers
set_names(c("year", "species", "tons", "lbs", "lbs_mil", "val", "val_mil", "price_per_lb", "no_licenses", "no_trap", "water_temp_C")) %>% # better column names
mutate_at(funs(readr::parse_number), .vars=c("tons", "lbs", "lbs_mil", "val_mil", "no_licenses", "no_trap", "water_temp_C")) %>%
mutate(year = as.Date(sprintf("%s-01-01", year))) -> lobsters
arrange(lobsters, year) %>%
mutate(year_lab = lubridate::year(year)) %>%
mutate(highlight = ifelse(year_lab == 2017, "2017", "Other")) %>% # so we can highlight 2017
ggplot(aes(no_trap*1e6, tons)) +
geom_path() +
geom_label(
aes(label = year_lab, color = highlight, size = highlight),
family = font_ps, show.legend = FALSE
) +
scale_y_comma(name = "Tons of Lobsters Landed →", limits = c(0, 70000)) +
scale_x_comma(name = "Fishing effort (Number of Traps) →", limits = c(0, 3.5e6)) +
scale_color_manual(values = c("2017" = "#742111", "Other" = "#2b2b2b")) +
scale_size_manual(values = c("2017" = 6, "Other" = 4)) +
labs(
title = "Maine Lobster Fishing Effort and Landings — Lobster (1939-2017)",
subtitle = "All counties",
caption = "The 2002 & 2003 landings may possibly reflect the increased effort by DMR to collect voluntary landings from some lobster dealers;\nLobster reporting became mandatory in 2004 for all Maine dealers buying directly from harvesters.\nSource: <https://www.maine.gov/dmr/commercial-fishing/landings/historical-data.html>"
) +
theme_ipsum_ps(grid = "XY")
@noamross
Copy link
Author

noamross commented Mar 5, 2018

screenshot 2018-03-05 10 09 10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment