# Install necessary packages if not installed
if(!require(sf)) install.packages("sf")
## Le chargement a nécessité le package : sf
## Warning: le package 'sf' a été compilé avec la version R 4.3.2
## Linking to GEOS 3.11.2, GDAL 3.7.2, PROJ 9.3.0; sf_use_s2() is TRUE
if(!require(tmap)) install.packages("tmap")
## Le chargement a nécessité le package : tmap
## Warning: le package 'tmap' a été compilé avec la version R 4.3.2
## Breaking News: tmap 3.x is retiring. Please test v4, e.g. with
## remotes::install_github('r-tmap/tmap')
if(!require(dplyr)) install.packages("dplyr")
## Le chargement a nécessité le package : dplyr
## Warning: le package 'dplyr' a été compilé avec la version R 4.3.2
## 
## Attachement du package : 'dplyr'
## Les objets suivants sont masqués depuis 'package:stats':
## 
##     filter, lag
## Les objets suivants sont masqués depuis 'package:base':
## 
##     intersect, setdiff, setequal, union
if(!require(openxlsx)) install.packages("openxlsx")
## Le chargement a nécessité le package : openxlsx
## Warning: le package 'openxlsx' a été compilé avec la version R 4.3.3
# Load required libraries
library(sf)
library(tmap)
library(dplyr)
library(openxlsx)

# Step 1: Create the data (You can skip this step if you already have your dataset loaded)
regions <- c(
  "Dakahlia", "Menia", "Damietta", "South Sinai", "Matrouh", "Kafr El-Shikh", 
  "Kalyoubia", "Behera", "North Sinai", "Suhag", "Fayoum", "Menoufia", 
  "Sharkia", "Port Said", "Red Sea", "Aswan", "Beni Suef", "Gharbia", 
  "Cairo", "Qena", "New Valley", "Ismailia", "Giza", "Alexandria", 
  "Assiut", "Luxor", "Suez"
)

# Generate a dataframe with fictitious data for each region
data <- data.frame(
  Region = regions,  # Only one row per region
  Year = rep(2023, length(regions)),
  Notif_rate = round(100 + seq(1, length(regions)) * 0.5, 2),
  Success_rate = round(85 + seq(1, length(regions)) * 0.2, 2),
  TB_Cases = 500 + seq(1, length(regions)) * 10,
  Children0_14 = 100 + seq(1, length(regions)) * 2,
  HIV_status = 450 + seq(1, length(regions)) * 5,
  HIV_pos = 50 + seq(1, length(regions))
)

# Export the data to Excel (optional)
write.xlsx(data, "TB_Fictitious_Data_Regions.xlsx")

# Step 2: Import the shapefile
shapefile_path <- "C:/Users/magas/Downloads/Nouveau dossier/egy_admbnda_adm3_capmas_20170421.shp"

# Read the shapefile
egy_shapefile <- st_read(shapefile_path)
## Reading layer `egy_admbnda_adm3_capmas_20170421' from data source 
##   `C:\Users\magas\Downloads\Nouveau dossier\egy_admbnda_adm3_capmas_20170421.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 5716 features and 22 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 24.90994 ymin: 22.11572 xmax: 35.94237 ymax: 31.62807
## Geodetic CRS:  WGS 84
# Step 3: Merge the shapefile with the data
# Assuming the region names in the shapefile are in the column 'ADM1_EN'
merged_data <- merge(egy_shapefile, data, by.x = "ADM1_EN", by.y = "Region")

# Step 4: Visualize the merged data using tmap
tmap_mode("view")  # Set tmap to interactive mode
## tmap mode set to interactive viewing
tm_shape(merged_data) + 
  tm_polygons("Notif_rate", title = "Notification Rate", palette = "Blues") +
  tm_layout(title = "TB Notification Rate by Region in Egypt")
