# 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")