-
Notifications
You must be signed in to change notification settings - Fork 0
/
plots.Rmd
288 lines (227 loc) · 7.78 KB
/
plots.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
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
---
title: "Plots"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Plots}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
This vignette demonstrates how this package can be used to plot camera trap data.
```{r setup, message=FALSE, results='hide'}
library(camtrapviz)
library(dplyr)
library(ggiraph)
library(ggplot2)
library(RColorBrewer)
```
## Import and prepare data
```{r}
data(recordTableSample, package = "camtrapR")
data(camtraps, package = "camtrapR")
```
```{r}
recordTableSample$DateTimeOriginal <- as.POSIXct(recordTableSample$DateTimeOriginal)
recordTableSample$Date <- as.Date(recordTableSample$Date)
recordTableSample$Time <- chron::times(recordTableSample$Time)
camtraps$Setup_date <- as.Date(camtraps$Setup_date, format = "%d/%m/%Y")
camtraps$Retrieval_date <- as.Date(camtraps$Retrieval_date, format = "%d/%m/%Y")
```
## Plot observations
The package provides a function to plot the observations for each camera.
```{r}
plot_points(recordTableSample,
cam_col = "Station",
datetime_col = "DateTimeOriginal")
```
In case a camera was in the data but no observations were made, you can provide a vector of cameras that should show up on the graph:
```{r}
all_cam <- c("StationA", "StationB", "StationC", "StationD")
plot_points(recordTableSample,
cam_col = "Station",
datetime_col = "DateTimeOriginal",
cam_vec = all_cam)
```
`date_col` and `time_col` can also be used instead of `datetime_col`.
```{r}
plot_points(recordTableSample,
cam_col = "Station",
date_col = "Date",
time_col = "Time")
```
It is also possible to provide a column for points colors:
```{r}
plot_points(recordTableSample,
cam_col = "Station",
points_col = "Species",
datetime_col = "DateTimeOriginal")
```
By providing additional info, it is also possible to visualize the sampling period on the same graph.
```{r}
plot_points(recordTableSample,
cam_col = "Station",
points_col = "Species",
datetime_col = "DateTimeOriginal",
dfcam = camtraps,
setup_dfcam = "Setup_date",
retrieval_dfcam = "Retrieval_date")
```
Various options are available to customize this graph:
```{r, echo = TRUE}
spp <- unique(recordTableSample$Species)
pal <- RColorBrewer::brewer.pal(length(spp), "Purples")
plot_points(recordTableSample,
cam_col = "Station",
points_col = "Species",
datetime_col = "DateTimeOriginal",
dfcam = camtraps,
setup_dfcam = "Setup_date",
retrieval_dfcam = "Retrieval_date",
fill_rect = "darkgoldenrod1",
alpha_rect = 0.6,
col_rect = NA,
height_rect = 0.5,
ptsize = 3,
date_breaks = "1 month",
date_limits = as.POSIXct(c("2009-02-01",
"2009-06-01"),
tz = "UTC"),
date_format = "%a %d %B",
text_x_angle = 45,
cols = pal)
```
Using `ggiraph`, the plot can also be made interactive:
```{r}
p <- plot_points(recordTableSample,
cam_col = "Station",
points_col = "Species",
datetime_col = "DateTimeOriginal",
dfcam = camtraps,
setup_dfcam = "Setup_date",
retrieval_dfcam = "Retrieval_date",
interactive = TRUE)
ggiraph::girafe(ggobj = p)
```
## Plot cameras
The package also provides a function to plot the cameras with Leaflet.
Here, the dataset is already projected using the UTM zone 50 (East Asia)/ Therefore, we provide the EPSG code to use (EPSG:32650).
```{r}
plot_map(camtraps,
cam_col = "Station",
lat_col = "utm_y", lon_col = "utm_x",
crs = 32650)
```
It is also possible to display cameras names:
```{r}
plot_map(camtraps,
cam_col = "Station",
lat_col = "utm_y", lon_col = "utm_x",
display_camnames = TRUE,
crs = 32650)
```
Additional information can also be displayed using the radius of the camera points. Here, we choose to make circles sizes proportional to the abundance of one species at each camera:
```{r}
# Get abundance per camera
PBE_abundance <- recordTableSample |>
filter(Species == "PBE") |>
group_by(Station) |>
summarise(count = n())
radius <- PBE_abundance$count
names(radius) <- PBE_abundance$Station
radius
```
```{r}
plot_map(camtraps,
cam_col = "Station",
lat_col = "utm_y", lon_col = "utm_x",
radius = radius,
label = paste("Camera:", names(radius),
"| abundance:", radius),
crs = 32650)
```
If data is missing, it will show up in purple on the map.
```{r}
radius_missing <- radius[1:2]
plot_map(camtraps,
cam_col = "Station",
lat_col = "utm_y", lon_col = "utm_x",
radius = radius_missing,
label = paste("Camera:", names(radius_missing),
"| abundance:", radius_missing),
crs = 32650)
```
## Plot species
The package also includes a simple function to plot species counts.
```{r}
plot_species_bars(recordTableSample,
spp_col = "Species")
```
If there is a count column, it can also be included.
```{r}
with_count <- recordTableSample |>
mutate(count = 2)
plot_species_bars(with_count,
spp_col = "Species", count_col = "count")
```
If there is an observation type column, `NA` values in the species column will be replaced with the values in observation type. This is primarily intended for the camtrapDP standard, where non-animal observations have a `NA` in the species column.
Here, we add an observation type column to the data and replace some species with `NA`.
```{r}
with_obstype <- recordTableSample |>
mutate(type = ifelse(Species == "PBE", "blank", "animal"),
.after = Species) |>
mutate(Species = ifelse(Species == "PBE", NA, Species))
with_obstype |>
select(Station, Species, type) |>
head()
```
```{r}
plot_species_bars(with_obstype,
spp_col = "Species",
obstype_col = "type")
```
Finally, this plot can also be made interactive using `ggiraph`:
```{r}
p <- plot_species_bars(recordTableSample,
spp_col = "Species",
interactive = TRUE)
girafe(ggobj = p)
```
## Plot activity
It is also possible to explore the activity times of a given species:
```{r}
# Get records corresponding only to PBE
PBE_records <- recordTableSample |>
filter(Species == "PBE")
p <- plot_activity(dfrec = PBE_records,
time_dfrec = "Time",
unit = "clock",
interactive = TRUE)
girafe(ggobj = p)
```
Another vignette gives a more in-depth overview of this type of plots (see [here](https://lisanicvert.github.io/camtrapviz/articles/activity-patterns.html)).
# Plot diversity
It is also possible to plot diversity indices:
```{r}
# Get diversity
spp_cam <- summarize_species(recordTableSample,
spp_col = "Species",
cam_col = "Station",
by_cam = TRUE)
div <- get_diversity_indices(spp_cam,
spp_col = "Species",
cam_col = "Station")
```
```{r}
p <- plot_diversity(div,
div_col = "simpson",
cam_col = "Station",
interactive = TRUE) +
ggtitle("Simpson diversity index")
girafe(ggobj = p)
```
Another vignette gives a more in-depth overview of this type of plots (see [here](https://lisanicvert.github.io/camtrapviz/articles/diversity.html)).