# INSTALLATIONS - YOU ONLY NEED TO DO THIS ONCE (ONCE IN A WHILE) --------------
remotes::install_local("R:/R/Pakkar/mardata", force = TRUE)
remotes::install_github("fishvice/xe", dependencies = FALSE)
remotes::install_github("tidyverse/dbplyr@v1.4.4", force = TRUE)
# further info, see: https://heima.hafro.is/~einarhj/xe/
# RUN THIS CHUNK line-by-line EACH TIME YOU HAVE GOTTEN NEW DATA IN HAFVOG
library(ROracle)
library(xe)
# 2023-09-26: NEW
#             Create the connection the database
#             Old setup: dbname = "xe"      (Klara)
#             New setup: dbname = "XEPDB1"  (Valur, Hlynur)
con <- connect_oracle(dbname = "xe")
# 2022-02-23:
#            import_hafvog2 is a temporary function, because veidarfaeri is NA
#            (some problem between different versions of hafvog - historical though
#             should not be a problem for new data in 2022)
# Specify the synaflokkur using the id and the gear using the gid. E.g.:
#         id 
# SMB     30
# SMH     35
res <- import_hafvog3(id = 35, con = con)
# just a quick fix
res$st <- 
  res$st %>% 
  dplyr::mutate(veidarfaeri = ifelse(is.na(veidarfaeri), 73, veidarfaeri))
# Here specify whatever cruise(s) you want to include.
#  Make sure that these cruises are in your Hafvog, change accordingly
xe::munge_for_smxapp(res, cruise = c("TB1-2022"))
library(magrittr)
load("data2/smb_dashboard.rda")

pp <- 
  readr::read_rds("data2/pp.rds") %>% 
  dplyr::mutate(m.thyngd = round(thyngd/n, 2))
pp2 <- 
  pp %>% 
  dplyr::group_by(leidangur, stod, pred, nr, astand) %>% 
  dplyr::summarise(thyngd = sum(thyngd, na.rm = TRUE),
                   n = sum(n, na.rm = TRUE),
                   oslaegt = max(oslaegt, na.rm = TRUE),
                   .groups = "drop") %>% 
  dplyr::mutate(magafylli = round(thyngd / oslaegt * 100, 2))

Tegund <- sort(unique(by.tegund.lengd.ar$tegund))
# You may need next line if smb_dashboard.rda was generated in windose but
#  currently running linux - this is normally only needed when setting up
#  app on the MRI shiny-webserver
#fisktegundir$heiti <- iconv(fisktegundir$heiti, from = "ISO-8859-1", to = "UTF-8")
pp$prey <- iconv(pp$prey, from = "ISO-8859-1", to = "UTF-8")
x <- 
  fisktegundir %>% 
  dplyr::filter(tegund %in% Tegund) %>% 
  dplyr::mutate(val = paste(tegund, heiti)) %>% 
  dplyr::select(tegund, val)
my.species <- as.list(x$tegund)
names(my.species) <- x$val
x <- st %>% 
  dplyr::filter(ar == now.year) %>% 
  dplyr::select(leidangur) %>% 
  dplyr::distinct()
my.cruises <- as.list(x$leidangur)
names(my.cruises) <- x$leidangur

index.done.cruise <-
  tibble::tibble(index = index.done) %>% 
  dplyr::left_join(st %>% 
                     dplyr::filter(ar == now.year) %>% 
                     dplyr::select(leidangur, stod, index, togbyrjun))
last.20 <-
  st %>%  
  dplyr::filter(ar == now.year) %>% 
  dplyr::arrange(leidangur, desc(togbyrjun)) %>% 
  dplyr::group_by(leidangur) %>% 
  dplyr::slice(1:20) %>% 
  dplyr::mutate(id = dplyr::n():1) %>% 
  dplyr::ungroup() %>% 
  dplyr::select(leidangur, id, index) %>% 
  dplyr::left_join(st %>% 
                     dplyr::select(index, ar, larett_opnun, lodrett_opnun,
                                   botnhiti, yfirbordshiti, vir_uti)) %>%
  tidyr::gather(variable, value, larett_opnun:vir_uti) %>% 
  dplyr::filter(!is.na(value))
st.dummy <-
  st %>%  
  dplyr::filter(index %in% index.done,
                ar == now.year) %>% 
  dplyr::select(leidangur, index) %>% 
  dplyr::left_join(st %>% 
                     dplyr::select(index, ar, larett_opnun, lodrett_opnun,
                                   botnhiti, yfirbordshiti, vir_uti)) %>%
  tidyr::gather(variable, value, larett_opnun:vir_uti) %>% 
  dplyr::filter(!is.na(value))

# skítamixið:
maeliatridi <- readr::read_rds("data2/maeliatridi.rds")
maeliatridi$heiti <- iconv(maeliatridi$heiti, 
                           from = "ISO-8859-1",
                           to = "UTF-8")
skr <- 
  readr::read_rds("data2/res.rds")$skraning %>% 
  dplyr::group_by(synis_id, maeliadgerd) %>% 
  dplyr::summarise(n = sum(fjoldi),
                   .groups = "drop")
