library(leaflet)
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(tidyr)
library(ggplot2)
library(DT)
library(lubridate)
##
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
##
## date
library(scales) # install.packages("scales")
Read in and tidy data
data_url <- "https://knb.ecoinformatics.org/knb/d1/mn/v2/object/urn%3Auuid%3Af119a05b-bbe7-4aea-93c6-85434dcb1c5e"
esc <- tryCatch(
read.csv("data/escapement.csv", stringsAsFactors = FALSE),
error=function(cond) {
message(paste("Escapement file does not seem to exist, so get it from the KNB."))
esc <- read.csv(url(data_url, method = "libcurl"), stringsAsFactors = FALSE)
return(esc)
}
)
## Warning in file(file, "rt"): cannot open file 'data/escapement.csv': No such
## file or directory
## Escapement file does not seem to exist, so get it from the KNB.
#code above will try to read in the csv on your project, but won't be able to find it, then will send an error message that can't find on your computer and will go to KNB to find it. Can use this tryCatch function to help with any error that might happen from running anything. Function in base.
head(esc)
## Location SASAP.Region sampleDate Species DailyCount Method Latitude
## 1 Akalura Creek Kodiak 1930-05-24 Sockeye 4 Unknown 57.1641
## 2 Akalura Creek Kodiak 1930-05-25 Sockeye 10 Unknown 57.1641
## 3 Akalura Creek Kodiak 1930-05-26 Sockeye 0 Unknown 57.1641
## 4 Akalura Creek Kodiak 1930-05-27 Sockeye 0 Unknown 57.1641
## 5 Akalura Creek Kodiak 1930-05-28 Sockeye 0 Unknown 57.1641
## 6 Akalura Creek Kodiak 1930-05-29 Sockeye 0 Unknown 57.1641
## Longitude Source
## 1 -154.2287 ADFG
## 2 -154.2287 ADFG
## 3 -154.2287 ADFG
## 4 -154.2287 ADFG
## 5 -154.2287 ADFG
## 6 -154.2287 ADFG
Make annual counts of salmon data
annual_esc <- esc %>%
#mutate(year = lubridate::year(sampleDate))
separate(sampleDate,c("Year", "Month", "Day"),sep = "-") %>% #can use mutate with lubridate instead if just want the year, but the date needs to be in a certain format
mutate(Year = as.numeric(Year)) %>%
group_by(Species, SASAP.Region, Year) %>%
summarize(escapement = sum(DailyCount)) %>%
filter(Species %in% c("Chinook", "Sockeye", "Chum", "Coho", "Pink"))
head(annual_esc)
## # A tibble: 6 x 4
## # Groups: Species, SASAP.Region [1]
## Species SASAP.Region Year escapement
## <chr> <chr> <dbl> <int>
## 1 Chinook Alaska Peninsula and Aleutian Islands 1974 1092
## 2 Chinook Alaska Peninsula and Aleutian Islands 1975 1917
## 3 Chinook Alaska Peninsula and Aleutian Islands 1976 3045
## 4 Chinook Alaska Peninsula and Aleutian Islands 1977 4844
## 5 Chinook Alaska Peninsula and Aleutian Islands 1978 3901
## 6 Chinook Alaska Peninsula and Aleutian Islands 1979 10463
Make some static plots
ggplot(data=annual_esc, mapping = aes(x=Species, y=escapement, fill = SASAP.Region))+
geom_col()
# when see other people's code you will not see data= or the mapping=
annual_esc %>%
filter(SASAP.Region == "Kodiak") %>%
ggplot(mapping=aes(x=Year, y=escapement, color=Species))+
geom_line()+
geom_point(size=3)
#if you don't want to save each subset this is really useful, and you could just read in a list and make a for loop to have multiple plots
kodiak_esc<- annual_esc %>%
filter(SASAP.Region == "Kodiak")
#can also create a new object that has all the custom options that you like to set, also if you use the built in themes and then adjust them with your custom stuff, you can't then add in another theme function because it will override anything you have customized. You could also have a plot setup chunk at the beginning of the r markdown file in a code chunk to set up the plotting theme, or source a function that had the theme arguments in it that you would just source at the beginning of each markdown document.
my_theme <- theme_bw() +
theme(legend.position = "bottom",
legend.title = element_blank())
ggplot(data=kodiak_esc, mapping=aes(x=Year, y=escapement, color=Species))+
geom_line()+
geom_point()+
scale_y_continuous(labels = comma)+
ylab("EScapement (num fish)")+
ggtitle("Kodiak Salmon Escapement")+
#theme_bw()+# nice way to set a bunch of aspects of plot quickly, but might not be what you want, can add options using theme()
#theme(legend.position = "bottom",
# legend.text = element_blank())
my_theme
# to dynamically genearate a plot title
plot_title<- paste(unique(annual_esc$SASAP.Region),"escapement")
#package scales allows easy formatting of tic labels
ggplot(data = annual_esc, mapping = aes(x=Year, y=escapement, color = Species))+
geom_line()+
geom_point()+
scale_y_continuous(labels=comma)+
facet_wrap(~SASAP.Region, scales="free_y", ncol=2)+
my_theme
#syntax for facet is a ~ and then the name over whatever you want to facet over for the multiple plots, could also check into labeler for ggplot options, can add the figure width and height to the settings for the r chunk, or set it up from the beginning. e.g. {r, fig.width=10, fig.height=5}
#plotly is a way to look at more ways to deal with maps, regular r code that generates an html file that, leaflet also manipulates the map/plot
#shiny actually manipulate the data that is hosted on a server
Make some maps
locations <- esc %>%
distinct(Location, Latitude, Longitude) %>%
drop_na()
head(locations)
## Location Latitude Longitude
## 1 Akalura Creek 57.1641 -154.2287
## 4 Anvik River 62.7000 -160.2000
## 6 Ayakulik River 57.1956 -154.5290
## 7 Bear Creek 56.0389 -160.2734
## 8 Bear River 56.0389 -160.2734
## 9 Big Bay Creek 58.5426 -152.5683
# distinct will pull out unique values from whatever you give it
datatable(locations)
#from package DT and makes nice tables for you, but these are only interactive tables that work for html and can't use these to knit a pdf or word docs
leaflet(locations) %>%
addTiles() %>%
addMarkers(lng = ~Longitude, lat = ~Latitude, popup = ~Location)
# broadly think of the ~ as "mapping to", even though it's a modeling tool, leaflet is specifically for maps, plotly is for interactive plots
Prettier map
leaflet(locations) %>%
addWMSTiles("https://www.gebco.net/data_and_products/gebco_web_services/web_map_service/mapserv?",
layers = 'GEBCO_LATEST',
attribution = "Imagery reproduced from the GEBCO_2014 Grid, version 20150318, www.gebco.net") %>%
addCircleMarkers(lng = ~Longitude,
lat = ~Latitude,
popup = ~ Location,
radius = 5,
# set fill properties
fillColor = "salmon",
fillOpacity = 1,
# set stroke properties
stroke = T,
weight = 0.5,
color = "white",
opacity = 1)
#gets the most recent tiles from gebco, also manipulates the circles a bit with radius in the circle markers
# could also make an image in the popup with anything at an url
# popup = "<img src="https://www.nceas.uscsb.edu/files/newLogo_0.png' width='50%'/>",
# or use addPopups()