We are using a map dataset containing the coordinates outlining the perimeter of every country’s territories in the world.
# import dataset
globeMap <- read.csv("output/globeMap.csv")
# show the structure of the dataframe
str(globeMap)
## 'data.frame': 104410 obs. of 13 variables:
## $ X : int 1 2 3 4 5 6 7 8 9 10 ...
## $ long : num -69.9 -69.9 -69.9 -70 -70.1 ...
## $ lat : num 12.5 12.4 12.4 12.5 12.5 ...
## $ group : int 1 1 1 1 1 1 1 1 1 1 ...
## $ order : int 1 2 3 4 5 6 7 8 9 10 ...
## $ region : chr "Aruba" "Aruba" "Aruba" "Aruba" ...
## $ subregion : chr NA NA NA NA ...
## $ Three_Letter_Country_Code: chr NA NA NA NA ...
## $ Continent_Name : chr NA NA NA NA ...
## $ Continent_Code : chr NA NA NA NA ...
## $ Country_Name : chr NA NA NA NA ...
## $ Two_Letter_Country_Code : chr NA NA NA NA ...
## $ Country_Number : int NA NA NA NA NA NA NA NA NA NA ...
# Load ggplot library
library(ggplot2)
# create ggplot object and save it in an object. The group parameter is very important because it groups all the coordinates by country
myMap <- ggplot(globeMap, aes(x=long, y=lat, group = as.factor(group)))
# add geometry
myMap <- myMap + geom_polygon(aes(fill=Continent_Name, color=Continent_Name))
# add color palettes
myMap <- myMap + scale_fill_viridis_d()
# customized color palette
myMap <- myMap + scale_colour_manual(values = c("#999999", "#E69F00", "#56B4E9", "#009E73", "#F0E442", "#0072B2", "#D55E00", "#CC79A7"))
# add labels
myMap <- myMap + labs(title = "Countries outlined by region", subtitle = "Using a mercator projection", caption = "Dataset retrieved from native ggplot map", x = "Longitude", y= "Latitude")
# add margins
myMap <- myMap + theme_bw()
# plot the map
myMap
To do so let’s use the IHDI data from 2017
# Import the dataset. IMPORTANT set na.strings to something different than "NA" because NA is used to define North America
IHDI <- read.csv2("output/Coefficient_of_human_inequality.csv", na.strings="")
# Subset the main dataset
IHDI_2017 <- subset(IHDI, IHDI$year == "X2017")
# Rename IHDI dataset column to match with map column
colnames(IHDI_2017)[5] <- "Three_Letter_Country_Code"
# Add data to the map
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
mapData <- left_join(globeMap,IHDI_2017, by = c("Three_Letter_Country_Code","Three_Letter_Country_Code"))
# Create ggplot object and save it in an object. The group parameter is very important because it groups all the coordinates by country
myMap <- ggplot(mapData, aes(x=globeMap$long, y=globeMap$lat, group = as.factor(globeMap$group)))
# add geometry
myMap <- myMap + geom_polygon(aes(fill = value))
# add color palettes
myMap <- myMap + scale_fill_viridis_c(option="magma")
# add labels
myMap <- myMap + labs(title = "World Inequality-Adjusted Development Index (IHDI) for 2017", subtitle = "Average of education, health and income inequality indexes", caption = "Grey territories have no index. Source UN", x = "Longitude", y= "Latitude")
# add margins
myMap <- myMap + theme_bw()
# plot the map
myMap