MAELINGAR <- 
  st %>% 
  dplyr::filter(ar == now.year) %>% 
  dplyr::select(synis_id,
                leidangur,
                time = togbyrjun) %>% 
  dplyr::group_by(leidangur) %>% 
  dplyr::mutate(start.time = min(time),
                rel.time = as.numeric(time - start.time) / (60 * 60 * 24),
                max.time = max(rel.time)) %>% 
  dplyr::select(leidangur, rel.time, max.time, synis_id) %>% 
  dplyr::ungroup() %>% 
  dplyr::left_join(skr) %>% 
  dplyr::arrange(rel.time) %>% 
  tidyr::complete(rel.time, tidyr::nesting(leidangur, maeliadgerd), 
                  fill = list(n = 0)) %>% 
  dplyr::group_by(rel.time, leidangur) %>% 
  tidyr::fill(max.time) %>% 
  dplyr::filter(rel.time <= max.time) %>% 
  dplyr::arrange(rel.time) %>% 
  dplyr::left_join(maeliatridi) %>% 
  dplyr::group_by(leidangur, heiti) %>% 
  dplyr::mutate(n.cum = cumsum(n)) %>% 
  dplyr::ungroup()


res <- readr::read_rds("data2/res.rds")
tegund <- res$other.stuff$fisktegundir
tegund$heiti <- iconv(tegund$heiti, from = "ISO-8859-1", to = "UTF-8")

species.first.year <- 
  res$st %>% 
  dplyr::left_join(res$nu) %>% 
  dplyr::select(ar, reitur, tegund, fj_alls) %>% 
  dplyr::filter(!tegund %in% c(160,180,182,184,191,197,201,237,303,304,344,403,731:739,799,904)) %>% 
  dplyr::group_by(reitur, tegund) %>% 
  dplyr::summarise(first.year = min(ar)) %>% 
  dplyr::ungroup() %>% 
  dplyr::mutate(lon = geo::r2d(reitur)$lon,
                lat = geo::r2d(reitur)$lat)


by.rect <- 
  res$st %>% 
  dplyr::left_join(res$nu) %>% 
  dplyr::group_by(ar, reitur, tegund) %>% 
  dplyr::summarise(n = mean(fj_alls, na.rm = TRUE)) %>% 
  dplyr::filter(!is.na(tegund)) %>% 
  dplyr::group_by(ar, tegund) %>% 
  dplyr::mutate(n = ifelse(n > quantile(n, 0.975), quantile(n, 0.975), n)) %>% 
  dplyr::ungroup() %>% 
  dplyr::mutate(lon = geo::r2d(reitur)$lon,
                lat = geo::r2d(reitur)$lat)

scale_longitude_ices <- function(min = -44, max = 68.5, step = 1, ...) {
  breaks <- seq(min + 0.5, max - 0.5, step)
  labels <- geo::d2ir(60, breaks) %>% stringr::str_sub(3)
  return(ggplot2::scale_x_continuous(name = NULL, breaks = breaks, labels = labels, ...))
}
scale_latitude_ices <- function(min = 36, max = 84.5, step = 0.5, ...) {
  breaks <- seq(min + 0.25, max - 0.25, step)
  labels <- geo::d2ir(breaks, 0) %>% stringr::str_sub(1, 2)
  return(ggplot2::scale_y_continuous(name = NULL, breaks = breaks, labels = labels, ...))
}

# some addendums
tows.done <- 
  st.done.sf %>% dplyr::filter(year == now.year) %>% 
  sf::st_coordinates() %>% 
  tibble::as_tibble()

Sidebar {.sidebar data-width=175}

selectInput(inputId = "Species", label = "Tegund:",
            choices = my.species, selected = 1)


radioButtons(inputId = "Type", label = "Val:", 
             choices = list("Numbers", "Weight"),
             selected = list("Numbers"))

checkboxGroupInput(inputId = "Leidangur", label = "Leidangur:",
                   choices = my.cruises, selected = my.cruises[[1]])

Síðast uppfært:

r as.character(timi)

ATH: Val á leiðangri ekki virkt í öllum gluggum.

Dót til prufu - um kóðann sem er á bak við má fræðast um nánar hér.

Forsíða

Column

Eftir lengd

renderPlot({
  if(input$Type == "Numbers") 
  {
    ggplot2::ggplot() +
      ggplot2::theme_grey(base_size = 16) +
      ggplot2::geom_ribbon(data = by.tegund.lengd.ar.m %>% 
                             dplyr::filter(tegund == as.numeric(input$Species)),
                           ggplot2::aes(lengd, ymax = n.std, ymin = 0), fill = "grey") +
      ggplot2::geom_line(data = by.tegund.lengd.ar  %>% 
                           dplyr::filter(tegund == as.numeric(input$Species),
                                         ar >= 2010),
                         ggplot2::aes(lengd, n.std)) +
      ggplot2::facet_grid(ar ~ .) +
      ggplot2::labs(x = NULL, y = "Fjöldi í hverju lengdarbili") +
      ggplot2::scale_x_continuous(breaks = seq(10, 200, by = 10))
  } else {
    ggplot2::ggplot() +
      ggplot2::theme_grey(base_size = 16) +
      ggplot2::geom_ribbon(data = by.tegund.lengd.ar.m %>% 
                             dplyr::filter(tegund == as.numeric(input$Species)),
                           ggplot2::aes(lengd, ymax = b.std, ymin = 0), fill = "grey") +
      ggplot2::geom_line(data = by.tegund.lengd.ar  %>% 
                           dplyr::filter(tegund == as.numeric(input$Species),
                                         ar >= 2010),
                         ggplot2::aes(lengd, b.std)) +
      ggplot2::facet_grid(ar ~ .) +
      ggplot2::labs(x = NULL, y = "Þyngd [kg] í hverju lengdarbili") +
      ggplot2::scale_x_continuous(breaks = seq(10, 200, by = 10))
  } 

})

