Human Wildlife Conflict

library(ggplot2)
library(viridis)
## Warning: package 'viridis' was built under R version 4.6.1
## Loading required package: viridisLite
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union

#keep only rows with usable coordinates for the mapping plots

Hwc_june <- read.csv("Hwc_june.csv", stringsAsFactors = FALSE)
Hwc_june$date <- as.Date(Hwc_june$date)
Hwc_june_geo <- Hwc_june %>% filter(!is.na(longitude) & !is.na(latitude))

cat("Total records:", nrow(Hwc_june_geo), "\n")
## Total records: 24
cat("Records with verified coordinates:", nrow(Hwc_june_geo), "\n")
## Records with verified coordinates: 24
cat("Records flagged for manual GPS check:", sum(Hwc_june_geo$coord_flag != "", na.rm = TRUE), "\n")
## Records flagged for manual GPS check: 0
theme_set(theme_minimal(base_size = 12))
# 1. POINT MAP, coloured by animal responsible, faceted by month --------

p1 <- ggplot(Hwc_june_geo, aes(x = longitude, y = latitude, color = animal_responsible)) +
  geom_point(size = 3, alpha = 0.8) +
  facet_wrap(~month) +
  scale_color_viridis_d(option = "turbo") +
  labs(title = "HWC Incident Locations by Month",
       subtitle = "Mountain Conservation Area — Nyahururu Station",
       x = "Longitude", y = "Latitude", color = "Animal") +
  theme(legend.position = "bottom")

p1         # <-- add this line so it renders in the report

ggsave("map_by_month.png", p1, width = 9, height = 6, dpi = 150)

2. POINT MAP coloured by incident type —————————-

p2 <- ggplot(Hwc_june_geo, aes(x = longitude, y = latitude, color = incident_type)) +
  geom_point(size = 3, alpha = 0.85) +
  scale_color_brewer(palette = "Set1") +
  labs(title = "HWC Incident Locations by Type",
       subtitle = "HT = Human Threat, CD = Crop Damage",
       x = "Longitude", y = "Latitude", color = "Incident Type") +
  theme(legend.position = "bottom")
p2    # <-- add this line so it renders in the report

ggsave("map_by_type.png", p2, width = 8, height = 6, dpi = 150)

3. MONTHLY INCIDENT TREND ——————————————

monthly_counts <- Hwc_june %>% count(month)
p3 <- ggplot(monthly_counts, aes(x = month, y = n)) +
  geom_col(fill = "steelblue", width = 0.5) +
  geom_text(aes(label = n), vjust = -0.4) +
  labs(title = "Monthly HWC Incident Count", x = NULL, y = "Number of Incidents")
p3  # <-- add this line so it renders in the report

ggsave("monthly_trend.png", p3, width = 6, height = 5, dpi = 150)

4. INCIDENTS BY ANIMAL RESPONSIBLE ———————————-

animal_counts <- Hwc_june %>% count(animal_responsible) %>% arrange(n)
p4 <- ggplot(animal_counts, aes(x = reorder(animal_responsible, n), y = n)) +
  geom_col(fill = "darkgreen") +
  coord_flip() +
  labs(title = "Total Incidents by Animal Responsible", x = NULL, y = "Count")
p4  # <-- add this line so it renders in the report

ggsave("by_animal.png", p4, width = 6, height = 4, dpi = 150)

5. STACKED BAR: animal x month —————————————

stack_data <- Hwc_june %>% count(month, animal_responsible)
p5 <- ggplot(stack_data, aes(x = month, y = n, fill = animal_responsible)) +
  geom_col(position = "stack") +
  scale_fill_viridis_d(option = "turbo") +
  labs(title = "HWC Incidents by Animal, per Month", x = NULL, y = "Count", fill = "Animal")
p5  # <-- add this line so it renders in the report

ggsave("stacked_animal_month.png", p5, width = 7, height = 5, dpi = 150)

6. HEATMAP: incident type intensity by county ———————–

heat_data <- Hwc_june %>% count(county, incident_type)
p6 <- ggplot(heat_data, aes(x = incident_type, y = county, fill = n)) +
  geom_tile(color = "white") +
  geom_text(aes(label = n), color = "white") +
  scale_fill_gradient(low = "lightyellow", high = "darkred") +
  labs(title = "Incident Type Intensity by County", x = "Incident Type", y = "County", fill = "Count")
p6  # <-- add this line so it renders in the report

ggsave("heatmap_county_type.png", p6, width = 6, height = 4, dpi = 150)

7. BY COUNTY bar————————————

county_counts <- Hwc_june %>% count(county) %>% arrange(n)
p7 <- ggplot(county_counts, aes(x = reorder(county, n), y = n)) +
  geom_col(fill = "sienna") +
  coord_flip() +
  labs(title = "Total Incidents by County", x = NULL, y = "Count")
p7  # <-- add this line so it renders in the report

ggsave("by_county.png", p7, width = 6, height = 4, dpi = 150)

cat("\nAll plots saved.\n")
## 
## All plots saved.