say("MPAs - Are they worth the swim?", "fish")
## 
##  --------------------------------- 
## < MPAs - Are they worth the swim? >
##  --------------------------------- 
##        \
##         \
## 
##   ><((((Âş>  ><((((Âş>  ><((((Âş>  ><((((Âş>  ><((((Âş>
##       Kiyoko Gotanda
## 
Open source data was utilized for this class exercise from the study (Moland et al., 2013):
Moland, Even, et al. (2013). Lobster and cod benefit from small-scale northern marine protected areas: inference from an empirical before–after control-impact study. Proceedings of the Royal Society B: Biological Sciences. https://doi.org/10.1098/rspb.2012.2679
Load packages
library(tidyverse); library(janitor); library(here) #BASICS   
library(gt); library(gtExtras) # TABLES
library(praise); library(cowsay); library(beepr) # FUN
library(ggridges); library(bbplot); library(ggthemes) # THEMES
library(RColorBrewer); library(viridis) # COLOR
### Use code below to install packages: `bbplot` & `gtExtras` ###
# install.packages('devtools')
# devtools::install_github('bbc/bbplot')
# remotes::install_github("jthomasmock/gtExtras")
say("Let's get started!", "buffalo")
## 
##  -------------------- 
## < Let's get started! >
##  -------------------- 
##                 \
##                  \
## 
##                    _.-````'-,_
##          _,.,_ ,-'`           `'-.,_
##        /)     (                   '``-.
##       ((      ) )                      `\
##         \)    (_/                        )\
##         |       /)           '    ,'    / \
##         `\    ^'            '     (    /  ))
##           |      _/\ ,     /    ,,`\   (  "`
##           \Y,   |   \  \  | ````| / \_ \
##             `)_/      \  \  )    ( >  ( >
##                        \( \(     |/   |/
##           mic & dwb  /_(/_(    /_(  /_(
## 
moland13_lobsters.csvlob_data <- read_csv(here("data", "moland13_lobsters.csv")) %>% 
    mutate(year = factor(year)) %>% 
    mutate(region= factor(region))
year (5-levels): Years measured from 2006 to 2010region (3-levels): bol= Bolærne ,
kve = Kvernskjær , flo = Flødevigentreat (2-levels): mpa = treatment ,
con = controlcpue: “Catch per unit effort”gt} packagetbl_2way <- lob_data %>%
  group_by(year, region) %>%
  summarize(
      total_cpue = sum(cpue, na.rm = TRUE),
            .groups = "drop") %>% # <<< Same as `ungroup()`
  pivot_wider(
      names_from = region, 
      values_from = total_cpue) %>%
  arrange(year) %>%
  adorn_totals("row")
tbl_2way %>% 
  gt(rowname_col = "year") %>%
  tab_header(
    title = "European Lobster Catch by Region and Year",
    subtitle = "Total Catch Per Unit Effort (CPUE) by year and region") %>% 
  cols_label(
    bol = "Bolærne",
    flo = "Kvernskjær",
    kve = "Flødevigen") %>%
  tab_source_note(
      "Source: Moland et al., 2013")
