Data from https://worldpopulationreview.com/country-rankings/median-income-by-country Import the data and libraries. Clean country names as necessary.
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.0.5
##
## 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
library(sf)
## Warning: package 'sf' was built under R version 4.0.5
## Linking to GEOS 3.9.1, GDAL 3.2.1, PROJ 7.2.1; sf_use_s2() is TRUE
library(plotly)
## Warning: package 'plotly' was built under R version 4.0.5
## Loading required package: ggplot2
## Warning: package 'ggplot2' was built under R version 4.0.5
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(ggplot2)
library(viridis)
## Warning: package 'viridis' was built under R version 4.0.4
## Loading required package: viridisLite
## Warning: package 'viridisLite' was built under R version 4.0.4
library(rworldmap)
## Warning: package 'rworldmap' was built under R version 4.0.5
## Loading required package: sp
## Warning: package 'sp' was built under R version 4.0.5
## ### Welcome to rworldmap ###
## For a short introduction type : vignette('rworldmap')
income <- read.csv("C:/Users/ericp/Downloads/csvData.csv")
colnames(income) <- c("country", "medianIncome", "meanIncome", "gdpPerCapita", "pop2022")
income$country[which(income$country == 'United States')] <- 'United States of America'
income$country[which(income$country == 'DR Congo')] <- 'Democratic Republic of the Congo'
income$country[which(income$country == 'Serbia')] <- 'Republic of Serbia'
head(income)
## country medianIncome meanIncome gdpPerCapita pop2022
## 1 Luxembourg 26321 31376 124590 647.599
## 2 United Arab Emirates 24292 27017 70089 9441.129
## 3 Norway 22684 25272 70005 5434.319
## 4 Switzerland 21490 25787 72376 8740.472
## 5 United States of America 19306 25332 65297 338289.857
## 6 Canada 18652 22042 51668 38454.327
Let’s look at the GDP per capita around the world.
world_sf <- st_as_sf(getMap(resolution = "low"))%>%
left_join(income,by = c("GEOUNIT" = "country"))
map <- ggplot() +
geom_sf(data = world_sf, mapping = aes(fill = gdpPerCapita, text=sprintf("Country: %s<br>GDP Per Capita: %s", GEOUNIT, gdpPerCapita)), color = "white", size = 0.1) +
scale_fill_viridis(option='magma', name = "GDP Per Capita") +
labs(title = "GDP Per Capita Around the World")
## Warning: Ignoring unknown aesthetics: text
ggplotly(map, tooltip = "text")
There looks to be trends by continent. Most of Africa had GDP less than $10,000, most of South America and Asia $10,000-$20,000, and most of North America, Australia and Europe greater than $20,000.