Column

Meðalafli - hártogun með staðalvikmörkum

renderPlot({
  if(input$Type == "Numbers") 
  {
    by.station.boot %>% 
      dplyr::filter(tegund == as.numeric(input$Species),
                    variable == "n") %>% 
      ggplot2::ggplot(ggplot2::aes(ar, n.std)) +
      ggplot2::theme_grey(base_size = 16) +
      ggplot2::geom_pointrange(ggplot2::aes(ar, mean, ymin = lower.ci, ymax = upper.ci)) +
      ggplot2::scale_x_continuous(breaks = seq(1985, 2025, by = 5)) +
      ggplot2::expand_limits(y = 0) +
      ggplot2::labs(x = NULL, y = NULL)
  } else {
    by.station.boot %>% 
      dplyr::filter(tegund == as.numeric(input$Species),
                    variable == "b") %>% 
      ggplot2::ggplot(ggplot2::aes(ar, b.std)) +
      ggplot2::theme_grey(base_size = 16) +
      ggplot2::geom_pointrange(ggplot2::aes(ar, mean, ymin = lower.ci, ymax = upper.ci)) +
      ggplot2::scale_x_continuous(breaks = seq(1985, 2025, by = 5)) +
      ggplot2::expand_limits(y = 0) +
      ggplot2::labs(x = NULL, y = NULL)
  }
})

Afli í staðaltogi árið r now.year

leaflet::renderLeaflet({

  x0 <- 
    by.station %>% 
    dplyr::filter(ar == now.year) %>% 
    dplyr::select(lon, lat) %>% 
    dplyr::distinct()
  if(input$Type == "Numbers") {
    x <- 
      by.station %>% 
      dplyr::filter(ar == now.year,
                    tegund == as.numeric(input$Species),
                    n.std > 0) %>% 
      dplyr::select(lon, lat, n.std) %>% 
      dplyr::arrange(-n.std)
    skali <- sqrt(max(x$n.std))
    leaflet::leaflet(x) %>% 
      leaflet::addTiles() %>% 
      leaflet::addCircles(data = x0, weight = 0.5, color = "white") %>% 
      leaflet::addCircles(weight = 1,
                          popup = ~paste(n.std, "stykki"),
                          radius = ~sqrt(n.std)/skali * 5e4,
                          color = "red")
  } else {
    x <- 
      by.station %>% 
      dplyr::filter(ar == now.year,
                    b.std > 0,
                    tegund == as.numeric(input$Species)) %>% 
      dplyr::select(lon, lat, b.std) %>% 
      dplyr::arrange(-b.std)
    skali <- sqrt(max(x$b.std))
    leaflet::leaflet(x) %>% 
      leaflet::addTiles() %>%
      leaflet::addCircles(data = x0, weight = 0.5, color = "white") %>% 
      leaflet::addCircles(weight = 1,
                          popup = ~paste(round(b.std), "kg"),
                          radius = ~sqrt(b.std)/skali * 5e4,
                          color = "red")
  }
})

Dreifing

Column {.tabset}

Afli í staðaltogi

renderPlot({
  if(input$Type == "Numbers") {
    by.station %>% 
      dplyr::filter(ar %in% c(1985, 1990, 1995, 2000, seq(2005, 2015, by = 2), now.year-1, now.year),
                    tegund == as.numeric(input$Species)) %>% 
      ggplot2::ggplot() +
      ggplot2::theme_grey(base_size = 16) +
      ggplot2::geom_path(data = geo::island, ggplot2::aes(lon, lat)) +
      ggplot2::geom_point(ggplot2::aes(lon, lat, size = n.std),
                          alpha = 0.5, colour = "red") +
      ggplot2::scale_size_area(max_size = 30) +
      ggplot2::coord_quickmap(xlim = range(by.station$lon, na.rm = TRUE),
                              ylim = range(by.station$lat, na.rm = TRUE)) +
      #ggplot2::theme(legend.position = c(0.5, 0.6)) +
      ggplot2::labs(x = NULL, y = NULL, size = "Stykki") +
      ggplot2::facet_wrap(~ ar, nrow = 3)
  } else {
    by.station %>% 
      dplyr::filter(ar %in% c(1985, 1990, 1995, 2000, seq(2005, 2015, by = 2), now.year - 1, now.year),
                    tegund == as.numeric(input$Species)) %>% 
      ggplot2::ggplot() +
      ggplot2::theme_grey(base_size = 16) +
      ggplot2::geom_path(data = geo::island, ggplot2::aes(lon, lat)) +
      ggplot2::geom_point(ggplot2::aes(lon, lat, size = b.std),
                          alpha = 0.5, colour = "red") +
      ggplot2::scale_size_area(max_size = 30) +
      ggplot2::coord_quickmap(xlim = range(by.station$lon, na.rm = TRUE),
                              ylim = range(by.station$lat, na.rm = TRUE)) +
      #ggplot2::theme(legend.position = c(0.5, 0.6)) +
      ggplot2::labs(x = NULL, y = NULL, size = "kg") +
      ggplot2::facet_wrap(~ ar, nrow = 3)
  }
})

Tegund - fjöldi í reit

