library(ggplot2)
library(maps)
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
# 1. Create the Dataset directly in R
nipah_data <- data.frame(
  district = c("Faridpur", "Rajbari", "Naogaon", "Meherpur", "Lalmonirhat", "Kushtia", "Magura", "Manikganj"),
  lat = c(23.6071, 23.7574, 24.9132, 23.7622, 25.9123, 23.9013, 23.4855, 23.8617),
  long = c(89.8429, 89.6444, 88.7531, 88.6332, 89.4491, 89.1204, 89.4199, 90.0003),
  deaths = c(56, 31, 21, 11, 9, 14, 8, 12)
)

# 2. Get Bangladesh Map Data
bg_map <- map_data("world", region = "Bangladesh")

# 3. Plot the Map
ggplot() +
  # Draw the base map of Bangladesh
  geom_polygon(data = bg_map, aes(x = long, y = lat, group = group), 
               fill = "#f2f2f2", color = "gray") +
  # Add circles for deaths (size represents magnitude)
  geom_point(data = nipah_data, aes(x = long, y = lat, size = deaths, color = deaths), 
             alpha = 0.7) +
  # Customize colors and labels
  scale_color_gradient(low = "orange", high = "red") +
  labs(title = "Nipah Virus Deaths in Bangladesh by District",
       subtitle = "Visualizing Hotspots (2001 - 2025)",
       size = "Number of Deaths",
       color = "Intensity") +
  theme_minimal() +
  coord_fixed(1.3)

# 2. Creating the dataset within R to avoid file path errors
nipah_data <- data.frame(
  District = c("Faridpur", "Rajbari", "Naogaon", "Meherpur", "Lalmonirhat", "Kushtia", "Magura", "Manikganj", "Pabna", "Tangail", "Natore", "Bogra", "Dhaka"),
  Lat = c(23.6071, 23.7574, 24.9132, 23.7622, 25.9123, 23.9013, 23.4855, 23.8617, 24.0063, 24.2513, 24.4102, 24.8481, 23.8103),
  Long = c(89.8429, 89.6444, 88.7531, 88.6332, 89.4491, 89.1204, 89.4199, 90.0003, 89.2493, 89.9231, 88.9815, 89.3730, 90.4125),
  Cases = c(82, 45, 31, 18, 14, 21, 10, 15, 25, 12, 19, 11, 20),
  Deaths = c(62, 34, 24, 13, 11, 16, 8, 12, 20, 10, 15, 9, 15)
)

# 3. Get Bangladesh Boundary Data
bg_map <- map_data("world", region = "Bangladesh")
ggplot() +
  # Draw the background map
  geom_polygon(data = bg_map, aes(x = long, y = lat, group = group), 
               fill = "#f0f0f0", color = "gray") +
  # Use geom_point for the circles
  geom_point(data = nipah_data, 
             aes(x = Long, y = Lat, size = Cases, color = Deaths), 
             alpha = 0.8) +
  # Customize size and colors
  scale_size_continuous(range = c(4, 15)) +
  scale_color_gradient(low = "orange", high = "red") +
  # Fix aspect ratio to make it look like Bangladesh
  coord_fixed(1.3) +
  theme_minimal() +
  labs(title = "Point Map: Nipah Cases and Deaths",
       subtitle = "Size = Total Cases, Color = Fatality Count")

ggplot() +
  # Draw the background map
  geom_polygon(data = bg_map, aes(x = long, y = lat, group = group), 
               fill = "black", color = "#222222") +
  # Heat map layer (Density)
  stat_density_2d(data = nipah_data, 
                  aes(x = Long, y = Lat, fill = after_stat(level)), 
                  geom = "polygon", alpha = 0.5) +
  # Color scale for the "heat"
  scale_fill_gradient(low = "blue", high = "yellow") +
  # Overlay individual points
  geom_point(data = nipah_data, aes(x = Long, y = Lat), 
             color = "white", size = 0.8) +
  coord_fixed(1.3) +
  theme_void() +
  labs(title = "Heat Map: Nipah Virus Intensity Zones",
       fill = "Intensity")

# 1. Create the Historical Dataset
niv_history <- data.frame(
  Year = c(1998, 1999, 2001, 2001, 2007, 2014, 2018, 2023, 2025),
  Country = c("Malaysia", "Singapore", "India", "Bangladesh", "India", "Philippines", "India", "Bangladesh", "Bangladesh"),
  Lat = c(4.59, 1.35, 26.72, 23.76, 23.47, 6.52, 11.25, 23.68, 24.91),
  Long = c(101.09, 103.81, 88.39, 88.63, 88.55, 124.60, 75.78, 90.35, 88.75),
  Deaths = c(105, 1, 45, 9, 5, 9, 21, 10, 4)
)

