-
Notifications
You must be signed in to change notification settings - Fork 1
/
ruo_codes.Rmd
213 lines (170 loc) · 5.87 KB
/
ruo_codes.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
---
title: "ruo's codes"
author: "Ruoyu Wang"
date: "2/22/2020"
output: html_document
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(sf)
library(tmap)
library(leaflet)
library(shiny)
library(kableExtra)
library(shinythemes)
library(here)
library(janitor)
library(paletteer)
library(raster)
```
read in data files
```{r}
# Channel islands
channel_islands <- read_csv(here::here("data", "Animals", "channel_islands.csv")) %>%
dplyr::select(latitude, longitude, common_name, iconic_taxon_name) %>%
mutate(iconic_taxon_name = as.character(iconic_taxon_name)) %>%
filter(!iconic_taxon_name %in% c("Actinopterygii", "Animalia")) %>%
mutate(park = "Channel Islands National Park")
channel_islands_sf <- st_as_sf(channel_islands,
coords = c("longitude", "latitude"),
crs = 4326)
# Death Valley
death_valley <- read_csv(here::here("data", "Animals", "death_valley.csv")) %>%
dplyr::select(latitude, longitude, common_name, iconic_taxon_name) %>%
filter(!iconic_taxon_name == "Animalia") %>%
mutate(park = "Death Valley National Park")
death_valley_sf <- st_as_sf(death_valley,
coords = c("longitude", "latitude"),
crs = 4326)
# Joshua Tree
joshua_tree <- read_csv(here::here("data", "Animals", "joshua_tree.csv")) %>%
dplyr::select(latitude, longitude, common_name, iconic_taxon_name) %>%
filter(!iconic_taxon_name %in% c("Animalia", "Plantae")) %>%
mutate(park = "Joshua Tree National Park")
joshua_tree_sf <- st_as_sf(joshua_tree,
coords = c("longitude", "latitude"),
crs = 4326)
```
```{r}
# Yosemite
yosemite <- read.csv(here("data", "Animals", "yosemite.csv")) %>%
clean_names()
yosemite_clean <- yosemite %>%
dplyr::select(longitude, latitude, common_name, iconic_taxon_name) %>%
mutate(iconic_taxon_name = as.character(iconic_taxon_name)) %>%
filter(iconic_taxon_name != "Animalia") %>%
mutate(park = "Yosemite National Park")
yosemite_sf <- st_as_sf(yosemite_clean, coords = c("longitude", "latitude"),
crs = 4326)
# Sequoia
sequoia <- read.csv(here("data", "Animals", "sequoia.csv")) %>%
clean_names()
sequoia_clean <- sequoia %>%
dplyr::select(longitude, latitude, common_name, iconic_taxon_name) %>%
mutate(iconic_taxon_name = as.character(iconic_taxon_name)) %>%
mutate(park = "Sequoia National Park")
sequoia_sf <- st_as_sf(sequoia_clean, coords = c("longitude", "latitude"),
crs = 4326)
# join species observations data
animal <- rbind(channel_islands_sf, death_valley_sf, yosemite_sf, sequoia_sf, joshua_tree_sf)
```
# park boundaries
```{r}
nps <- read_sf(dsn = here("data", "National_Park_Service__Park_Unit_Boundaries"),
layer = "National_Park_Service__Park_Unit_Boundaries", crs = 4326) %>%
clean_names()
nps_ca <- nps %>%
filter(state == "CA")
nps_ca_five <- nps_ca %>%
filter(unit_name %in% c("Death Valley National Park", "Joshua Tree National Park", "Yosemite National Park", "Channel Islands National Park", "Sequoia National Park")) %>%
dplyr::select(unit_name)
```
```{r}
park_animals <- st_join(animal, nps_ca_five, left = FALSE) %>%
tibble::rowid_to_column("ID") %>%
filter(!ID == 3079)
# For POINTS that fall within CA_counties, adds ATTRIBUTES, retains ALL pts if left=TRUE, otherwise uses inner_join
#isd_ca_co_pts <- st_join(isd_history, left = FALSE, ca_co["name"]) # join points
# plot
#plot(park_animals$geometry, pch=21, cex=0.7, col="purple")
#plot(nps_ca_five, border="gray20", col=NA, add = T)
```
```{r}
tmap_mode("view")
tm_shape(nps_ca_five)+
tm_borders("grey40")+
tm_shape(park_animals$geometry)+
tm_dots(labels = "location", col = "purple", size = 0.02)
```
```{r}
# %>%
# addCircleMarkers(data = species(),
# color = ~pal_species()(common_name),
# opacity = 2,
# weight = 1,
# radius = 7) %>%
# addLegend(data = species(),
# title = "Species",
# pal = pal_species(),
# values = ~common_name,
# opacity = 1)
```
```{r}
# # A reactive expression that returns the selected animal groups
#
# # animals(), first filter result data frame
# observeEvent(input$animal_type, {
#
# animals <- reactive({
# park_animals_coords %>%
# filter(park %in% input$unit_name,
# iconic_taxon_name == input$animal_type)
# })
#
#
#
#
#
# # build up choices based on previous selection
#
# updatePickerInput(session = session,
# "species", # inputId
# choices = c(unique(animals()$common_name))
# )
# })
#
# output$map2 <- observeEvent(input$tab3,{
# leafletProxy("map2", data = animals()) %>%
# clearMarkerClusters() %>%
# addMarkers(lng = ~X, lat = ~Y, label = ~common_name,
# clusterOptions = markerClusterOptions())
# })
#
# # observeEvent(input$animal_type,{
# # leafletProxy("map2", data = animals(), session = session) %>%
# # clearMarkerClusters() %>%
# # addMarkers(lng = ~X, lat = ~Y, label = ~common_name,
# # clusterOptions = markerClusterOptions())
# #
# # })
# #proxy <-
#
#
# # species(), second result data frame
# species <- reactive({
# park_animals %>%
# filter(park %in% input$unit_name,
# iconic_taxon_name %in% input$animal_type,
# common_name %in% input$species)
# })
#
# # create species color palette
# n_species <- reactive({
# length(input$species)
# })
#
# pal_species <- reactive({
# leaflet::colorFactor(c(rainbow(n = n_species(), s = 0.5)), species()$common_name)
# })
```