renderPlot({
  n.glyph <- 
    by.rect %>% 
    dplyr::filter(tegund == input$Species) %>% 
    GGally::glyphs(x_major = "lon", 
                   y_major = "lat",
                   x_minor = "ar", 
                   y_minor = "n", 
                   width = 1, 
                   height = 0.5)

  n.glyph %>% 
    dplyr::mutate(years = ifelse(ar < now.year, "history", "current"),
                  pos = ifelse(n != 0, TRUE, FALSE),
                  base = lat - 0.25,
                  gy = ifelse(n == 0, gy + 0.005, gy)) %>% 
    ggplot2::ggplot() +
    ggplot2::theme_bw() +
    ggplot2::geom_linerange(ggplot2::aes(x = gx, ymin = base, ymax = gy,
                                         colour = years)) +
    #ggplot2::geom_(data = st.done.sp %>% sf::st_as_sf() %>% dplyr::filter(ar == 2021)) +
    ggplot2::geom_path(data = geo::island, ggplot2::aes(lon, lat)) +
    ggplot2::coord_quickmap() +
    scale_longitude_ices() +
    scale_latitude_ices() +
    ggplot2::scale_colour_manual(values = c("history" = "#377EB8", "current" = "#E41A1C")) +
    ggplot2::theme(panel.grid.major = ggplot2::element_blank(),
                   panel.grid.minor = ggplot2::element_line(size = 1),
                   axis.ticks = ggplot2::element_blank(),
                   legend.position = "none")
})

Tegund - fyrsta skipti í reit: kort

renderPlot({
  species.first.year %>% 
    dplyr::filter(tegund == input$Species) %>% 
    ggplot2::ggplot() +
    ggplot2::geom_tile(ggplot2::aes(lon, lat, fill = first.year)) +
    ggplot2::geom_path(data = geo::island, ggplot2::aes(lon, lat), colour = "white") +
    ggplot2::geom_text(ggplot2::aes(lon, lat, label = first.year),
                       angle = 45, colour = "yellow") +
    ggplot2::coord_quickmap() +
    ggplot2::labs(x = NULL, y = NULL)
})

Vikmörk-myndir

Column {.tabset}

Lengd-óslægt

renderPlot({
  d <- 
    kv.this.year %>%
    dplyr::left_join(st %>% 
                       dplyr::select(synis_id, leidangur)) %>% 
    dplyr::filter(tegund == as.numeric(input$Species),
                  leidangur %in% input$Leidangur)
  ggplot2::ggplot() +
    ggplot2::theme_grey(base_size = 16) +
    ggplot2::geom_ribbon(data = stadlar.lw %>% 
                           dplyr::filter(tegund == as.numeric(input$Species)),
                         ggplot2::aes(lengd, ymin = osl1, ymax = osl2),
                         fill = "pink") +
    ggplot2::geom_point(data = d %>% 
                          dplyr::filter(ok.l.osl), 
                        ggplot2::aes(lengd, oslaegt), 
                        size = 1, alpha = 0.5, colour = "blue") +
    ggplot2::geom_point(data = d %>% dplyr::filter(!ok.l.osl), 
                        ggplot2::aes(lengd, oslaegt), colour = "red") +
    ggrepel::geom_text_repel(data = d %>% dplyr::filter(!ok.l.osl), 
                             ggplot2::aes(lengd, oslaegt, label = lab)) +
    ggplot2::scale_x_log10(breaks = c(seq(5, 50, by = 5), seq(60, 100, by = 10), 120, 140, 160, 200)) +
    ggplot2::scale_y_log10(breaks = c(seq(5, 50, by = 5),
                                      seq(60, 100, by = 10),
                                      seq(120, 200, by = 20),
                                      seq(300, 1000, by = 100),
                                      seq(1500, 10000, by = 500),
                                      seq(15000, 30000, by = 1000))) +
    ggplot2::coord_cartesian(xlim = range(d$lengd, na.rm = TRUE),
                             ylim = range(d$oslaegt, na.rm = TRUE))
})

Lengd-slægt

renderPlot({
  d <- 
    kv.this.year %>% 
    dplyr::left_join(st %>% 
                       dplyr::select(synis_id, leidangur)) %>% 
    dplyr::filter(tegund == as.numeric(input$Species),
                  leidangur %in% input$Leidangur)
  ggplot2::ggplot() +
    ggplot2::theme_grey(base_size = 16) +
    ggplot2::geom_ribbon(data = stadlar.lw %>% 
                           dplyr::filter(tegund == as.numeric(input$Species)),
                         ggplot2::aes(lengd, ymin = sl1, ymax = sl2),
                         fill = "pink") +
    ggplot2::geom_point(data = d %>% 
                          dplyr::filter(ok.l.sl), 
                        ggplot2::aes(lengd, slaegt), 
                        size = 1, alpha = 0.5, colour = "blue") +
    ggplot2::geom_point(data = d %>% dplyr::filter(!ok.l.sl), 
                        ggplot2::aes(lengd, slaegt), colour = "red") +
    ggrepel::geom_text_repel(data = d %>% 
                               dplyr::filter(!ok.l.sl), 
                             ggplot2::aes(lengd, slaegt, label = lab)) +
    ggplot2::scale_x_log10(breaks = c(5, 10, 15, 30, 60, 100)) +
    ggplot2::scale_y_log10(breaks = c(5, 25, 250, 500, 1000, 5000, 10000)) +
    ggplot2::coord_cartesian(xlim = range(d$lengd, na.rm = TRUE),
                             ylim = range(d$oslaegt, na.rm = TRUE))
})

