options(dplyr.summarise.inform = FALSE)
# Crime Heat Map
crime_data_aggregated <- crime_data %>%
group_by(Area.Unit, Latitude, Longitude) %>%
summarise(Victimisations = sum(Victimisations))
crime_points <- crime_data_aggregated %>%
filter(!is.na(Longitude), !is.na(Latitude))
leaflet(crime_points) %>%
addTiles() %>%
addHeatmap(
lng = ~Longitude,
lat = ~Latitude,
intensity = ~Victimisations,
radius = 12
) %>%
addCircleMarkers(
lng = ~Longitude,
lat = ~Latitude,
radius = 4,
fillColor = "blue",
fillOpacity = 0.15,
stroke = FALSE,
# 1. é¼ æ ‡æ‚¬æµ®æ˜¾ç¤ºçš„æ ‡ç¾ï¼ˆç”µè„‘端)
label = ~paste("Number of crimes: ", Victimisations),
labelOptions = labelOptions(
style = list(
"font-size" = "12px",
"font-weight" = "bold",
"color" = "red",
"background-color" = "rgba(255,255,255,0.8)",
"padding" = "3px 6px",
"border-radius" = "4px"
),
direction = "auto"
),
# 2. 点击显示的弹窗(手机端和电脑端通用)
popup = ~paste("Number of crimes: ", Victimisations),
popupOptions = popupOptions(
maxWidth = 300, # 弹窗最大宽度
style = list(
"font-size" = "12px",
"font-weight" = "bold",
"color" = "red",
"background-color" = "rgba(255,255,255,0.9)",
"padding" = "3px 6px",
"border-radius" = "4px"
)
)
)
# Clock of Crime Time
ggplot(hourly_crimes, aes(x = factor(Occurrence.Hour.Of.Day),
y = crime_count, fill = crime_count)) +
geom_bar(stat = "identity", show.legend = FALSE) +
coord_polar(start = -0.15) +
scale_fill_gradient(low = "white", high = "red") +
labs(
title = "Crime Heatmap by Hour of Day in Auckland",
x = "Hour of Day",
y = "Crime Count") +
theme_minimal() +
theme(
axis.text.x = element_text(size = 12, angle = 0, hjust = 0.5, face = "bold"),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
plot.title = element_text(size = 14, hjust = 0.5))
