library(leaflet)
library(leaflet.extras)
## Warning: package 'leaflet.extras' was built under R version 4.5.2
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(ggplot2)

df <- data.frame(
  zone = c(
    "Gulshan-2 & Hatirjheel",
    "Mirpur-12",
    "Tejgaon",
    "Baunia",
    "Khilkhet"
  ),
  
  lat = c(
    23.796943,  # Gulshan-2
    23.827200,  # Mirpur-12
    23.759739,  # Tejgaon
    23.843200,  # Baunia
    23.831122   # Khilkhet
  ),
  
  lon = c(
    90.413696,  # Gulshan-2
    90.367140,  # Mirpur-12
    90.392418,  # Tejgaon
    90.387500,  # Baunia
    90.424301   # Khilkhet
  ),
  
  total_mosquito = c(30, 24, 14, 9, 15),
  tested = c(3, 3, 2, 3, 3),
  positive = c(1, 1, 1, 1, 1)
)

# Positive rate (weight for heatmap)
df <- df %>%
  mutate(rate = positive / tested)

df
leaflet(df) %>%
  addTiles() %>%
  
  # Heatmap layer
  addHeatmap(
    lng = ~lon, lat = ~lat,
    intensity = ~rate,
    blur = 25,
    radius = 25,
    max = max(df$rate)
  ) %>%
  
  # Red markers
  addCircleMarkers(
    lng = ~lon,
    lat = ~lat,
    color = "red",
    fillColor = "red",
    fillOpacity = 0.9,
    radius = ~ (6 + rate * 20),
    label = ~paste0(
      zone,
      "\nTotal Mosquito: ", total_mosquito,
      "\nTested: ", tested,
      "\nPositive: ", positive,
      "\nPositive Rate: ", round(rate, 2)
    )
  ) %>%
  
  setView(
    lng = mean(df$lon),
    lat = mean(df$lat),
    zoom = 12
  )
p <- ggplot(df, aes(x = lon, y = lat)) +
  stat_density_2d(
    aes(fill = ..level.., weight = rate),
    geom = "polygon",
    alpha = 0.6
  ) +
  scale_fill_gradient(low = "yellow", high = "red") +
  geom_point(aes(size = rate), color = "red") +
  geom_text(aes(label = zone), vjust = -1, size = 4) +
  coord_fixed() +
  labs(
    title = "Dengue-positive Aedes Mosquito Density Map (Dhaka)",
    x = "Longitude",
    y = "Latitude"
  ) +
  theme_minimal(base_size = 14)
## Warning in stat_density_2d(aes(fill = ..level.., weight = rate), geom =
## "polygon", : Ignoring unknown aesthetics: weight
print(p)
## Warning: The dot-dot notation (`..level..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(level)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The following aesthetics were dropped during statistical transformation:
## weight.
## ℹ This can happen when ggplot fails to infer the correct grouping structure in
##   the data.
## ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
##   variable into a factor?