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
library(networkD3)
library(htmltools)
data <- data.frame(
Year = c(2024, 2024, 2024, 2024, 2024),
`Year ending` = c("June", "June", "June", "June", "June"),
`Local Government Area` = c("Alpine", "Alpine", "Alpine", "Alpine", "Alpine"),
`Offence Subdivision` = c("C10 Drug dealing and trafficking", "C20 Cultivate or manufacture drugs", "C30 Drug use and possession", "C30 Drug use and possession", "C30 Drug use and possession"),
`Offence Group` = c("C12 Drug trafficking", "C21 Cultivate drugs", "C31 Drug use", "C32 Drug possession", "C32 Drug possession"),
`CSA Drug Type` = c("Methylamphetamine", "Cannabis", "Prescription", "Amphetamine", "Cannabis"),
`Offence Count` = c(1, 2, 1, 1, 9)
)
data <- data %>% rename_with(~ gsub(" ", ".", .))
nodes <- data.frame(
name = unique(c(data$Offence.Subdivision, data$Offence.Group, data$CSA.Drug.Type))
)
data_links <- data %>%
group_by(Offence.Subdivision, Offence.Group, CSA.Drug.Type) %>%
summarise(Total = sum(Offence.Count), .groups = "drop") %>%
ungroup()
links_sub_to_group <- data_links %>%
group_by(Offence.Subdivision, Offence.Group) %>%
summarise(Total = sum(Total), .groups = "drop") %>%
mutate(source = match(Offence.Subdivision, nodes$name) - 1,
target = match(Offence.Group, nodes$name) - 1)
links_group_to_type <- data_links %>%
group_by(Offence.Group, CSA.Drug.Type) %>%
summarise(Total = sum(Total), .groups = "drop") %>%
mutate(source = match(Offence.Group, nodes$name) - 1,
target = match(CSA.Drug.Type, nodes$name) - 1)
links <- bind_rows(links_sub_to_group, links_group_to_type) %>%
select(source, target, Total) %>%
rename(value = Total)
sankey <- sankeyNetwork(
Links = links,
Nodes = nodes,
Source = "source",
Target = "target",
Value = "value",
NodeID = "name",
units = "Offences",
fontSize = 16,
nodeWidth = 50,
nodePadding = 15,
margin = list(top = 20, right = 100, bottom = 160, left = 100),
colourScale = JS("d3.scaleOrdinal(d3.schemeCategory10)")
)
## Links is a tbl_df. Converting to a plain data frame.
htmlwidgets::prependContent(
sankey,
tags$div(
style = "text-align:center; margin-bottom: 5px;",
tags$h2("Understanding Drug-Related Offences Across LGAs: Key Patterns and Connections (2024)"),
tags$h4("This Sankey diagram illustrates the flow of drug-related offences from subdivisions to specific drug types, highlighting dominant trends and areas for intervention."),
tags$p(
style = "margin:5px;",
"Key Insights: Cannabis possession dominates drug-related offences, while trafficking offences are primarily linked to methylamphetamine. Targeted policies could address the demand for cannabis and mitigate trafficking risks."
)
)
)
Key Insights: Cannabis possession dominates drug-related offences, while trafficking offences are primarily linked to methylamphetamine. Targeted policies could address the demand for cannabis and mitigate trafficking risks.