library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.6
## ✔ forcats   1.0.1     ✔ stringr   1.6.0
## ✔ ggplot2   4.0.1     ✔ tibble    3.3.0
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.2
## ✔ purrr     1.2.0     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(leaflet)
library(sf)
## Linking to GEOS 3.13.1, GDAL 3.11.4, PROJ 9.7.0; sf_use_s2() is TRUE
library(rnaturalearth)

C:/Users/hp/Downloads/WHO-COVID-19-global-daily-data.csv

library(dplyr)
library(leaflet)

# Load your dataset
covid <- read.csv("C:/Users/hp/Downloads/WHO-COVID-19-global-daily-data.csv")

# Find the first reported case globally
first_case <- covid %>%
  filter(Cumulative_cases > 0) %>%
  arrange(as.Date(Date_reported)) %>%
  slice(1)

# Create a coords dataframe with the country and coordinates
# For now, we manually assign coordinates for China (first case)
coords <- data.frame(
  country = first_case$Country,
  date_reported = first_case$Date_reported,
  lat = 35.8617,   # latitude for China
  long = 104.1954  # longitude for China
)

# Create the leaflet map
leaflet(coords) %>%
  addTiles() %>%
  addCircleMarkers(
    lng = ~long,
    lat = ~lat,
    radius = 10,
    color = "red",
    fillOpacity = 0.7,
    label = ~paste("Country:", country, "<br>", "First reported case:", date_reported)
  )
coords <- data.frame(
  country = first_case$Country,
  date_reported = first_case$Date_reported,
  lat = 35.8617,
  long = 104.1954
)

leaflet(coords) %>%
  addTiles() %>%
  addCircleMarkers(
    lng = ~long,
    lat = ~lat,
    radius = 10,
    color = "red",
    label = ~paste("First reported case:", country, date_reported)
  )
country_cases <- covid %>%
  group_by(Country) %>%
  summarise(total_cases = max(Cumulative_cases, na.rm = TRUE))

world <- ne_countries(scale = "medium", returnclass = "sf")

world_cases_map <- world %>%
  left_join(country_cases, by = c("name" = "Country"))

pal <- colorBin("YlOrRd", world_cases_map$total_cases)

leaflet(world_cases_map) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~pal(total_cases),
    fillOpacity = 0.8,
    weight = 0.5,
    color = "white",
    label = ~paste(name, total_cases)
  ) %>%
  addLegend(
    pal = pal,
    values = ~total_cases,
    title = "Total Confirmed Cases"
  )
country_deaths <- covid %>%
  group_by(Country) %>%
  summarise(total_deaths = max(Cumulative_deaths, na.rm = TRUE))

max_death_country <- country_deaths %>%
  filter(total_deaths == max(total_deaths))

world_death_map <- world %>%
  left_join(country_deaths, by = c("name" = "Country")) %>%
  mutate(
    fill_col = ifelse(name == max_death_country$Country, "red", "lightgrey")
  )

leaflet(world_death_map) %>%
  addTiles() %>%
  addPolygons(
    fillColor = ~fill_col,
    fillOpacity = 0.8,
    weight = 0.5,
    color = "white",
    label = ~paste(name, total_deaths)
  )
bd_first <- covid %>%
  filter(Country == "Bangladesh", Cumulative_cases > 0) %>%
  arrange(Date_reported) %>%
  slice(1)

bd_coords <- data.frame(
  country = "Bangladesh",
  lat = 23.6850,
  long = 90.3563
)

leaflet(bd_coords) %>%
  addTiles() %>%
  addCircleMarkers(
    lng = ~long,
    lat = ~lat,
    radius = 9,
    color = "blue",
    label = paste(
      "First case in Bangladesh:",
      bd_first$Date_reported
    )
  ) %>%
  setView(90.3563, 23.6850, zoom = 6)
library(dplyr)
library(sf)
library(leaflet)
library(rnaturalearth)

# world map (sf object)
world <- ne_countries(scale = "medium", returnclass = "sf")

# Top 10 countries by total cases
top10 <- country_cases %>%
  arrange(desc(total_cases)) %>%
  slice(1:10)

# Centroid বের করা
coords_top10 <- world %>%
  filter(name %in% top10$Country) %>%
  st_centroid() %>%
  mutate(
    longitude = st_coordinates(.)[,1],
    latitude  = st_coordinates(.)[,2]
  ) %>%
  st_drop_geometry() %>%
  left_join(top10, by = c("name" = "Country"))
## Warning: st_centroid assumes attributes are constant over geometries
# Leaflet map
leaflet(coords_top10) %>%
  addTiles() %>%
  addCircleMarkers(
    lng = ~longitude,
    lat = ~latitude,
    radius = ~sqrt(total_cases) / 300,
    color = "darkred",
    fillOpacity = 0.7,
    label = ~paste(name, ": ", total_cases)
  )