# install.packages(c("readxl", "janitor")) library(tidyverse) library(readxl) library(janitor) d <- read_excel("data-raw/fisheries_data_liberia.xlsx") d |> glimpse() # clean names d <- d |> janitor::clean_names() d |> glimpse() # how many records per year d |> count(year) # the data is by month so lets create a "proper" date that R understands d <- d |> # this is a two step process: # first we generate something like 2019-01-15" (inside the paste0 stuff # then we convert that to proper R date format using ymd() mutate(date = ymd(paste0(year, "-", month_number, "-01"))) # we now have redundant variables (year, month, month_number) so lest get rid of them d <- d |> select(date, segment, species, latin = scientific_names, # i like shorter variable names kg = total_kg, # again something shorter lrd = value_of_prod_lrd) # ditto d |> glimpse() # lets just do a sum of catches by date and plot it d |> group_by(date) |> reframe(kg = sum(kg, na.rm = TRUE)) |> ggplot(aes(date, kg)) + geom_col() + labs(x = "Month", y = "Kg") # same thing by "segment" d |> group_by(date, segment) |> reframe(tonnes = sum(kg, na.rm = TRUE) / 1000) |> ggplot(aes(date, tonnes, fill = segment)) + geom_col() + labs(x = "Month", y = "Tonnes") + # nicer colour than the default scale_fill_brewer(palette = "Set1") # The catch by species catch.by.species <- d |> group_by(species) |> reframe(tonnes = sum(kg, na.rm = TRUE) / 1000) |> arrange(desc(tonnes)) catch.by.species |> knitr::kable() # lump all but top 10 species into "other" # first get the top ten top10 <- catch.by.species |> slice(1:10) |> # first 10 rows pull(species) # extract species to make a "vector" top10 # catch by top 10 and then the rest d |> mutate(species = ifelse(species %in% top10, species, "rest")) |> group_by(date, species, segment) |> reframe(tonnes = sum(kg, na.rm = TRUE) / 1000) |> ggplot(aes(date, tonnes, fill = segment)) + geom_col() + # separate plot for each species, separate scale on each panel facet_wrap(~ species, scales = "free_y") + labs(x = "Month", y = "Tonnes") + # nicer colour than the default scale_fill_brewer(palette = "Set1")