-
Notifications
You must be signed in to change notification settings - Fork 0
/
.Rhistory
259 lines (259 loc) · 9.53 KB
/
.Rhistory
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
gbif <- fread("rawData/0289344-200613084148143.csv",header=TRUE)
library("rgdal")
library("sf")
library('tidyverse')
library('spocc')
library('mapr')
library("scrubr")
library("osmdata")
library("raster")
library("osmdata")
library("taxize")
library("INLA")
library("data.table")
library("sp")
library("spdep")
gbif <- fread("rawData/0289344-200613084148143.csv",header=TRUE)
gbif <- fread("Earth_Lab_Sampler/rawData/0289344-200613084148143.csv",header=TRUE)
gbif <- fread("rawData/0289344-200613084148143.csv",header=TRUE)
GBIF <- gbif %>% scrubr::fix_names( how = 'shortest')
plot(gbif$decimalLatitude, gbif$decimalLongitude)
plot(st_GBIF, pch=19, cex=0.25)
gbif <- fread("rawData/0289344-200613084148143.csv",header=TRUE)
GBIF <- gbif %>% scrubr::fix_names( how = 'shortest')
plot(gbif$decimalLatitude, gbif$decimalLongitude)
# Plot to check that everything worked
plot(st_GBIF, pch=19, cex=0.25)
st_crs(st_GBIF) <- crs(hex_polys)
intersection <- st_intersection(x = hex_polys, y = st_GBIF)
int_result <- intersection %>%
group_by(id_polygons)
plot(g, lwd=0.1)
plot(int_result$geometry, pch=19, cex=0.25, col=adjustcolor("cornflowerblue", alpha.f = 0.2), add=TRUE)
species_richness <- rep(0, max(hex_polys$id_polygons))
hex_counts <- cbind(hex_polys, species_richness)
int_count <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
count()
hex_counts$species_richness[int_count$id_polygons] <- int_count$n
plot(hex_counts$geometry, lwd=0.001,
col=grey.colors(max(hex_counts$species_richness), rev = TRUE, start=0, end=1)[hex_counts$species_richness+1])
plot(int_result$geometry, pch=19, cex=0.05, col=adjustcolor("cornflowerblue", alpha.f = 0.2), add=TRUE)
# Define bounding box
ext <- as(extent(getbb ("Boulder County Colorado")) , "SpatialPolygons")
# Set spatial projection
crs(ext) <- "EPSG:4326"
# Use spsample to measure out a grid of center points within the bounding box
h <- spsample(ext, type = "hexagonal", cellsize = 0.01)
# convert center points to hexagons
g <- HexPoints2SpatialPolygons(h, dx = 0.01)
# Reformat for easy plotting
g <- st_as_sf(g)
# Plot to check that it's built corrrectly
plot(g, lwd=0.1)
# Make the dataset more presentable
hex_polys <- cbind(seq(1, length(g$geometry)), g)
colnames(hex_polys) <- c("id_polygons", "geometry") # change colnames
plot(hex_counts$geometry, lwd=0.001,
col=hcl.colors(max(hex_counts$species_richness)+2, palette = "viridis", alpha = NULL, rev = TRUE, fixup = TRUE)[hex_counts$species_richness+1])
st_crs(st_GBIF) <- crs(hex_polys)
intersection <- st_intersection(x = hex_polys, y = st_GBIF)
int_result <- intersection %>%
group_by(id_polygons)
plot(g, lwd=0.1)
plot(int_result$geometry, pch=19, cex=0.25, col=adjustcolor("cornflowerblue", alpha.f = 0.2), add=TRUE)
green_pts <- covariate_hex_greens(greens)
green_hex <- point_to_hex_mean(hex_polys, green_pts)
hex <- green_hex
plot_variable <- function(hex, variable ){
plot(hex$geometry, lwd=0.001,
col=hcl.colors(max(as.integer(green_hex$area), na.rm = TRUE), palette = "viridis", alpha = NULL, rev = TRUE, fixup = TRUE)[as.integer(green_hex$area, na.rm=TRUE)+1])
}
plot_variable(green_hex, green_hex$area)
# Count the number of points in each bin
point_to_hex_count <- function(hex_polys, points){
intersection <- st_intersection(x = hex_polys, y = points)
counter <- rep(0, max(hex_polys$id_polygons))
hex_count <- cbind(hex_polys, counter)
counted <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
count()
hex_count$area[counted$id_polygons] <- counted$n
return(hex_count)
}
# Report the mean value for each bin
point_to_hex_mean <- function(hex_polys, points){
intersection <- st_intersection(x = hex_polys, y = points)
area <- rep(0, max(hex_polys$id_polygons))
hex_area <- cbind(hex_polys, area)
avg_area <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
summarise(mean = mean(points))
hex_area$area[avg_area$id_polygons] <- avg_area$mean
return(hex_area)
}
green_pts <- covariate_hex_greens(greens)
green_hex <- point_to_hex_mean(hex_polys, green_pts)
hex <- green_hex
plot_variable <- function(hex, variable ){
plot(hex$geometry, lwd=0.001,
col=hcl.colors(max(as.integer(green_hex$area), na.rm = TRUE), palette = "viridis", alpha = NULL, rev = TRUE, fixup = TRUE)[as.integer(green_hex$area, na.rm=TRUE)+1])
}
plot_variable(green_hex, green_hex$area)
green_hex
green_hex$area
green_pts
green_hex <- point_to_hex_mean(hex_polys, green_pts)
green_hex
hex <- green_hex
plot(hex$geometry, lwd=0.001,
col=hcl.colors(max(as.integer(green_hex$area), na.rm = TRUE), palette = "viridis", alpha = NULL, rev = TRUE, fixup = TRUE)[as.integer(green_hex$area, na.rm=TRUE)+1])
max(as.integer(green_hex$area)
, na.rm = TRUE)
green_hex
green_hex$area
green_pts
intersection <- st_intersection(x = hex_polys, y = points)
intersection
area <- rep(0, max(hex_polys$id_polygons))
hex_area <- cbind(hex_polys, area)
avg_area <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
summarise(mean = mean(points))
avg_area
avg_area$id_polygons
avg_area$mean
avg_area
intersection %>%
group_by(id_polygons, .drop = FALSE)
mean(points)
points
hex_polys
points
area
hex_area
intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
summarise(mean = mean(points))
avg_area <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
summarise(mean = mean(points))
avg_area$mean
avg_area <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
summarise(mean = mean(points, na.rm=TRUE))
avg_area
avg_area$mean
points
# Report the mean value for each bin
point_to_hex_mean <- function(hex_polys, points){
intersection <- st_intersection(x = hex_polys, y = points)
area <- rep(0, max(hex_polys$id_polygons))
hex_area <- cbind(hex_polys, area)
avg_area <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
summarise(mean = mean(points$log_area, na.rm=TRUE))
hex_area$area[avg_area$id_polygons] <- avg_area$mean
return(hex_area)
}
green_pts <- covariate_hex_greens(greens)
green_hex <- point_to_hex_mean(hex_polys, green_pts)
hex <- green_hex
plot_variable <- function(hex, variable ){
plot(hex$geometry, lwd=0.001,
col=hcl.colors(max(as.integer(green_hex$area), na.rm = TRUE), palette = "viridis", alpha = NULL, rev = TRUE, fixup = TRUE)[as.integer(green_hex$area, na.rm=TRUE)+1])
}
plot_variable(green_hex, green_hex$area)
green_pts <- covariate_hex_greens(greens)
green_hex <- point_to_hex_mean(hex_polys, green_pts)
hex <- green_hex
plot_variable <- function(hex, variable ){
plot(hex$geometry, lwd=0.001,
col=hcl.colors(max(as.integer(green_hex$area)+2, na.rm = TRUE), palette = "viridis", alpha = NULL, rev = TRUE, fixup = TRUE)[as.integer(green_hex$area, na.rm=TRUE)+1])
}
plot_variable(green_hex, green_hex$area)
point_to_hex_mean <- function(hex_polys, points){
intersection <- st_intersection(x = hex_polys, y = points)
area <- rep(0, max(hex_polys$id_polygons))
hex_area <- cbind(hex_polys, area)
avg_area <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
summarise(mean = mean(log_area, na.rm=TRUE))
hex_area$area[avg_area$id_polygons] <- avg_area$mean
return(hex_area)
}
green_pts <- covariate_hex_greens(greens)
green_hex <- point_to_hex_mean(hex_polys, green_pts)
hex <- green_hex
plot_variable <- function(hex, variable ){
plot(hex$geometry, lwd=0.001,
col=hcl.colors(max(as.integer(green_hex$area)+2, na.rm = TRUE), palette = "viridis", alpha = NULL, rev = TRUE, fixup = TRUE)[as.integer(green_hex$area, na.rm=TRUE)+1])
}
plot_variable(green_hex, green_hex$area)
# Count the number of points in each bin
point_to_hex_count <- function(hex_polys, points){
intersection <- st_intersection(x = hex_polys, y = points)
counter <- rep(0, max(hex_polys$id_polygons))
hex_count <- cbind(hex_polys, counter)
counted <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
count()
hex_count$area[counted$id_polygons] <- counted$n
return(hex_count)
}
# Report the mean value for each bin
point_to_hex_mean <- function(hex_polys, points){
intersection <- st_intersection(x = hex_polys, y = points)
area <- rep(0, max(hex_polys$id_polygons))
hex_area <- cbind(hex_polys, area)
avg_area <- intersection %>%
group_by(id_polygons, .drop = FALSE) %>%
summarise(mean = sum(log_area, na.rm=TRUE))
hex_area$area[avg_area$id_polygons] <- avg_area$mean
return(hex_area)
}
green_pts <- covariate_hex_greens(greens)
green_hex <- point_to_hex_mean(hex_polys, green_pts)
hex <- green_hex
plot_variable <- function(hex, variable ){
plot(hex$geometry, lwd=0.001,
col=hcl.colors(max(as.integer(green_hex$area)+2, na.rm = TRUE), palette = "viridis", alpha = NULL, rev = TRUE, fixup = TRUE)[as.integer(green_hex$area, na.rm=TRUE)+1])
}
plot_variable(green_hex, green_hex$area)
hex_list <- st_drop_geometry(hex_counts) %>% inner_join(as.data.frame(hex_green_area), by="id_polygons")
hex_list <- st_drop_geometry(hex_counts) %>% inner_join(as.data.frame(green_hex$area), by="id_polygons")
hex_list <- st_drop_geometry(hex_counts) %>% inner_join(as.data.frame(green_hex), by="id_polygons")
colnames(hex_list) <- c("id_polygon", "species_richness", "park_size_average","geometry")
hex_list
library("rgdal")
library("sf")
library('tidyverse')
library('spocc')
library('mapr')
library("scrubr")
library("osmdata")
library("raster")
library("osmdata")
library("taxize")
library("INLA")
library("data.table")
library("sp")
library("spdep")
install.packages("ggregplot")
install.packages("devtools")
install.packages("devtools")
install.packages("devtools")
install.packages("devtools")
githubinstall("ggregplot")
install.packages("githubinstall")
library("githubinstall")
githubinstall("ggregplot")
githubinstall()
install_github("gfalbery/ggregplot")
install.packages("devtools")
library(devtools)
library(devtools)
install.packages("devtools")
library(devtools)
install_github("gfalbery/ggregplot")
library(devtools)