| European Lobster Catch by Region and Year | |||
| Total Catch Per Unit Effort (CPUE) by year and region | |||
| Bolærne | Kvernskjær | Flødevigen | |
|---|---|---|---|
| 2006 | 127 | 122 | 177 | 
| 2007 | 269 | 93 | 276 | 
| 2008 | 249 | 151 | 367 | 
| 2009 | 484 | 168 | 466 | 
| 2010 | 463 | 175 | 449 | 
| Total | 1592 | 709 | 1735 | 
| Source: Moland et al., 2013 | |||
beep(sound=2); praise()
## [1] "You are great!"
say("Create more tables we must", "yoda")
## 
##  ---------------------------- 
## < Create more tables we must >
##  ---------------------------- 
##                 \
##                  \
## 
##                    ____
##                 _.' :  `._
##             .-.'`.  ;   .'`.-.
##    __      / : ___\ ;  /___ ; \      __
##   ,'_ ""--.:__;".-.";: :".-.":__;.--"" _`,
##   :' `.t""--.. '<@.`;_  ',@>` ..--""j.' `;
##        `:-.._J '-.-'L__ `-- ' L_..-;'
##           "-.__ ;  .-"  "-.  : __.-"
##              L ' /.------.\ ' J
##              "-.   "--"   .-"
##              __.l"-:_JL_;-";.__
##          .-j/'.;  ;""""  / .'\"-.
##          .' /:`. "-.:     .-" .';  `.
##       .-"  / ;  "-. "-..-" .-"  :    "-.
##   .+"-.  : :      "-.__.-"      ;-._   \
##   ; \  `.; ;                    : : "+. ;
##   :  ;   ; ;                    : ;  : \:
##   ;  :   ; :                    ;:   ;  :
##   : \  ;  :  ;                  : ;  /  ::
##   ;  ; :   ; :                  ;   :   ;:
##   :  :  ;  :  ;                : :  ;  : ;
##   ;\    :   ; :                ; ;     ; ;
##   : `."-;   :  ;              :  ;    /  ;
##  ;    -:   ; :              ;  : .-"   :
##   :\     \  :  ;            : \.-"      :
##   ;`.    \  ; :            ;.'_..--  / ;
##   :  "-.  "-:  ;          :/."      .'  :
##    \         \ :          ;/  __        :
##     \       .-`.\        /t-""  ":-+.   :
##      `.  .-"    `l    __/ /`. :  ; ; \  ;
##        \   .-" .-"-.-"  .' .'j \  /   ;/
##         \ / .-"   /.     .'.' ;_:'    ;
##   :-""-.`./-.'     /    `.___.'
##                \ `t  ._  /  bug
##                 "-.t-._:'
## 
year,
region, & treatmenttbl_3way <- lob_data %>%
  group_by(year, region, treat) %>%
  summarize(total_cpue = sum(cpue, na.rm = TRUE),
            .groups = "drop") %>%
  pivot_wider(names_from = c(region, treat), values_from = total_cpue) %>%
  arrange(year)
gt}fancy_table <- tbl_3way %>%
  gt(rowname_col = "year") %>%
  tab_header(
    title = "European Lobster Catch by Year, Region and Treatment",
    subtitle = "Total Catch Per Unit Effort (CPUE)"
  ) %>%
  tab_spanner(
    label = "Bolærne",
    columns = c("bol_con", "bol_mpa")
  ) %>%
  tab_spanner(
    label = "Flødevigen",
    columns = c("flo_con", "flo_mpa")
  ) %>%
  tab_spanner(
    label = "Kvernskjær",
    columns = c("kve_con", "kve_mpa")
  ) %>%
  cols_label(
    bol_con = "Control",
    bol_mpa = "MPA",
    flo_con = "Control",
    flo_mpa = "MPA",
    kve_con = "Control",
    kve_mpa = "MPA")
fancy_table
| European Lobster Catch by Year, Region and Treatment | ||||||
| Total Catch Per Unit Effort (CPUE) | ||||||
| 
         Bolærne 
       | 
      
         Flødevigen 
       | 
      
         Kvernskjær 
       | 
    ||||
|---|---|---|---|---|---|---|
| Control | MPA | Control | MPA | Control | MPA | |
| 2006 | 52 | 75 | 54 | 68 | 125 | 52 | 
| 2007 | 98 | 171 | 33 | 60 | 114 | 162 | 
| 2008 | 78 | 171 | 55 | 96 | 178 | 189 | 
| 2009 | 187 | 297 | 51 | 117 | 244 | 222 | 
| 2010 | 148 | 315 | 64 | 111 | 198 | 251 | 
Pick a pallete & add color
colorful_table <- fancy_table %>% 
  data_color(
    columns = c("bol_con", "flo_con", "kve_con"),
    colors = scales::col_numeric(
      palette = viridis::mako(
          n=2,
          begin=.5, 
          end=1,
          direction=-1),
      domain = NULL)) %>%
  data_color(
    columns = c("bol_mpa", "flo_mpa", "kve_mpa"),
    colors = scales::col_numeric(
      palette = viridis::rocket(
          n=2, 
          begin=.5, 
          end=1,
          direction=-1),
      domain = NULL))
colorful_table
| European Lobster Catch by Year, Region and Treatment | ||||||
| Total Catch Per Unit Effort (CPUE) | ||||||
| 
         Bolærne 
       | 
      
         Flødevigen 
       | 
      
         Kvernskjær 
       | 
    ||||
