library(ggplot2)
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
data <- data.frame(
  Year = c(2024, 2024, 2024, 2024, 2024, 2024, 2024),
  `Year.ending` = c("June", "June", "June", "June", "June", "June", "June"),
  `Local.Government.Area` = c("Alpine", "Alpine", "Alpine", "Alpine", "Ararat", "Ararat", "Ararat"),
  `Investigation.Status` = c("Arrest/Summons", "Not authorised", "Other", "Unsolved", "Arrest/Summons", "Not authorised", "Other"),
  `Offence.Count` = c(272, 18, 74, 140, 627, 78, 205)
)

data <- data %>% rename_with(~ gsub(" ", ".", .))

data <- data %>%
  mutate(
    Outcome = case_when(
      Investigation.Status == "Arrest/Summons" ~ "Positive",
      Investigation.Status %in% c("Not.authorised", "Other", "Unsolved") ~ "Negative"
    ),
    DivergeValue = ifelse(Outcome == "Positive", Offence.Count, -Offence.Count)
  )

data <- data %>% filter(!is.na(Outcome))

summed_data <- data %>%
  group_by(Local.Government.Area) %>%
  summarise(
    Positive = sum(Offence.Count[Outcome == "Positive"], na.rm = TRUE),
    Negative = sum(Offence.Count[Outcome == "Negative"], na.rm = TRUE)
  ) %>%
  mutate(
    Total = Positive + Negative
  )

ggplot() +
  geom_bar(data = summed_data,
           aes(x = -Negative, y = Local.Government.Area, fill = "Negative"),
           stat = "identity", color = "black", size = 0.5) +
  geom_bar(data = summed_data,
           aes(x = Positive, y = Local.Government.Area, fill = "Positive"),
           stat = "identity", color = "black", size = 0.5) +
  geom_text(data = summed_data,
            aes(x = -Negative - 8, y = Local.Government.Area, label = Negative),
            inherit.aes = FALSE, color = "black", hjust = 1, size = 4) +
  geom_text(data = summed_data,
            aes(x = Positive + 6, y = Local.Government.Area, label = Positive),
            inherit.aes = FALSE, color = "black", hjust = 0, size = 4) +
  geom_text(data = summed_data,
            aes(x = 0, y = Local.Government.Area, label = paste0("Total: ", Total)),
            inherit.aes = FALSE, color = "black", vjust = -1.5, size = 4) +
  scale_fill_manual(values = c("Positive" = "#F4D06F", "Negative" = "#A8DADC"),
                    labels = c("Negative outcomes (Unsolved, Not Authorised, Other)",
                               "Positive outcomes (Arrest/Summons)")) +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.3, face = "bold", size = 14, vjust = -2.5),
    plot.subtitle = element_text(hjust = 0.3, size = 12, vjust = -6.2),
    plot.caption = element_text(hjust = 0.1, size = 10),
    legend.position = "bottom",
    legend.box.margin = margin(t = 10, b = 10),
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    axis.line.x = element_line(color = "black", size = 0.8),
    axis.line.y = element_blank(),
    panel.border = element_blank()
  ) +
  labs(
    title = "Effectiveness of Investigations by Local Government Area (Year Ending June 2024)",
    subtitle = "Positive outcomes refer to Arrest/Summons. Negative outcomes include Not Authorised, Other, and Unsolved cases.",
    x = "Total Offence Count",
    y = "Local Government Area",
    caption = "Data reflects the investigation status for recorded offences as of June 2024. This chart displays the two LGAs with the highest (Ararat) and lowest (Alpine) total investigation outcomes.",
    fill = ""
  ) +
  annotate("text", x = 600, y = 1.9, label = "Ararat has the highest \npositive outcomes", hjust = 1, vjust = -0.5, size = 4, color = "black") +
  annotate("text", x = -203, y = 0.97, label = "Negative outcomes \noutweigh positives in Alpine", hjust = 0, vjust = 0, size = 4, color = "black")
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.