# Change mode
tmap_mode("plot")  # Set tmap to interactive mode
## tmap mode set to plotting
#______________________________________________________________________________
# 1. Thematic Map: TB Notification Rate
tm_shape(merged_data) + 
  tm_polygons("Notif_rate", title = "TB Notification Rate", palette = "Blues", style = "quantile") +
  tm_layout(
    title = "TB Notification Rate by Region in Egypt",  # Title for the map
    legend.outside = TRUE,  # Legend outside the map
    legend.title.size = 1.0,  # Decreasing font size of the legend
    legend.text.size = 0.8,  # Adjusting the legend text size
    legend.outside.size = 0.25,  # Increase legend width for better readability
    scale = 1.5,  # Scaling of the map
    frame = TRUE,  # Frame around the map
    inner.margins = c(0.05, 0.05, 0.05, 0.05)  # Reduce inner margins for map size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +  # Scale bar at the bottom left
  tm_compass(type = "4star", size = 1, position = c("left", "bottom")) +  # Smaller compass at bottom left
  tm_credits("Source: Fictitious Data, 2023", position = c("left", "bottom"), size = 0.5, just = "center")  # Data source credit placed under the legend, smaller size
## Legend labels were too wide. The labels have been resized to 0.97, 0.97, 0.97, 0.97, 0.97. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

#_________________________________________________________________________

# 2. Thematic Map: TB Treatment Success Rate
tm_shape(merged_data) + 
  tm_polygons("Success_rate", title = "TB Treatment Success Rate", palette = "Oranges", style = "jenks") +
  tm_layout(
    title = "TB Treatment Success Rate by Region in Egypt",  # Title for the map
    legend.outside = TRUE,  # Legend outside the map
    legend.title.size = 1.0,  # Decreasing font size of the legend
    legend.text.size = 0.8,  # Adjusting the legend text size
    legend.outside.size = 0.25,  # Increase legend width for better readability
    scale = 1.5,  # Scaling of the map
    frame = TRUE,  # Frame around the map
    inner.margins = c(0.05, 0.05, 0.05, 0.05)  # Reduce inner margins for map size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +  # Scale bar at the bottom left
  tm_compass(type = "rose", size = 1, position = c("left", "bottom")) +  # Smaller compass at bottom left
  tm_credits("Source: Fictitious Data, 2023", position = c("left", "bottom"), size = 0.5, just = "center")  # Data source credit placed under the legend, smaller size

# _____________________________________________________________________________
# 3. Thematic Map: TB Cases Distribution
tm_shape(merged_data) + 
  tm_bubbles("TB_Cases", title.size = "TB Cases", palette = "Reds", style = "fixed") +
  tm_layout(
    title = "TB Cases Distribution by Region in Egypt",  # Title for the map
    legend.outside = TRUE,  # Legend outside the map
    legend.title.size = 1.0,  # Decreasing font size of the legend
    legend.text.size = 0.8,  # Adjusting the legend text size
    legend.outside.size = 0.25,  # Increase legend width for better readability
    scale = 1.5,  # Scaling of the map
    frame = TRUE,  # Frame around the map
    inner.margins = c(0.05, 0.05, 0.05, 0.05)  # Reduce inner margins for map size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +  # Scale bar at the bottom right
  tm_compass(type = "8star", size = 1, position = c("left", "bottom")) +  # Smaller compass at bottom left
  tm_credits("Source: Fictitious Data, 2023", position = c("left", "bottom"), size = 0.5, just = "center")  # Data source credit placed under the legend, smaller size
## Legend labels were too wide. Therefore, legend.text.size has been set to 1.1. Increase legend.width (argument of tm_layout) to make the legend wider and therefore the labels larger.

# __________________________________________________________________________
# 4. Thematic Map: HIV Status (Tested)
tm_shape(merged_data) + 
  tm_polygons("HIV_status", title = "HIV Status (Tested)", palette = "Purples", style = "quantile") +
  tm_layout(
    title = "HIV Status by Region in Egypt",  # Title for the map
    legend.outside = TRUE,  # Legend outside the map
    legend.title.size = 1.0,  # Decreasing font size of the legend
    legend.text.size = 0.8,  # Adjusting the legend text size
    legend.outside.size = 0.25,  # Increase legend width for better readability
    scale = 1.5,  # Scaling of the map
    frame = TRUE,  # Frame around the map
    inner.margins = c(0.05, 0.05, 0.05, 0.05)  # Reduce inner margins for map size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +  # Scale bar at the bottom left
  tm_compass(type = "rose", size = 1, position = c("left", "bottom")) +  # Smaller compass at bottom left
  tm_credits("Source: Fictitious Data, 2023", position = c("left", "bottom"), size = 0.5, just = "center")  # Data source credit placed under the legend, smaller size

#______________________________________________________________________________
# 5. Thematic Map: HIV Positive Cases
tm_shape(merged_data) + 
  tm_polygons("HIV_pos", title = "HIV Positive Cases", palette = "Oranges", style = "pretty") +
  tm_layout(
    title = "HIV Positive Cases by Region in Egypt",  # Title for the map
    legend.outside = TRUE,  # Legend outside the map
    legend.title.size = 1.0,  # Decreasing font size of the legend
    legend.text.size = 0.8,  # Adjusting the legend text size
    legend.outside.size = 0.25,  # Increase legend width for better readability
    scale = 1.5,  # Scaling of the map
    frame = TRUE,  # Frame around the map
    inner.margins = c(0.05, 0.05, 0.05, 0.05)  # Reduce inner margins for map size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +  # Scale bar at the bottom left
  tm_compass(type = "4star", size = 1, position = c("left", "bottom")) +  # Smaller compass at bottom left
  tm_credits("Source: Fictitious Data, 2023", position = c("left", "bottom"), size = 0.5, just = "center")  # Data source credit placed under the legend, smaller size

## Using 2 variables 
# 1. TB Notification Rate and HIV Positive Cases
tm_shape(merged_data) + 
  tm_polygons("Notif_rate", title = "TB Notification Rate", palette = "YlOrRd", style = "quantile") +
  tm_bubbles(size = "HIV_pos", title.size = "HIV Positive Cases", col = "darkred", scale = 0.4) +  # Reduced bubble size
  tm_layout(
    title = "TB Notification Rate and HIV Positive Cases by Region",
    legend.outside = TRUE,
    frame = TRUE,
    scale = 1.5,
    legend.text.size = 0.8,  # Reduced legend text size
    legend.title.size = 1.0  # Standard legend title size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +
  tm_compass(type = "8star", size = 0.8, position = c("left", "bottom")) +
  tm_credits("Source: Fictitious Data, 2023", position = c("right", "bottom"), size = 0.6)

# 2. TB Cases and Children Aged 0-14
tm_shape(merged_data) + 
  tm_polygons("TB_Cases", title = "TB Cases", palette = "Reds", style = "jenks") +
  tm_bubbles(size = "Children0_14", title.size = "Children (0-14)", col = "blue", scale = 0.4) +  # Reduced bubble size
  tm_layout(
    title = "TB Cases and Children Aged 0-14 by Region",
    legend.outside = TRUE,
    frame = TRUE,
    scale = 1.5,
    legend.text.size = 0.8,  # Reduced legend text size
    legend.title.size = 1.0  # Standard legend title size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +
  tm_compass(type = "4star", size = 0.8, position = c("left", "bottom")) +
  tm_credits("Source: Fictitious Data, 2023", position = c("right", "bottom"), size = 0.6)

# 3. TB Success Rate and HIV Status
tm_shape(merged_data) + 
  tm_polygons("Success_rate", title = "TB Success Rate", palette = "Greens", style = "quantile") +
  tm_bubbles(size = "HIV_status", title.size = "HIV Status (Tested)", col = "purple", scale = 0.4) +  # Reduced bubble size
  tm_layout(
    title = "TB Success Rate and HIV Status by Region",
    legend.outside = TRUE,
    frame = TRUE,
    scale = 1.5,
    legend.text.size = 0.8,  # Reduced legend text size
    legend.title.size = 1.0  # Standard legend title size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +
  tm_compass(type = "rose", size = 0.8, position = c("left", "bottom")) +
  tm_credits("Source: Fictitious Data, 2023", position = c("right", "bottom"), size = 0.6)

# 4. TB Notification Rate and TB Cases
tm_shape(merged_data) + 
  tm_polygons("Notif_rate", title = "TB Notification Rate", palette = "Blues", style = "quantile") +
  tm_bubbles(size = "TB_Cases", title.size = "TB Cases", col = "red", scale = 0.4) +  # Reduced bubble size
  tm_layout(
    title = "TB Notification Rate and TB Cases by Region",
    legend.outside = TRUE,
    frame = TRUE,
    scale = 1.5,
    legend.text.size = 0.8,  # Reduced legend text size
    legend.title.size = 1.0  # Standard legend title size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +
  tm_compass(type = "8star", size = 0.8, position = c("left", "bottom")) +
  tm_credits("Source: Fictitious Data, 2023", position = c("right", "bottom"), size = 0.6)

# 5. HIV Positive and Children Aged 0-14
tm_shape(merged_data) + 
  tm_polygons("HIV_pos", title = "HIV Positive Cases", palette = "Purples", style = "quantile") +
  tm_bubbles(size = "Children0_14", title.size = "Children (0-14)", col = "orange", scale = 0.4) +  # Reduced bubble size
  tm_layout(
    title = "HIV Positive and Children Aged 0-14 by Region",
    legend.outside = TRUE,
    frame = TRUE,
    scale = 1.5,
    legend.text.size = 0.8,  # Reduced legend text size
    legend.title.size = 1.0  # Standard legend title size
  ) +
  tm_scale_bar(position = c("left", "bottom")) +
  tm_compass(type = "rose", size = 0.8, position = c("left", "bottom")) +
  tm_credits("Source: Fictitious Data, 2023", position = c("right", "bottom"), size = 0.6)