|---|---|---|---|---|---|---|
| Control | MPA | Control | MPA | Control | MPA | |
| 2006 | 52 | 75 | 54 | 68 | 125 | 52 | 
| 2007 | 98 | 171 | 33 | 60 | 114 | 162 | 
| 2008 | 78 | 171 | 55 | 96 | 178 | 189 | 
| 2009 | 187 | 297 | 51 | 117 | 244 | 222 | 
| 2010 | 148 | 315 | 64 | 111 | 198 | 251 | 
Add inline plots to a gt table using the
{gtExtras} package
table_w_plots <- lob_data %>%
  group_by(year) %>%
  summarize(
      total_cpue = sum(cpue, na.rm = TRUE),
      dist_cpue = list(cpue),
      .groups = "drop") %>% 
  arrange(year) %>% 
    gt() %>% 
     tab_header(
    title = "European Lobster Catch Totals and Distribution (2006-2010)",
    subtitle = "Total Catch Per Unit Effort (CPUE)") %>% 
    cols_label(
    year = "Year",
    total_cpue = "Total CPUE",
    dist_cpue = "Density CPUE") %>%
    gtExtras::gt_plt_dist( # Add in-line plots!
        dist_cpue,
        type = "density", 
        line_color = "blue", 
        fill_color = "red")
table_w_plots 
| European Lobster Catch Totals and Distribution (2006-2010) | ||
| Total Catch Per Unit Effort (CPUE) | ||
| Year | Total CPUE | Density CPUE | 
|---|---|---|
| 2006 | 426 | |
| 2007 | 638 | |
| 2008 | 767 | |
| 2009 | 1118 | |
| 2010 | 1087 | |
praise("${EXCLAMATION}!- You've ${created} something ${adjective}!"); beep(1)
## [1] "HURRAH!- You've built something kryptonian!"
say("Time for plotting", "squirrel2")
## 
##  ------------------- 
## < Time for plotting >
##  ------------------- 
##                     \
##                      \
## 
##                       . .
##                       |\|\_
##                       /  @ \
##                      /  _-_/°
##    \\\\\\\\\       /   / \
##   ////////////   /  \ / ||
##  \\\\\\\\\\\\\\ /   /\\ \\
## ////////////////   /  \\ \\
##  \\\\\\\\\\\\\/   /  / `` ``
##      /////////   \  /  \
##  ML     \\\\\\___/_/___/
ggthemes} &
{bbplot}lob_size <- read_csv(here("data", "moland13_lobstersize.csv")) %>% 
    mutate(region = factor(region, labels = c("Kve", "Flo", "Bol"))) %>% 
    mutate(treat = factor(treat, labels = c("Control Sites", "MPA Sites"))) 
lob_size %>% 
ggplot( aes(x = size, y = region , fill = region)) +
  geom_density_ridges(alpha=.9) +
  labs(title = "European Lobster Size: Total Length",
      subtitle =  "Data from Moland et al., 2013",
      caption = "Total Length (mm)" ) +
  facet_wrap(~treat) +
scale_fill_wsj() + 
  theme_wsj() +
  theme(
    legend.position = "none",
    plot.title = element_text(size = 16),
    plot.subtitle = element_text(size = 12),
    plot.caption = element_text(size = 12),
    strip.text = element_text(size = 12, face = "bold", family = "mono"))
Save plot figure
ggsave(here("figures", "lbstr-ridge-plot.png"), dpi=300, height=5, width=7, units="in")
## Picking joint bandwidth of 0.711
## Picking joint bandwidth of 0.86
Change plot theme to follow the BBC viz conventions
(bbplot::bbc_style())
lob_size %>%
  group_by(year,treat) %>%
  summarize(
      mean_size = mean(size, na.rm = TRUE),
      .groups = "drop") %>% 
ggplot( aes(x = year, y = mean_size , color = treat)) +
  geom_line(size=1) +
  labs(title = "European Lobster Size (2006-2010)",
      subtitle = "Total Length (mm)" ) +
    bbc_style()
