##Title: DWVGIS 1. Bosnia.R
#Author: Jack Reilly
#Purpose: Illustrate loading a shape file and layering points for the Bosnian civil war
#Requires: ged.csv and bosnia.shp, both in a bosnia folder inside ~/desktop/working/dwv26

library(tidyverse)
library(sf)

#Set the working directory
setwd("~/desktop/working/dwv26")

#Load the event data and inspect it
events <- read.csv("bosnia/ged.csv")
summary(events)

#Declare the spatial element of the event data
events <- st_as_sf(events, coords = c("longitude", "latitude"), crs = 4326)
events

#Begin plotting it
plot(events)
plot(st_geometry(events))

#Load the shape file 
bosnia <- st_read("bosnia/bosnia.shp", crs = 4326)
bosnia

#Begin plotting
plot(bosnia)

plot(st_geometry(bosnia), col = "lightgrey")
plot(st_geometry(events), pch = 16, add = T)
plot(st_geometry(events), col = events$best, pch = 16, add = TRUE)

#Let's create a color scale
library(scales)
black_red <- gradient_n_pal(c("black", "red"))

# numeric vector to color
vals <- events$best
rng  <- range(vals, na.rm = TRUE)          # data domain
z    <- rescale(vals, to = c(0, 1), from = rng)
ptcol <- black_red(z)                      # colors for each point

# plot
plot(st_geometry(bosnia), col = "lightgrey")
plot(st_geometry(events), col = ptcol, pch = 16, add = TRUE)

#This color looks funny

library(lattice)
histogram(events$best)

#Ah.  Some large outliers there.
#Let's find the biggest events
events_sorted <- events[order(events$best), ]
tail(events_sorted$best, 10)    # 10 lowest

#OK.  First, we'll create some groups
events$outlier <- events$best %in% c(3000, 8106)
events$large <- events$best %in% c(144, 150, 197, 270, 300)

#Now, we'll break our point dataset in three: "normal", "large", and "outlier"

large_vals    <- c(144, 150, 197, 270, 300)
outlier_vals  <- c(3000, 8106)

ev_out   <- events[!is.na(events$best) & events$best %in% outlier_vals, ]
ev_large <- events[!is.na(events$best) & events$best %in% large_vals, ]
ev_reg   <- events[!is.na(events$best) &
                     !(events$best %in% c(large_vals, outlier_vals)) &
                     events$best >= 0 & events$best <= 101, ]

vals <- ev_reg$best
rng  <- range(vals, na.rm = TRUE)          # data domain
z    <- rescale(vals, to = c(0, 1), from = rng)
ptcol <- black_red(z)  

plot(st_geometry(bosnia), col = "lightgrey")
plot(st_geometry(ev_reg), col = ptcol, pch = 16, add = TRUE)

#Still dark.  what if we made the mean of the color scale the mean of the variable?
mean(ev_reg$best)
median(ev_reg$best)

z <- rescale_mid(vals, to = c(0, 1), from = rng, mid = 7)
ptcol <- black_red(z)  

plot(st_geometry(bosnia), col = "lightgrey")
plot(st_geometry(ev_reg), col = ptcol, pch = 16, add = TRUE)

z <- rescale(vals, to = c(0, 1), from = rng)
z <- z ^ 0.7  # lower exponent -> shifts color toward red earlier
ptcol <- black_red(z)

plot(st_geometry(bosnia), col = "lightgrey")
plot(st_geometry(ev_reg), col = ptcol, pch = 16, add = TRUE)

#Still a tough distribution to figure out how to color effectively, even with the larger channels taken out
histogram(ev_reg$best)

#Ok.  What about adding the others?

plot(st_geometry(bosnia), col = "lightgrey", main="Bosnian Civil War")
plot(st_geometry(ev_reg), col = ptcol, pch = 16, add = TRUE)
plot(st_geometry(ev_large), col = "maroon", pch = 15, cex=2, add = TRUE)
plot(st_geometry(ev_out[1,]), col = "maroon", pch = 17, cex=3, add = TRUE)
plot(st_geometry(ev_out[2,]), col = "red", pch = 17, cex=3, add = TRUE)



#Legend 
legend("bottomleft",
       legend = c("Small Event (0–101 casualties)",
                  "Large Event (102–300)",
                  "Major Event (3000)",
                  "Major Event (8106)"),
       pch    = c(16, 15, 17, 17),
       pt.cex = c(0.9, 1.3, 1.8, 2.2),
       col    = c("red", "#800000", "#800000", "red"),
       bty    = "o")  # <-- draw box

#Join the two datsets together to allow for counting
joined <- st_join(events, bosnia)

eventcounts <- joined |>
  as.data.frame() |>
  group_by(id.y) |>
  count(name = "num_events") 
  
eventcounts