Lengd-slægt/óslægt

renderPlot({
  stadlar <- 
    stadlar.tegundir %>% 
    dplyr::filter(tegund == as.numeric(input$Species))
  d <- 
    kv.this.year %>% 
    dplyr::left_join(st %>% 
                       dplyr::select(synis_id, leidangur)) %>% 
    dplyr::filter(tegund == as.numeric(input$Species),
                  leidangur %in% input$Leidangur)
  ggplot2::ggplot() +
    ggplot2::theme_grey(base_size = 16) +
    ggplot2::geom_hline(yintercept = 1, colour = "red", lwd = 2) +
    ggplot2::geom_rect(data = stadlar,
                       ggplot2::aes(xmin = -Inf, xmax = Inf, ymin = oslaegt_slaegt_low, ymax = oslaegt_slaegt_high),
                       fill = "pink") +
    ggplot2::geom_point(data = d %>% dplyr::filter(ok.sl.osl), 
                        ggplot2::aes(lengd, slaegt/oslaegt), size = 1, alpha = 0.5, colour = "blue") +
    ggplot2::geom_point(data = d %>% dplyr::filter(!ok.sl.osl), 
                        ggplot2::aes(lengd, slaegt/oslaegt), colour = "red") +
    ggrepel::geom_text_repel(data = d %>% dplyr::filter(!ok.sl.osl), 
                             ggplot2::aes(lengd, slaegt/oslaegt, label = lab))
})

Lengd-lifur/óslaegt

renderPlot({
  d <- 
    kv.this.year %>% 
    dplyr::left_join(st %>% 
                       dplyr::select(synis_id, leidangur)) %>% 
    dplyr::filter(tegund == as.numeric(input$Species),
                  leidangur %in% input$Leidangur)
  ggplot2::ggplot() +
    ggplot2::theme_grey(base_size = 16) +
    #geom_hline(yintercept = 1, colour = "red", lwd = 2) +
    ggplot2::geom_rect(data = stadlar.tegundir %>% 
                         dplyr::filter(tegund == as.numeric(input$Species)),
                       ggplot2::aes(xmin = -Inf, xmax = Inf, ymin = lifur_low, ymax = lifur_high),
                       fill = "pink") +
    ggplot2::geom_point(data = d %>% 
                          dplyr::filter(ok.lifur.osl), 
                        ggplot2::aes(lengd, lifur/oslaegt), size = 1, alpha = 0.5, colour = "blue") +
    ggplot2::geom_point(data = d %>% 
                          dplyr::filter(!ok.lifur.osl), 
                        ggplot2::aes(lengd, lifur/oslaegt), colour = "red") +
    ggrepel::geom_text_repel(data = d %>% 
                               dplyr::filter(!ok.lifur.osl), 
                             ggplot2::aes(lengd, lifur/oslaegt, label = lab))
})

Lengd-kynkirtlar/óslaegt

renderPlot({
  d <- 
    kv.this.year %>% 
    dplyr::left_join(st %>% 
                       dplyr::select(synis_id, leidangur)) %>% 
    dplyr::filter(tegund == as.numeric(input$Species),
                  leidangur %in% input$Leidangur)
  ggplot2::ggplot() +
    ggplot2::theme_grey(base_size = 16) +
    #geom_hline(yintercept = 1, colour = "red", lwd = 2) +
    ggplot2::geom_rect(data = stadlar.tegundir %>% 
                         dplyr::filter(tegund == as.numeric(input$Species)),
                       ggplot2::aes(xmin = -Inf, xmax = Inf, ymin = kynkirtlar_low, ymax = kynkirtlar_high),
                       fill = "pink") +
    ggplot2::geom_point(data = d %>% 
                          dplyr::filter(ok.kirtlar.osl), 
                        ggplot2::aes(lengd, kynfaeri/oslaegt), 
                        size = 1, alpha = 0.5, colour = "blue") +
    ggplot2::geom_point(data = d %>% 
                          dplyr::filter(!ok.kirtlar.osl), 
                        ggplot2::aes(lengd, kynfaeri/oslaegt),
                        colour = "red") +
    ggrepel::geom_text_repel(data = d %>% dplyr::filter(!ok.kirtlar.osl),
                             ggplot2::aes(lengd, kynfaeri/oslaegt, label = lab))
})

Vikmörk-tafla

Column {.tabset}

Lengdir - allar tegundir

DT::renderDataTable({
  le.this.year %>% 
    dplyr::filter(!ok.l,
                  leidangur %in% input$Leidangur) %>%
    dplyr::select(-c(synis_id, index)) %>% 
    dplyr::arrange(leidangur, stod, tegund) %>% 
    DT::datatable(extensions = 'Scroller',
                  rownames = FALSE,
                  options = list(deferRender = TRUE,
                                 scrollY = 700,
                                 scroller = TRUE
                  )) %>% 
    DT::formatStyle('ok.l', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD')))
})
### Kvarnir - eftir tegundum
DT::renderDataTable({
  d <- 
    kv.this.year %>% 
    dplyr::left_join(st %>% 
                       dplyr::select(synis_id, leidangur)) %>% 
    dplyr::filter(tegund == as.numeric(input$Species),
                  (!ok.l.osl | !ok.l.sl | !ok.sl.osl | !ok.kirtlar.osl | !ok.lifur.osl),
                  leidangur %in% input$Leidangur) %>%
    dplyr::left_join(st %>% 
                       dplyr::select(synis_id, leidangur, stod)) %>% 
    dplyr::select(leidangur, stod, index, nr, lengd, oslaegt, slaegt, kynfaeri, lifur, ok.l.osl:ok.lifur.osl)
  d %>% 
    DT::datatable(extensions = 'Scroller',
                  rownames = FALSE,
                  options = list(deferRender = TRUE,
                                 scrollY = 700,
                                 scroller = TRUE
                  )) %>% 
    DT::formatStyle('ok.l.osl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD'))) %>% 
    DT::formatStyle('ok.l.sl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD'))) %>% 
    DT::formatStyle('ok.sl.osl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD'))) %>% 
    DT::formatStyle('ok.kirtlar.osl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD'))) %>% 
    DT::formatStyle('ok.lifur.osl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD')))
})

