Libraries

library(tidyverse) 
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.0     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.1     ✔ tibble    3.1.8
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(tmap) 
## Warning: package 'tmap' was built under R version 4.2.3
library(tmaptools) 
## Warning: package 'tmaptools' was built under R version 4.2.3
library(leaflet) 
## Warning: package 'leaflet' was built under R version 4.2.3
library(sf) 
## Warning: package 'sf' was built under R version 4.2.3
## Linking to GEOS 3.9.3, GDAL 3.5.2, PROJ 8.2.1; sf_use_s2() is TRUE
library(leaflet.extras) 
## Warning: package 'leaflet.extras' was built under R version 4.2.3
library(dplyr) 
library(rio) 
## Warning: package 'rio' was built under R version 4.2.3
library(sp)
## Warning: package 'sp' was built under R version 4.2.3
library(urbnmapr)
library(gganimate)
## Warning: package 'gganimate' was built under R version 4.2.3
library(gifski)
## Warning: package 'gifski' was built under R version 4.2.3

Load the dataset co2_global_emissions_lab_clean

setwd("C:/Users/jakea/OneDrive/Desktop/MC 2022/DATA-110")
df <- read.csv("co2_global_emissions_lab_clean.csv")
head(df)
##   Country.Name Country.Code year      co2
## 1        Aruba          ABW 2000 26.19488
## 2        Aruba          ABW 2001 25.93402
## 3        Aruba          ABW 2002 25.67116
## 4        Aruba          ABW 2003 26.42045
## 5        Aruba          ABW 2004 26.51729
## 6        Aruba          ABW 2005 27.20071

Load world map data

world_map <- map_data("world")
head(world_map)
##        long      lat group order region subregion
## 1 -69.89912 12.45200     1     1  Aruba      <NA>
## 2 -69.89571 12.42300     1     2  Aruba      <NA>
## 3 -69.94219 12.43853     1     3  Aruba      <NA>
## 4 -70.00415 12.50049     1     4  Aruba      <NA>
## 5 -70.06612 12.54697     1     5  Aruba      <NA>
## 6 -70.05088 12.59707     1     6  Aruba      <NA>

Merge CO2 emissions data with world map data

merged_data_co <- left_join(df, world_map, by=c("Country.Name" = "region"))
## Warning in left_join(df, world_map, by = c(Country.Name = "region")): Each row in `x` is expected to match at most 1 row in `y`.
## ℹ Row 1 of `x` matches multiple rows.
## ℹ If multiple matches are expected, set `multiple = "all"` to silence this
##   warning.

Remove unnessacary subregion column

merged_data_co <- merged_data_co %>%
  select(-subregion)
head(merged_data_co)
##   Country.Name Country.Code year      co2      long      lat group order
## 1        Aruba          ABW 2000 26.19488 -69.89912 12.45200     1     1
## 2        Aruba          ABW 2000 26.19488 -69.89571 12.42300     1     2
## 3        Aruba          ABW 2000 26.19488 -69.94219 12.43853     1     3
## 4        Aruba          ABW 2000 26.19488 -70.00415 12.50049     1     4
## 5        Aruba          ABW 2000 26.19488 -70.06612 12.54697     1     5
## 6        Aruba          ABW 2000 26.19488 -70.05088 12.59707     1     6

Remove NA’s from merging in order to convert to sf

merged_data_co <- merged_data_co %>%
  filter(!is.na(Country.Name) & !is.na(Country.Code)& !is.na(year) & !is.na(year) & !is.na(long) & !is.na(lat) & !is.na(group) & !is.na(order))

In order to map it object merged_data is neither from class sf, stars, Spatial, Raster, nor SpatRaster so convert st_as_Sf

merged_sf_co <- st_as_sf(merged_data_co, coords = c("long", "lat"), crs = 4326) # crs = 4326 is (WGS84 geographic coordinate system)

Create a map using ggplot2

ggplot() +
  geom_sf(data = merged_sf_co,
          aes(fill = co2),
          color = "white", size = 0.1) +
  scale_fill_gradient(low = "white", high = "red", name = "CO2 Emissions") +
  labs(x = NULL, y = NULL, title = "World Map of CO2 Emissions")

```