# libraries
library(here)
## here() starts at C:/Users/halie.ofarrell/OneDrive - Florida Fish and Wildlife Conservation/Stock Assessments/Software/R/r3-exercises
library(readr)
library(DT)
# variables
url_ac <- "https://oceanview.pfeg.noaa.gov/erddap/tabledap/cciea_AC.csv"
csv_ac <- here("data/cciea_AC.csv")
# read data
d_ac <- read_csv(url_ac, col_names = F, skip = 2)
##
## -- Column specification --------------------------------------------------------
## cols(
## .default = col_double(),
## X1 = col_datetime(format = "")
## )
## i Use `spec()` for the full column specifications.
names(d_ac) <- names(read_csv(url_ac))
##
## -- Column specification --------------------------------------------------------
## cols(
## .default = col_character()
## )
## i Use `spec()` for the full column specifications.
# show data
datatable(d_ac)
ggplot2+ geom_line()library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(ggplot2)
# subset data
d_coast <- d_ac %>%
# select columns
select(time, total_fisheries_revenue_coastwide) %>%
# filter rows
filter(!is.na(total_fisheries_revenue_coastwide))
datatable(d_coast)
# ggplot object
p_coast <- d_coast %>%
# setup aesthetics
ggplot(aes(x = time, y = total_fisheries_revenue_coastwide)) +
# add geometry
geom_line()
# show plot
p_coast

+ geom_smooth()Add a smooth layer based on linear model
p_coast + geom_smooth(method = "gam")
## `geom_smooth()` using formula 'y ~ s(x, bs = "cs")'

+ geom_histogram()d_coast %>%
# setup aesthetics
ggplot(aes(x = total_fisheries_revenue_coastwide)) +
# add geometry
geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

aes(color = region)library(stringr)
library(tidyr)
d_rgn <- d_ac %>%
# select columns
select(time, starts_with("total_fisheries_revenue")) %>%
# exclude column
select(-total_fisheries_revenue_coastwide) %>%
# pivot longer all columns but time
pivot_longer(-time) %>%
#mutate region by stripping "other"
mutate(region = name %>%
str_replace("total_fisheries_revenue_", "") %>%
str_to_upper()) %>%
# filter for not NA
filter(!is.na(value)) %>%
# select columns
select(time, region, value)
# create plot object
p_rgn <- ggplot(d_rgn,
# aesthetics
aes(x = time, y = value, group = region, color = region)) +
# geometry
geom_line()
# show plot
p_rgn

+ labs()p_rgn <- p_rgn +
labs(title = "Fisheries Revenue", x = "Year", y = "Millions $ (year 2015)", color = "Region")
p_rgn

facet_wrap()p_rgn + facet_wrap(vars(region))

+ geom_col()library(glue)
##
## Attaching package: 'glue'
## The following object is masked from 'package:dplyr':
##
## collapse
library(lubridate)
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
# Determine terminal year
yr_max <- year(max(d_rgn$time))
d_rgn %>%
# filter by most recent time
filter(year(time) == yr_max) %>%
# set up aesthetics
ggplot(aes(x = region, y = value, fill = region)) +
# add geometry
geom_col() +
# add labels
labs(title = glue("Fisheries Revenue for {yr_max}"), x = "Region", y = "Millions $ (year 2015)", fill = "Region")

+ geom_boxplot()d_rgn %>%
# set up aesthetics
ggplot(aes(x = region, y = value, fill = region)) +
# add geometry
geom_boxplot() +
# add labels
labs(title = "Fisheries Revenue Variability", x = "Region", y = "Millions $ (year 2015)") +
# drop legend because it is redundant with x axis
theme(legend.position = "none")

+ geom_violin()p_rgn_violin <- d_rgn %>%
# set up aesthetics
ggplot(aes(x = region, y = value, fill = region)) +
# add geometry
geom_violin() +
# add labels
labs(title = "Fisheries Revenue Variability", x = "Region", y = "Millions $ (year 2015)") +
# drop legend because it is redundant with x axis
theme(legend.position = "none")
p_rgn_violin

theme()p_rgn_violin + theme_classic()

plotly or dygraphsplotly::ggplotlylibrary(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
ggplotly(p_rgn)
dygraphs::dygraphlibrary(dygraphs)
#packages requires wide format
d_rgn_wide <- d_rgn %>%
mutate(Year = year(time)) %>%
select(Year, region, value) %>%
pivot_wider(names_from = region, values_from = value)
datatable(d_rgn_wide)
d_rgn_wide %>%
dygraph() %>%
dyRangeSelector()