Kvarnir - allar tegundir

DT::renderDataTable({
  d <- 
    kv.this.year %>% 
    dplyr::left_join(st %>% dplyr::select(synis_id, leidangur)) %>% 
    dplyr::filter((!ok.l.osl | !ok.l.sl | !ok.sl.osl | !ok.kirtlar.osl | !ok.lifur.osl),
                  leidangur %in% input$Leidangur) %>%
    dplyr::left_join(st %>% dplyr::select(synis_id, leidangur, stod)) %>% 
    dplyr::select(leidangur, stod, index, tegund, nr, lengd, oslaegt, slaegt, kynfaeri, lifur, magi, ok.l.osl:ok.lifur.osl)
  d %>% 
    DT::datatable(extensions = 'Scroller',
                  rownames = FALSE,
                  options = list(deferRender = TRUE,
                                 scrollY = 700,
                                 scroller = TRUE
                  )) %>% 
    DT::formatStyle('ok.l.osl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD'))) %>% 
    DT::formatStyle('ok.l.sl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD'))) %>% 
    DT::formatStyle('ok.sl.osl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD'))) %>% 
    DT::formatStyle('ok.kirtlar.osl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD'))) %>% 
    DT::formatStyle('ok.lifur.osl', backgroundColor = DT::styleEqual(c(0, 1), c('pink', '#C1FAAD')))
})

Tegund - fyrsta skipti í reit

DT::renderDataTable({
  res$st %>% 
    dplyr::left_join(res$nu) %>% 
    dplyr::filter(tegund %in% Tegund) %>% 
    dplyr::select(ar, reitur, tegund, fj_alls) %>% 
    dplyr::group_by(reitur, tegund) %>% 
    dplyr::summarise(first.year = min(ar)) %>% 
    dplyr::ungroup() %>% 
    dplyr::arrange(-first.year, tegund) %>% 
    dplyr::left_join(tegund) %>% 
    DT::datatable(extensions = 'Scroller',
                  rownames = FALSE,
                  options = list(deferRender = TRUE,
                                 scrollY = 700,
                                 scroller = TRUE
                  ))
})

Magar

Column {.tabset}

Mælingar á bráð

DT::renderDataTable({
  pp %>% 
    dplyr::filter(leidangur %in% input$Leidangur) %>% 
    DT::datatable(extensions = 'Scroller',
                  rownames = FALSE,
                  options = list(deferRender = TRUE,
                                 scrollY = 700,
                                 scroller = TRUE
                  ))
})

Þyngdardreifing á bráð - Topp 20 bráðir

renderPlot({
  # applly some filter
  d <- 
    pp %>% 
    dplyr::filter(leidangur %in% input$Leidangur) %>% 
    dplyr::filter(astand == 1,
                  # brað verdur ad vera skilgreind
                  !is.na(prey),
                  # verður að hafa mælingu thyngd
                  !is.na(thyngd),
                  # verður að hafa mælingu talid
                  !is.na(n))

  # not really mean based on individual prey measurements
  pp.mean <- 
    d %>% 
    dplyr::group_by(prey) %>% 
    dplyr::summarise(fjoldi = sum(n),
                     mean = sum(thyngd) / sum(n)) %>% 
    dplyr::ungroup() %>% 
    dplyr::arrange(-fjoldi)

  # top 20 (bráð) tegundir "mældar"
  top20 <-
    pp.mean %>% 
    dplyr::slice(1:20)
  top20.brad <-
    top20 %>% 
    dplyr::pull(prey)

  pp %>% 
    dplyr::filter(leidangur %in% input$Leidangur) %>% 
    dplyr::filter(prey %in% top20.brad) %>% 
    ggplot2::ggplot() +
    ggplot2::geom_histogram(ggplot2::aes(m.thyngd)) +
    ggplot2::geom_vline(data = top20, 
                        ggplot2::aes(xintercept = mean), colour = "red") +
    ggplot2::facet_wrap(~ prey, scale = "free") +
    ggplot2::labs(x = "Meðalþyngd", y = "Fjöldi")
})

Samantekt per fisk

DT::renderDataTable({
  pp2 %>% 
    dplyr::filter(leidangur %in% input$Leidangur) %>% 
    DT::datatable(extensions = 'Scroller',
                  rownames = FALSE,
                  options = list(deferRender = TRUE,
                                 scrollY = 700,
                                 scroller = TRUE
                  ))
})

Dreifing á magafylli eftir tegundum

