EUvaccine Map

EU_vaccine map

SECTION 1: SETUP AND LIBRARIES

library(ggplot2)     #for creating graphs and maps
Warning: package 'ggplot2' was built under R version 4.5.2
library(tidyverse)   #for data manipulation (filtering, joining, etc.)
Warning: package 'tidyverse' was built under R version 4.5.2
Warning: package 'tibble' was built under R version 4.5.2
Warning: package 'tidyr' was built under R version 4.5.2
Warning: package 'readr' was built under R version 4.5.2
Warning: package 'purrr' was built under R version 4.5.2
Warning: package 'dplyr' was built under R version 4.5.2
Warning: package 'stringr' was built under R version 4.5.2
Warning: package 'forcats' was built under R version 4.5.2
Warning: package 'lubridate' was built under R version 4.5.2
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.6
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ lubridate 1.9.4     ✔ tibble    3.3.0
✔ purrr     1.2.0     ✔ tidyr     1.3.2
── 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

SECTION 2: IMPORTING DATA

EUvax <- read.csv("C:/Users/Administrator/Downloads/EU Vaccine map project with R/EUvaccine.csv") #####this reads in the data file I made you will need to change the path to your computer
View(EUvax)

SECTION 3: PREPARING MAP DATA

# Retrieve standard world map coordinates (latitude/longitude polygons) provided by ggplot2
mapdata <- map_data("world")
View(mapdata)                 # Inspect the raw map data

# Join the world map data with your vaccination data
# It matches rows based on the "region" column (Country names)
# 'left_join' keeps all map rows and adds vaccination data where names match
mapdata <- left_join(mapdata, EUvax, by="region")
View(mapdata)                 # Inspect the combined data

SECTION 4: FILTERING

# Filter the combined dataset to keep ONLY rows where 'Perc_vaccinated' is NOT NA (not missing)
# This effectively removes all countries that were not in your 'EUvax' file,
# allowing the map to focus only on the relevant EU countries.
mapdata1 <- mapdata %>% filter(!is.na(mapdata$Perc_vaccinated))
View(mapdata1)

SECTION 5: CREATING THE BASE MAP

# Initialize the plot using the filtered data
# x = longitude, y = latitude. 'group = group' tells R which points belong to which country polygon.
map1 <- ggplot(mapdata1, aes(x = long, y = lat, group = group)) +
  # Draw the country shapes. Fill the color based on vaccination %; draw borders in black.
  geom_polygon(aes(fill = Perc_vaccinated), color = "black")

map1 # Display the basic map

SECTION 6: STYLING THE MAP

# Create a new map object 'map2' based on 'map1' with better styling
map2 <- map1 +
  # Create a custom color gradient: Low % = steelblue, High % = blue4
  scale_fill_gradient(name = "% vaccinated", low = "steelblue", high = "blue4", na.value = "grey50") +
  # The theme() function removes the background grid, axis labels (lat/long numbers),
  # and tick marks to make it look like a clean map rather than a graph.
  theme(axis.text.x = element_blank(),
        axis.text.y = element_blank(),
        axis.ticks = element_blank(),
        axis.title.y = element_blank(),
        axis.title.x = element_blank(),
        rect = element_blank())

map2 # Display the styled map

SECTION 7: ADDING IMAGE OVERLAY

library(cowplot) # Load cowplot library to combine plots and images
Warning: package 'cowplot' was built under R version 4.5.2

Attaching package: 'cowplot'
The following object is masked from 'package:lubridate':

    stamp
library(lubridate)

# ggdraw() initializes a drawing canvas
ggdraw() +
  # Draw the external image (bill.jpg) onto the canvas
  # x and y control position (0-1 scale), scale controls size
  draw_image("C:/Users/Administrator/Downloads/EU Vaccine map project with R/bill.jpg",  x = 0.35, y = 0.3, scale = .2) +
  # Draw the map (map2) on top or alongside the image
  draw_plot(map2)