-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpublishing.Rmd
169 lines (130 loc) · 5.74 KB
/
publishing.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
---
title: "Publication Graphics"
author: "Kalei Shotwell"
date: "2/6/2020"
output: html_document
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(leaflet)
library(dplyr)
library(tidyr)
library(ggplot2)
library(DT)
library(lubridate)
library(scales) # install.packages("scales")
```
## Read in and tidy data
```{r}
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)
}
)
#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)
```
## Make annual counts of salmon data
```{r}
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)
```
## Make some static plots
```{r}
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=
```
```{r}
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
```
```{r}
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
```
```{r}
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
```{r}
locations <- esc %>%
distinct(Location, Latitude, Longitude) %>%
drop_na()
head(locations)
# 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
```
```{r}
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
```{r}
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()
```