renderPlot({
  pp2 %>% 
    dplyr::filter(leidangur %in% input$Leidangur) %>% 
    ggplot2::ggplot(ggplot2::aes(magafylli)) + 
    ggplot2::geom_histogram() + 
    ggplot2::facet_wrap(~ pred, scale = "free_y") +
    ggplot2::labs(x = "Magafylli", y = "Fjöldi fiska")
})

Togupplýsingar

Column {.tabset}

Lögun - síðustu 20 stöðvar

renderPlot({
  d <-
    last.20 %>% 
    dplyr::mutate(value = ifelse(variable == "vir_uti" & ar < now.year,
                                 value / 1.8288,
                                 value)) %>% 
    dplyr::filter(leidangur %in% input$Leidangur,
                  variable %in% c("larett_opnun", "lodrett_opnun", "vir_uti"))
  d2 <- 
    d %>% 
    dplyr::filter(ar %in% 2013:(now.year - 1))
  d3 <-
    d %>% 
    dplyr::filter(ar %in% now.year)

  d %>% 
    ggplot2::ggplot(ggplot2::aes(reorder(index, id), value)) +
    ggplot2::theme_bw(base_size = 24) +
    ggplot2::geom_violin(scale = "width", colour = "grey") +
    ggplot2::geom_point(data = d2, 
                        ggplot2::aes(colour = factor(ar), group = factor(ar))) +
    ggplot2::geom_line(data = d2,   
                       ggplot2::aes(colour = factor(ar), group = factor(ar))) +
    ggplot2::geom_point(data = d3, ggplot2::aes(group = factor(ar)), lwd = 1) +
    ggplot2::geom_line(data = d3,   
                       ggplot2::aes(group = factor(ar)), lwd = 1) +
    ggplot2::scale_colour_brewer(palette = "Set1") +
    ggplot2::facet_grid(leidangur ~ variable, scale = "free_x") +
    ggplot2::coord_flip() +
    ggplot2::labs(y = NULL, x = NULL, colour = "Year")
})

Umhverfi - síðustu 20 stöðvar

renderPlot({
  d <-
    last.20 %>% 
    dplyr::filter(leidangur %in% input$Leidangur,
                  variable %in% c("botnhiti", "yfirbordshiti"))
  d2 <- 
    d %>% 
    dplyr::filter(ar %in% 2013:(now.year - 1))
  d3 <-
    d %>% 
    dplyr::filter(ar %in% now.year)

  d %>% 
    ggplot2::ggplot(ggplot2::aes(reorder(index, id), value)) +
    ggplot2::theme_bw(base_size = 24) +
    ggplot2::geom_violin(scale = "width", colour = "grey") +
    ggplot2::geom_point(data = d2, 
                        ggplot2::aes(colour = factor(ar), group = factor(ar))) +
    ggplot2::geom_line(data = d2,   
                       ggplot2::aes(colour = factor(ar), group = factor(ar))) +
    ggplot2::geom_point(data = d3, 
                        ggplot2::aes(group = factor(ar)), lwd = 1) +
    ggplot2::geom_line(data = d3,   
                       ggplot2::aes(group = factor(ar)), lwd = 1) +
    ggplot2::scale_colour_brewer(palette = "Set1") +
    ggplot2::facet_grid(leidangur ~ variable, scale = "free_x") +
    ggplot2::coord_flip() +
    ggplot2::labs(y = NULL, x = NULL, colour = "Year")
})

Tímatrend - lögun

renderPlot({

  d <-
    st.dummy %>% 
    dplyr::mutate(value = ifelse(variable == "vir_uti" & ar < now.year,
                                 value / 1.8288,
                                 value)) %>% 
    dplyr::filter(leidangur %in% input$Leidangur,
                  variable %in% c("larett_opnun", "lodrett_opnun", "vir_uti")) 
  d.median <- 
    d %>% 
    dplyr::group_by(ar, variable) %>% 
    dplyr::summarise(value = median(value, na.rm = TRUE)) %>% 
    dplyr::ungroup()

  d %>% 
    ggplot2::ggplot(ggplot2::aes(ar, value)) +
    ggplot2::theme_bw(base_size = 24) +
    ggplot2::geom_violin(ggplot2::aes(group = ar), scale = "width") +
    ggplot2::geom_jitter(ggplot2::aes(group = ar), alpha = 0.2, colour = "red", size = 0.5) + 
    ggplot2::geom_line(data = d.median,
                       colour = "blue") +
    ggplot2::facet_wrap(~ variable, scale = "free_y") +
    ggplot2::labs(x = NULL, y = NULL)

})

Tímatrend - umhverfi

renderPlot({

  d <-
    st.dummy %>% 
    dplyr::filter(leidangur %in% input$Leidangur,
                  variable %in% c("botnhiti", "yfirbordshiti")) 
  d.median <- 
    d %>% 
    dplyr::group_by(ar, variable) %>% 
    dplyr::summarise(value = median(value, na.rm = TRUE)) %>% 
    dplyr::ungroup()

  d %>% 
    ggplot2::ggplot(ggplot2::aes(ar, value)) +
    ggplot2::theme_bw(base_size = 24) +
    ggplot2::geom_violin(ggplot2::aes(group = ar), scale = "width") +
    ggplot2::geom_jitter(ggplot2::aes(group = ar), alpha = 0.2, colour = "red", size = 0.5) + 
    ggplot2::geom_line(data = d.median,
                       colour = "blue") +
    ggplot2::facet_wrap(~ variable, scale = "free_y") +
    ggplot2::labs(x = NULL, y = NULL)
})