say("I guess this is the END :) ", "stegosaurus"); beep(3)  
## 
##  ---------------------------- 
## < I guess this is the END :) >
##  ---------------------------- 
##                              \
##                               \
## 
##                          .       .
##                         / `.   .' \
##                 .---.  <    > <    >  .---.
##                 |    \  \ - ~ ~ - /  /    |
##                  ~-..-~             ~-..-~
##              \~~~\.'                    `./~~~/
##    .-~~^-.    \__/                        \__/
##  .'  O    \     /               /       \  \
## (_____,    `._.'               |         }  \/~~~/
##  `----.          /       }     |        /    \__/
##        `-.      |       /      |       /      `. ,~~|
##            ~-.__|      /_ - ~ ^|      /- _      `..-'   f: f:
##                 |     /        |     /     ~-.     `-. _||_||_
##                 |_____|        |_____|         ~ - . _ _ _ _ _>
gt} package (Iannone et al., 2024):janitor} package (adorn_; Firke,
2023):ggthemes} & {bbplot} package (Arnold,
2024):# More themes! 
?theme_economist()
?theme_fivethirtyeight()
?theme_wsj()
?theme_solarized()
?bbc_style()
viridis} & {RColorBrewer}
packagescowsay} package (Chamberlain & Dobbyn, 2024):names(animals) # Check out all the animals!
##  [1] "cow"          "cow_borg"     "cow_greedy"   "cow_sleepy"   "cow_dead"    
##  [6] "cow_tired"    "cow_wired"    "cow_young"    "chicken"      "chuck"       
## [11] "clippy"       "poop"         "bigcat"       "ant"          "pumpkin"     
## [16] "ghost"        "spider"       "rabbit"       "pig"          "snowman"     
## [21] "frog"         "hypnotoad"    "shortcat"     "longcat"      "fish"        
## [26] "signbunny"    "facecat"      "behindcat"    "stretchycat"  "anxiouscat"  
## [31] "longtailcat"  "cat"          "trilobite"    "shark"        "buffalo"     
## [36] "grumpycat"    "smallcat"     "yoda"         "mushroom"     "endlesshorse"
## [41] "bat"          "bat2"         "turkey"       "monkey"       "daemon"      
## [46] "egret"        "duckling"     "duck"         "owl"          "squirrel"    
## [51] "squirrel2"    "goldfish"     "alligator"    "stegosaurus"  "whale"       
## [56] "wolf"
praise} package (Csardi & Sorhus, 2015)praise_parts
## $adjective
##   [1] "ace"             "amazing"         "astonishing"     "astounding"     
##   [5] "awe-inspiring"   "awesome"         "badass"          "beautiful"      
##   [9] "bedazzling"      "bee's knees"     "best"            "breathtaking"   
##  [13] "brilliant"       "cat's meow"      "cat's pajamas"   "classy"         
##  [17] "cool"            "dandy"           "dazzling"        "delightful"     
##  [21] "divine"          "doozie"          "epic"            "excellent"      
##  [25] "exceptional"     "exquisite"       "extraordinary"   "fabulous"       
##  [29] "fantastic"       "fantabulous"     "fine"            "finest"         
##  [33] "first-class"     "first-rate"      "flawless"        "funkadelic"     
##  [37] "geometric"       "glorious"        "gnarly"          "good"           
##  [41] "grand"           "great"           "groovy"          "groundbreaking" 
##  [45] "hunky-dory"      "impeccable"      "impressive"      "incredible"     
##  [49] "kickass"         "kryptonian"      "laudable"        "legendary"      
##  [53] "lovely"          "luminous"        "magnificent"     "majestic"       
##  [57] "marvelous"       "mathematical"    "mind-blowing"    "neat"           
##  [61] "outstanding"     "peachy"          "perfect"         "phenomenal"     
##  [65] "pioneering"      "polished"        "posh"            "praiseworthy"   
##  [69] "premium"         "priceless"       "prime"           "primo"          
##  [73] "rad"             "remarkable"      "riveting"        "sensational"    
##  [77] "shining"         "slick"           "smashing"        "solid"          
##  [81] "spectacular"     "splendid"        "stellar"         "striking"       
##  [85] "stunning"        "stupendous"      "stylish"         "sublime"        
##  [89] "super"           "super-duper"     "super-excellent" "superb"         
##  [93] "superior"        "supreme"         "swell"           "terrific"       
##  [97] "tiptop"          "top-notch"       "transcendent"    "tremendous"     
## [101] "ultimate"        "unreal"          "well-made"       "wicked"         
## [105] "wonderful"       "wondrous"        "world-class"    
## 
## $adverb
##  [1] "beautifully"      "bravely"          "brightly"         "calmly"          
##  [5] "carefully"        "cautiously"       "cheerfully"       "clearly"         
##  [9] "correctly"        "courageously"     "daringly"         "deliberately"    
## [13] "doubtfully"       "eagerly"          "easily"           "elegantly"       
## [17] "enormously"       "enthusiastically" "faithfully"       "fast"            
## [21] "fondly"           "fortunately"      "frankly"          "frantically"     
## [25] "generously"       "gently"           "gladly"           "gracefully"      
## [29] "happily"          "healthily"        "honestly"         "joyously"        
## [33] "justly"           "kindly"           "neatly"           "openly"          
## [37] "patiently"        "perfectly"        "politely"         "powerfully"      
## [41] "quickly"          "quietly"          "rapidly"          "really"          
## [45] "regularly"        "repeatedly"       "rightfully"       "seriously"       
## [49] "sharply"          "smoothly"         "speedily"         "successfully"    
## [53] "swiftly"          "tenderly"         "thoughtfully"     "truthfully"      
## [57] "warmly"           "well"             "wisely"          
## 
## $adverb_manner
##  [1] "beautifully"      "bravely"          "brightly"         "calmly"          
##  [5] "carefully"        "cautiously"       "cheerfully"       "clearly"         
##  [9] "correctly"        "courageously"     "daringly"         "deliberately"    
## [13] "doubtfully"       "eagerly"          "easily"           "elegantly"       
## [17] "enormously"       "enthusiastically" "faithfully"       "fast"            
## [21] "fondly"           "fortunately"      "frankly"          "frantically"     
## [25] "generously"       "gently"           "gladly"           "gracefully"      
## [29] "happily"          "healthily"        "honestly"         "joyously"        
## [33] "justly"           "kindly"           "neatly"           "openly"          
## [37] "patiently"        "perfectly"        "politely"         "powerfully"      
## [41] "quickly"          "quietly"          "rapidly"          "really"          
## [45] "regularly"        "repeatedly"       "rightfully"       "seriously"       
## [49] "sharply"          "smoothly"         "speedily"         "successfully"    
## [53] "swiftly"          "tenderly"         "thoughtfully"     "truthfully"      
## [57] "warmly"           "well"             "wisely"          
## 
## $created
##  [1] "assembled"   "brewed"      "built"       "created"     "composed"   
##  [6] "constructed" "designed"    "devised"     "forged"      "formed"     
## [11] "initiated"   "invented"    "made"        "organized"   "planned"    
## [16] "prepared"    "set up"     
## 
## $creating
##  [1] "assembling"   "brewing"      "building"     "creating"     "composing"   
##  [6] "constructing" "designing"    "devising"     "forging"      "forming"     
## [11] "initiating"   "inventing"    "making"       "organizing"   "planning"    
## [16] "preparin"     "setting up"  
## 
## $exclamation
##  [1] "ah"      "aha"     "ahh"     "ahhh"    "aw"      "aww"     "awww"   
##  [8] "aye"     "gee"     "ha"      "hah"     "hmm"     "ho-ho"   "huh"    
## [15] "heh"     "hooray"  "hurrah"  "hurray"  "huzzah"  "mhm"     "mm"     
## [22] "mmh"     "mmhm"    "mmm"     "oh"      "ole"     "uh-hu"   "wee"    
## [29] "whee"    "whoa"    "wow"     "wowie"   "yahoo"   "yay"     "yeah"   
## [36] "yee-haw" "yikes"   "yippie"  "yow"     "yowza"  
## 
## $rpackage
## [1] "code"                  "library (or package?)" "package"              
## [4] "program"               "project"               "software"             
## [7] "R package"
beepr} package (Rasmus BĂĄĂĄth, 2024)# Try: `beep(999)` to pick a sound at random (there are 11 sounds)!
beep(999)
say("That's all folks!", "egret")
## 
##  ------------------- 
## < That's all folks! >
##  ------------------- 
##         \
##          \
## 
##            _,
##       -==<' `
##           ) /
##          / (_.
##         |  ,-,`\
##          \\   \ \
##           `\,  \ \
##            ||\  \`|,
##  jgs      _|| `=`-'
##          ~~`~`