# 2. Get World Map Data (Focusing on Asia)
world <- map_data("world")

# 3. Visualization: Geographic Spread Timeline
ggplot() +
  geom_polygon(data = world, aes(x = long, y = lat, group = group), 
               fill = "lightgray", color = "white") +
  # Draw bubbles colored by the year they appeared
  geom_point(data = niv_history, aes(x = Long, y = Lat, size = Deaths, color = Year), 
             alpha = 0.8) +
  # Zoom into the South/Southeast Asia region
  coord_fixed(xlim = c(60, 130), ylim = c(-10, 40), ratio = 1.3) +
  scale_color_gradient(low = "blue", high = "red") +
  theme_minimal() +
  labs(title = "Nipah Virus: The Journey from 1998 to 2025",
       subtitle = "Blue indicates early outbreaks (Malaysia); Red indicates recent outbreaks (South Asia)",
       size = "Total Deaths",
       color = "Year of Outbreak")

library(leaflet)
# 1. Prepare the Data
niv_data <- data.frame(
  District = c("Malaysia (Perak)", "Singapore", "Siliguri (India)", "Meherpur (BD)", "Kozhikode (India)", "Faridpur (BD)"),
  Year = c(1998, 1999, 2001, 2001, 2018, 2023),
  Lat = c(4.59, 1.35, 26.72, 23.76, 11.25, 23.60),
  Long = c(101.09, 103.81, 88.39, 88.63, 75.78, 89.84),
  Cases = c(265, 11, 66, 13, 23, 82),
  Deaths = c(105, 1, 45, 9, 21, 62)
)

# Calculate Fatality Rate for the details
niv_data$Fatality <- round((niv_data$Deaths / niv_data$Cases) * 100, 1)

# 2. Create the Interactive Details String
# This is what appears when you click the point
niv_data$details <- paste0(
  "<b>District/Region:</b> ", niv_data$District, "<br>",
  "<b>Year:</b> ", niv_data$Year, "<br>",
  "<b>Total Cases:</b> ", niv_data$Cases, "<br>",
  "<b>Total Deaths:</b> ", niv_data$Deaths, "<br>",
  "<b>Fatality Rate:</b> ", niv_data$Fatality, "%"
)

# 3. Build the Leaflet Map
leaflet(niv_data) %>%
  addTiles() %>%  # Adds the standard street map background
  addCircleMarkers(
    lng = ~Long, lat = ~Lat,
    radius = ~sqrt(Cases) * 2, # Size based on cases
    color = "red",
    stroke = FALSE, fillOpacity = 0.6,
    popup = ~details  # THIS creates the click interaction
  ) %>%
  addControl("Click on the red circles to see Nipah outbreak details", position = "topright")
# 2. Expanded Dataset
nipah_global_data <- data.frame(
  Location = c("Perak, Malaysia", "Singapore", "Siliguri, India", "Meherpur, BD", 
               "Kozhikode, India", "Faridpur, BD", "Naogaon, BD", "Bhola, BD"),
  Year = c(1998, 1999, 2001, 2001, 2018, 2023, 2025, 2025),
  Lat = c(4.59, 1.35, 26.72, 23.76, 11.25, 23.60, 24.91, 22.68),
  Long = c(101.09, 103.81, 88.39, 88.63, 75.78, 89.84, 88.75, 90.65),
  Cases = c(265, 11, 66, 13, 23, 14, 1, 1),
  Deaths = c(105, 1, 45, 9, 21, 10, 1, 1),
  Status = c("Resolved", "Resolved", "Sporadic", "Annual", "Sporadic", "High Risk", "Recent-2025", "Recent-2025")
)

# 3. Create Custom Details Label
nipah_global_data$info <- paste0(
  "<div style='font-family: Arial; padding: 5px;'>",
  "<b>Location:</b> ", nipah_global_data$Location, "<br>",
  "<b>Year:</b> ", nipah_global_data$Year, "<br>",
  "<b>Fatality:</b> ", round((nipah_global_data$Deaths/nipah_global_data$Cases)*100, 1), "%<br>",
  "<b>Status:</b> ", nipah_global_data$Status, "</div>"
)

# 4. Interactive Map with Markers
leaflet(nipah_global_data) %>%
  addProviderTiles(providers$CartoDB.Positron) %>%
  addCircleMarkers(
    lng = ~Long, lat = ~Lat,
    radius = ~sqrt(Cases) * 1.5, 
    color = ~ifelse(Year == 2025, "darkred", "red"),
    stroke = TRUE, weight = 2, fillOpacity = 0.7,
    popup = ~info,
    label = ~paste(Location, "(", Year, ")")
  ) %>%
  addLegend("bottomright", colors = c("red", "darkred"), 
            labels = c("Historical", "2025 Outbreaks"), title = "Outbreak Timing")