This is an R Markdown Notebook. When you execute code within the notebook, the results appear beneath the code.
Try executing this chunk by clicking the Run button within the chunk or by placing your cursor inside it and pressing Ctrl+Shift+Enter.
plot(cars)
Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.
When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).
The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.
#ALL CODE FOR ASSIGNMENT 2
install.packages(c("tidyverse", "pwr"))
Error in install.packages : Updating loaded packages
library(tidyverse)
library(lubridate)
#NEW CODE FOR GLM INFORMATION
images <- read_csv("images.csv")
# Confirm exact species names
images %>%
count(common_name, sort = TRUE)
# Filter focal species and count detections per deployment
species_counts <- images %>%
filter(common_name %in% c(
"Harvey's Duiker",
"Suni",
"Bushbuck",
"Blue Duiker"
)) %>%
count(deployment_id, common_name, name = "occurences")
#keep values only with TZA-001 to match
library(dplyr)
dat_names_cleaned <- species_counts %>%
filter(grepl("^TZA-001", deployment_id, ignore.case = TRUE))
library(tidyverse)
library(lubridate)
species_wide <- dat_names_cleaned %>%
tidyr::pivot_wider(
names_from = common_name,
values_from = occurences,
values_fill = 0
) %>%
rename(
harveys_duiker = `Harvey's Duiker`,
suni = `Suni`,
bushbuck = `Bushbuck`,
blue_duiker = `Blue Duiker`
)
glimpse(species_wide)
Rows: 92
Columns: 5
$ deployment_id <chr> "TZA-001-D0005", "TZA-001-D0010", "TZA-001-D0019", "TZA-001-D0025", "TZA-001-D0031", "T…
$ harveys_duiker <int> 10, 5, 26, 9, 4, 4, 1, 0, 21, 2, 5, 8, 1, 0, 3, 0, 0, 1, 1, 2, 0, 1, 1, 1, 3, 2, 0, 0, …
$ suni <int> 4, 0, 4, 4, 0, 1, 0, 0, 1, 0, 4, 6, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 2, 3, 1, …
$ blue_duiker <int> 0, 0, 0, 0, 0, 4, 1, 1, 0, 0, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ bushbuck <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#GLM CALCULATIONS
m_suni <- glm(
suni ~ harveys_duiker,
family = poisson,
data = species_wide
)
m_bushbuck <- glm(
bushbuck ~ harveys_duiker,
family = poisson,
data = species_wide
)
m_blue <- glm(
blue_duiker ~ harveys_duiker,
family = poisson,
data = species_wide
)
summary(m_suni)
Call:
glm(formula = suni ~ harveys_duiker, family = poisson, data = species_wide)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.46900 0.09551 4.910 9.1e-07 ***
harveys_duiker 0.02527 0.01576 1.603 0.109
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 295.54 on 91 degrees of freedom
Residual deviance: 293.24 on 90 degrees of freedom
AIC: 431.62
Number of Fisher Scoring iterations: 6
summary(m_bushbuck)
Call:
glm(formula = bushbuck ~ harveys_duiker, family = poisson, data = species_wide)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.96421 0.20673 -4.664 3.1e-06 ***
harveys_duiker -0.01139 0.04361 -0.261 0.794
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 171.34 on 91 degrees of freedom
Residual deviance: 171.27 on 90 degrees of freedom
AIC: 203.38
Number of Fisher Scoring iterations: 7
summary(m_blue)
Call:
glm(formula = blue_duiker ~ harveys_duiker, family = poisson,
data = species_wide)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.75239 0.34171 -5.128 2.92e-07 ***
harveys_duiker -0.09051 0.10812 -0.837 0.402
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 67.513 on 91 degrees of freedom
Residual deviance: 66.542 on 90 degrees of freedom
AIC: 89.035
Number of Fisher Scoring iterations: 6
#GLM CALCULATIONS
m_suni <- glm(
suni ~ harveys_duiker,
family = poisson,
data = species_wide
)
m_bushbuck <- glm(
bushbuck ~ harveys_duiker,
family = poisson,
data = species_wide
)
m_blue <- glm(
blue_duiker ~ harveys_duiker,
family = poisson,
data = species_wide
)
summary(m_suni)
Call:
glm(formula = suni ~ harveys_duiker, family = poisson, data = species_wide)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) 0.46900 0.09551 4.910 9.1e-07 ***
harveys_duiker 0.02527 0.01576 1.603 0.109
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 295.54 on 91 degrees of freedom
Residual deviance: 293.24 on 90 degrees of freedom
AIC: 431.62
Number of Fisher Scoring iterations: 6
summary(m_bushbuck)
Call:
glm(formula = bushbuck ~ harveys_duiker, family = poisson, data = species_wide)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -0.96421 0.20673 -4.664 3.1e-06 ***
harveys_duiker -0.01139 0.04361 -0.261 0.794
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 171.34 on 91 degrees of freedom
Residual deviance: 171.27 on 90 degrees of freedom
AIC: 203.38
Number of Fisher Scoring iterations: 7
summary(m_blue)
Call:
glm(formula = blue_duiker ~ harveys_duiker, family = poisson,
data = species_wide)
Coefficients:
Estimate Std. Error z value Pr(>|z|)
(Intercept) -1.75239 0.34171 -5.128 2.92e-07 ***
harveys_duiker -0.09051 0.10812 -0.837 0.402
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
(Dispersion parameter for poisson family taken to be 1)
Null deviance: 67.513 on 91 degrees of freedom
Residual deviance: 66.542 on 90 degrees of freedom
AIC: 89.035
Number of Fisher Scoring iterations: 6
#CODE TO CREATE ANTELOPE DENSITY COMPARISON GRAPH
#stuff from original code
install.packages(c("tidyverse", "pwr"))
Error in install.packages : Updating loaded packages
library(tidyverse)
install.packages(c("tidyverse", "pwr"))
WARNING: Rtools is required to build R packages but is not currently installed. Please download and install the appropriate version of Rtools before proceeding:
https://cran.rstudio.com/bin/windows/Rtools/
Warning in install.packages :
package ‘tidyverse’ is in use and will not be installed
trying URL 'https://cran.rstudio.com/bin/windows/contrib/4.5/pwr_1.3-0.zip'
Content type 'application/zip' length 161960 bytes (158 KB)
downloaded 158 KB
package ‘pwr’ successfully unpacked and MD5 sums checked
The downloaded binary packages are in
C:\Users\tyl25njr\AppData\Local\Temp\Rtmpa2MJe9\downloaded_packages
library(lubridate)
#dataset load
dataset <- read_csv("images.csv")
head(dataset)
glimpse(dataset)
Rows: 5,017
Columns: 30
$ project_id <dbl> 2003500, 2003500, 2003500, 2003500, 2003500, 2003500, 2003500, 2003500, 200350…
$ deployment_id <chr> "dfdb6f1c-9d8d-4d1f-86aa-7692f9c3d51a", "TZA-001-D0169", "e221a522-0668-4c7f-b…
$ image_id <chr> "514a68e6-776e-455f-8848-9f8f3a51071c", "478f8b7a-d1c1-4bad-9d6c-c8978a4d4915"…
$ sequence_id <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ filename <chr> "TZA-001-D0001-I000027", "TZA-001-D0169-I003880", "TZA-001-D0039-I001538", "TZ…
$ location <chr> "https://app.wildlifeinsights.org/download/2016630/project/2003500/data-files/…
$ is_blank <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ identified_by <chr> "F. Rovero - Batch Upload", "F. Rovero - Batch Upload", "F. Rovero - Batch Upl…
$ wi_taxon_id <chr> "7b40cf9f-0dae-431d-a541-7d465845ff97", "1bec402e-f98b-440c-bdf3-9ac196403fbb"…
$ class <chr> "Mammalia", "Mammalia", "Mammalia", "Mammalia", NA, "Mammalia", "Mammalia", "M…
$ order <chr> "Cetartiodactyla", "Cetartiodactyla", "Cetartiodactyla", "Cetartiodactyla", NA…
$ family <chr> "Bovidae", "Bovidae", "Bovidae", "Bovidae", NA, "Bovidae", "Bovidae", "Bovidae…
$ genus <chr> "Nesotragus", "Cephalophus", "Nesotragus", "Cephalophus", NA, "Nesotragus", "C…
$ species <chr> "moschatus", "harveyi", "moschatus", "harveyi", NA, "moschatus", "harveyi", "h…
$ common_name <chr> "Suni", "Harvey's Duiker", "Suni", "Harvey's Duiker", "Animal", "Suni", "Harve…
$ uncertainty <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ timestamp <dttm> 2003-12-23 07:19:00, 2006-11-15 08:23:00, 2005-02-13 13:46:00, 2004-11-22 10:…
$ age <chr> "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "…
$ sex <chr> "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "…
$ animal_recognizable <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, …
$ individual_id <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ number_of_objects <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
$ individual_animal_notes <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ behavior <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ highlighted <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F…
$ markings <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ cv_confidence <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ license <chr> "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY…
$ fuzzed <lgl> TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, F…
$ deployment_fuzzed <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F…
names(dataset)
[1] "project_id" "deployment_id" "image_id" "sequence_id"
[5] "filename" "location" "is_blank" "identified_by"
[9] "wi_taxon_id" "class" "order" "family"
[13] "genus" "species" "common_name" "uncertainty"
[17] "timestamp" "age" "sex" "animal_recognizable"
[21] "individual_id" "number_of_objects" "individual_animal_notes" "behavior"
[25] "highlighted" "markings" "cv_confidence" "license"
[29] "fuzzed" "deployment_fuzzed"
dim(dataset)
[1] 5017 30
glimpse(dataset)
Rows: 5,017
Columns: 30
$ project_id <dbl> 2003500, 2003500, 2003500, 2003500, 2003500, 2003500, 2003500, 2003500, 200350…
$ deployment_id <chr> "dfdb6f1c-9d8d-4d1f-86aa-7692f9c3d51a", "TZA-001-D0169", "e221a522-0668-4c7f-b…
$ image_id <chr> "514a68e6-776e-455f-8848-9f8f3a51071c", "478f8b7a-d1c1-4bad-9d6c-c8978a4d4915"…
$ sequence_id <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ filename <chr> "TZA-001-D0001-I000027", "TZA-001-D0169-I003880", "TZA-001-D0039-I001538", "TZ…
$ location <chr> "https://app.wildlifeinsights.org/download/2016630/project/2003500/data-files/…
$ is_blank <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ identified_by <chr> "F. Rovero - Batch Upload", "F. Rovero - Batch Upload", "F. Rovero - Batch Upl…
$ wi_taxon_id <chr> "7b40cf9f-0dae-431d-a541-7d465845ff97", "1bec402e-f98b-440c-bdf3-9ac196403fbb"…
$ class <chr> "Mammalia", "Mammalia", "Mammalia", "Mammalia", NA, "Mammalia", "Mammalia", "M…
$ order <chr> "Cetartiodactyla", "Cetartiodactyla", "Cetartiodactyla", "Cetartiodactyla", NA…
$ family <chr> "Bovidae", "Bovidae", "Bovidae", "Bovidae", NA, "Bovidae", "Bovidae", "Bovidae…
$ genus <chr> "Nesotragus", "Cephalophus", "Nesotragus", "Cephalophus", NA, "Nesotragus", "C…
$ species <chr> "moschatus", "harveyi", "moschatus", "harveyi", NA, "moschatus", "harveyi", "h…
$ common_name <chr> "Suni", "Harvey's Duiker", "Suni", "Harvey's Duiker", "Animal", "Suni", "Harve…
$ uncertainty <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ timestamp <dttm> 2003-12-23 07:19:00, 2006-11-15 08:23:00, 2005-02-13 13:46:00, 2004-11-22 10:…
$ age <chr> "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "…
$ sex <chr> "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "Unknown", "…
$ animal_recognizable <lgl> TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, …
$ individual_id <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ number_of_objects <dbl> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, …
$ individual_animal_notes <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ behavior <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ highlighted <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F…
$ markings <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ cv_confidence <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA…
$ license <chr> "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY", "CC-BY…
$ fuzzed <lgl> TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, F…
$ deployment_fuzzed <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F…
colSums(is.na(dataset))
project_id deployment_id image_id sequence_id
0 0 16 5017
filename location is_blank identified_by
0 16 5017 0
wi_taxon_id class order family
0 28 52 126
genus species common_name uncertainty
137 167 6 5017
timestamp age sex animal_recognizable
0 0 0 0
individual_id number_of_objects individual_animal_notes behavior
5017 0 5017 5017
highlighted markings cv_confidence license
0 5017 5017 0
fuzzed deployment_fuzzed
0 0
#dataset 2 deployments
dataset2 <- read_csv("deployments.csv")
#selecting columns
dat_names <- dataset %>% select(filename, deployment_id, common_name, timestamp)
#selecting columns from dataset2
dat_names2 <- dataset2 %>% select(deployment_id, longitude, latitude)
#rows in dataset
nrow(dataset)
[1] 5017
summary(dataset)
project_id deployment_id image_id sequence_id filename location
Min. :2003500 Length:5017 Length:5017 Mode:logical Length:5017 Length:5017
1st Qu.:2003500 Class :character Class :character NA's:5017 Class :character Class :character
Median :2003500 Mode :character Mode :character Mode :character Mode :character
Mean :2003500
3rd Qu.:2003500
Max. :2003500
is_blank identified_by wi_taxon_id class order family
Mode:logical Length:5017 Length:5017 Length:5017 Length:5017 Length:5017
NA's:5017 Class :character Class :character Class :character Class :character Class :character
Mode :character Mode :character Mode :character Mode :character Mode :character
genus species common_name uncertainty timestamp
Length:5017 Length:5017 Length:5017 Mode:logical Min. :2003-12-15 07:05:00
Class :character Class :character Class :character NA's:5017 1st Qu.:2004-11-23 00:00:00
Mode :character Mode :character Mode :character Median :2005-12-22 03:41:00
Mean :2006-06-18 13:08:16
3rd Qu.:2007-07-26 18:40:00
Max. :2009-11-23 10:53:00
age sex animal_recognizable individual_id number_of_objects
Length:5017 Length:5017 Mode:logical Mode:logical Min. : 1.000
Class :character Class :character TRUE:5017 NA's:5017 1st Qu.: 1.000
Mode :character Mode :character Median : 1.000
Mean : 1.037
3rd Qu.: 1.000
Max. :10.000
individual_animal_notes behavior highlighted markings cv_confidence license
Mode:logical Mode:logical Mode :logical Mode:logical Mode:logical Length:5017
NA's:5017 NA's:5017 FALSE:5017 NA's:5017 NA's:5017 Class :character
Mode :character
fuzzed deployment_fuzzed
Mode :logical Mode :logical
FALSE:2173 FALSE:5017
TRUE :2844
#removing void longitude
deployments <- dat_names2%>%filter(longitude != 38)
dat_names3 <- dat_names%>%filter(deployment_id != 'TZA-001')
#keep values only with TZA-001 to match
library(dplyr)
dat_names_cleaned <- dat_names %>%
filter(grepl("^TZA-001", deployment_id, ignore.case = TRUE))
#mean median mode
dat_names_cleaned%>%group_by(common_name)%>%summarise(N = n())->dataset_grouped
#joins
new_data <- merge(deployments, dat_names_cleaned, by = "deployment_id")
all_data <- merge(new_data, dataset_grouped, by = "common_name")
#extracting date data
all_data%>%mutate(Date = as_date(timestamp))->full_data
#joins
dated_data <- merge(dataset_grouped, full_data, by = "common_name")
#just antelopes
library(dplyr)
antelopes_only <- dataset_grouped %>% filter(common_name %in% c("Blue Duiker", "Bushbuck", "Harvey's Duiker", "Suni"))
write.csv(antelopes_only, "antelopes_only_fr.csv", row.names = FALSE)
#only antelopes density
antelopes_filtered <-full_data %>%
filter(common_name %in% c("Blue Duiker", "Bushbuck", "Harvey's Duiker", "Suni"))
ggplot(antelopes_filtered, aes(x = N, fill = common_name, colour = common_name))+
geom_density(alpha = 0.5)+
labs(title = "Density Plot of Antelope Species in the Study",
x = "Population of Antelope",
y = "Density Observed")+
theme_minimal()
#LINE PLOTS FOR DEPLOYMENT
#NEW CODE FOR GLM INFORMATION
images <- read_csv("images.csv")
# Confirm exact species names
images %>%
count(common_name, sort = TRUE)
# Filter focal species and count detections per deployment
species_counts <- images %>%
filter(common_name %in% c(
"Harvey's Duiker",
"Suni",
"Bushbuck",
"Blue Duiker"
)) %>%
count(deployment_id, common_name, name = "occurences")
#CONTINUED FOR LINE PLOTS
#NEW CODE FOR GLM
#keep values only with TZA-001 to match
library(dplyr)
dat_names_cleaned <- species_counts %>%
filter(grepl("^TZA-001", deployment_id, ignore.case = TRUE))
#CONTINUING FOR LINE PLOTS
#NEW CODE FOR GLM
library(tidyverse)
library(lubridate)
species_wide <- dat_names_cleaned %>%
tidyr::pivot_wider(
names_from = common_name,
values_from = occurences,
values_fill = 0
) %>%
rename(
harveys_duiker = `Harvey's Duiker`,
suni = `Suni`,
bushbuck = `Bushbuck`,
blue_duiker = `Blue Duiker`
)
glimpse(species_wide)
Rows: 92
Columns: 5
$ deployment_id <chr> "TZA-001-D0005", "TZA-001-D0010", "TZA-001-D0019", "TZA-001-D0025", "TZA-001-D0031", "T…
$ harveys_duiker <int> 10, 5, 26, 9, 4, 4, 1, 0, 21, 2, 5, 8, 1, 0, 3, 0, 0, 1, 1, 2, 0, 1, 1, 1, 3, 2, 0, 0, …
$ suni <int> 4, 0, 4, 4, 0, 1, 0, 0, 1, 0, 4, 6, 0, 0, 0, 2, 0, 0, 0, 0, 1, 0, 1, 0, 2, 0, 2, 3, 1, …
$ blue_duiker <int> 0, 0, 0, 0, 0, 4, 1, 1, 0, 0, 0, 0, 1, 2, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
$ bushbuck <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, …
#CONTINUING FOR LINE PLOTS
library(tidyverse)
species_long <- species_wide %>%
pivot_longer(
cols = c(harveys_duiker, suni, bushbuck, blue_duiker),
names_to = "species",
values_to = "count"
)
glimpse(species_long)
Rows: 368
Columns: 3
$ deployment_id <chr> "TZA-001-D0005", "TZA-001-D0005", "TZA-001-D0005", "TZA-001-D0005", "TZA-001-D0010", "TZ…
$ species <chr> "harveys_duiker", "suni", "bushbuck", "blue_duiker", "harveys_duiker", "suni", "bushbuck…
$ count <int> 10, 4, 0, 0, 5, 0, 0, 0, 26, 4, 0, 0, 9, 4, 0, 0, 4, 0, 0, 0, 4, 1, 0, 4, 1, 0, 0, 1, 0,…
#CONTINUING FOR LINE PLOTS
library(tidyverse)
ggplot(
species_long,
aes(x = deployment_id, y = count, group = species, colour = species)
) +
geom_line() +
geom_point() +
facet_wrap(
~ species,
ncol = 1,
scales = "free_y",
labeller = labeller(
species = c(
harveys_duiker = "Harvey’s Duiker",
suni = "Suni",
bushbuck = "Bushbuck",
blue_duiker = "Blue Duiker"
)
)
) +
scale_colour_manual(values = c(
harveys_duiker = "dodgerblue",
suni = "purple",
bushbuck = "olivedrab",
blue_duiker = "tomato"
)) +
labs(
title = "Detections Across Deployments by Species",
y = "Number of Detections",
x = "Deployments"
) +
theme(
axis.text.x = element_blank(), # removes TZA-001 labels
axis.ticks.x = element_blank(), # removes tick marks
legend.position = "none" # legend unnecessary with facets
)
#TILE PLOTS
#ratio (antelope / per harvey)
compare_long_ratio <- species_wide %>%
transmute(
deployment_id,
Suni = (suni + 1) / (harveys_duiker + 1),
Bushbuck = (bushbuck + 1) / (harveys_duiker + 1),
`Blue Duiker`= (blue_duiker + 1) / (harveys_duiker + 1)
) %>%
pivot_longer(cols = -deployment_id, names_to = "species", values_to = "ratio")
ggplot(compare_long_ratio, aes(x = deployment_id, y = 1, fill = ratio)) +
geom_tile(color = "gray") +
facet_wrap(~ species, ncol = 1) +
scale_fill_viridis_c() +
labs(
title = "Detection Ratio Compared to Harvey's Duiker (Species/Harvey's, +1 smoothing)",
x = "Deployment",
y = NULL,
fill = "Ratio"
) +
theme(
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
axis.text.x = element_blank(),
axis.ticks.x = element_blank()
)
#CODE FOR POLYGON -> USED TO MAP STUDY AREA IN ARCGIS
#CREATING POLYGON WITH COORDINATES
library(sf)
#CONVERT POINTS TO POLYGON
points_df <- data.frame(
lon = c(36.31811, 41.15940,41.99961, 37.67042),
lat = c(-8.080697, -8.693828,-8.386151, -3.568570)
)
library(sf)
#CONVERT TO POLYGON
coords <- as.matrix(points_df)
coords_closed <- rbind(coords, coords[1, ])
polygon_geom <- st_polygon(list(coords_closed))
#CONVERT TO SF
polygon_sf <- st_sf(
geometry = st_sfc(polygon_geom),
crs = 4326
)
#PLOT IN R
plot(polygon_sf, col = "lightblue", border = "black")