Surf and turf

Column {.tabset}

Togfar

leaflet::renderLeaflet({
  p <-
    leaflet::leaflet(stadlar.rallstodvar.sp) %>% 
    leaflet::addTiles() %>% 
    leaflet::addPolylines(color = "red", weight = 10)
  years <- as.character(now.year:(now.year - 3))
  for(i in 1:length(years)) {

    if(i == 1) {
      p <-
        p %>% 
        leaflet::addPolylines(data = st.done.sf[st.done.sf$year == years[i],],
                              group = years[i],
                              label = ~htmltools::htmlEscape(as.character(index)))
    } else {
      p <-
        p %>% 
        leaflet::addPolylines(data = st.done.sf[st.done.sf$year == years[i],],
                              group = years[i])
    }
  }
  p %>% 
    leaflet::addLayersControl(overlayGroups = years,
                              options = leaflet::layersControlOptions(collapsed = FALSE))
})

Af aflabrögðum

Column {.tabset}

Afli

DT::renderDataTable({
  x <- 
    by.station %>% 
    dplyr::filter(ar == now.year) %>% 
    dplyr::left_join(st %>% 
                       dplyr::filter(ar == now.year) %>% 
                       dplyr::select(index, leidangur)) %>% 
    dplyr::group_by(leidangur, tegund) %>% 
    dplyr::summarise(n = round(sum(n.std), 0),
                     b = round(sum(b.std), 0)) %>% 
    dplyr::filter(n > 0) %>% 
    tidyr::gather(variable, value, n:b) %>% 
    dplyr::mutate(variable = paste0(variable, ".", leidangur)) %>% 
    dplyr::ungroup() %>% 
    dplyr::select(-leidangur) %>% 
    tidyr::spread(variable, value) %>%
    dplyr::left_join(fisktegundir) %>% 
    dplyr::mutate(tegund = paste(tegund, heiti)) %>% 
    dplyr::select(-heiti)
  if(input$Type == "Numbers") {
    x %>% 
      dplyr::select(tegund, starts_with("n.")) %>% 
      DT::datatable(extensions = 'Scroller',
                    rownames = FALSE,
                    options = list(deferRender = TRUE,
                                   scrollY = 700,
                                   scroller = TRUE
                    ))
  } else {
    x %>% 
      dplyr::select(tegund, starts_with("b.")) %>% 
      DT::datatable(extensions = 'Scroller',
                    rownames = FALSE,
                    options = list(deferRender = TRUE,
                                   scrollY = 700,
                                   scroller = TRUE
                    ))
  }

})

Mælingar 1


Mælingar 2

renderPlot({

  MAELINGAR %>% 
    ggplot2::ggplot(ggplot2::aes(rel.time, n.cum, colour = leidangur)) +
    ggplot2::theme_grey(base_size = 16) +
    ggplot2::geom_step(lwd = 1) +
    ggplot2::facet_wrap(~ heiti, scale = "free_y") +
    ggplot2::expand_limits(y = 0) +
    ggplot2::labs(x = "Dagar frá fyrstu stöð",
                  y = "Fjöldi",
                  colour = "Leiðangur") +
    ggplot2::scale_color_brewer(palette = "Set1")
})

Mælingar eftir tegundum

renderPlot({

  skr <- 
    readr::read_rds("data2/res.rds")$skraning %>% 
    dplyr::filter(tegund == input$Species) %>% 
    dplyr::group_by(synis_id, maeliadgerd) %>% 
    dplyr::summarise(n = sum(fjoldi)) %>% 
    dplyr::ungroup()
  st2 <- 
    st %>% 
    dplyr::filter(ar == now.year) %>% 
    dplyr::select(synis_id,
                  leidangur,
                  time = togbyrjun) %>% 
    dplyr::group_by(leidangur) %>% 
    dplyr::mutate(start.time = min(time),
                  rel.time = as.numeric(time - start.time) / (60 * 60 * 24),
                  max.time = max(rel.time)) %>% 
    dplyr::select(leidangur, rel.time, max.time, synis_id) %>% 
    dplyr::ungroup() %>% 
    dplyr::left_join(skr) %>% 
    dplyr::arrange(rel.time) %>% 
    tidyr::complete(rel.time, tidyr::nesting(leidangur, maeliadgerd), 
                    fill = list(n = 0)) %>% 
    dplyr::group_by(rel.time, leidangur) %>% 
    tidyr::fill(max.time) %>% 
    dplyr::filter(rel.time <= max.time) %>% 
    dplyr::arrange(rel.time) %>% 
    dplyr::left_join(maeliatridi) %>% 
    dplyr::group_by(leidangur, heiti) %>% 
    dplyr::mutate(n.cum = cumsum(n)) %>% 
    dplyr::ungroup()


  st2 %>% 
    ggplot2::ggplot(ggplot2::aes(rel.time, n.cum, colour = leidangur)) +
    ggplot2::geom_step(lwd = 1) +
    ggplot2::theme_grey(base_size = 16) +
    ggplot2::facet_wrap(~ heiti, scale = "free_y") +
    ggplot2::expand_limits(y = 0) +
    ggplot2::labs(x = "Dagar frá fyrstu stöð",
                  y = "Fjöldi",
                  colour = "Leiðangur") +
    ggplot2::scale_color_brewer(palette = "Set1")
})


fishvice/xe documentation built on Oct. 4, 2023, 11:05 a.m.