1. Set up

Having a clean setup is the key to creating visualizations in R. First, load your working directory and any necessary packages. Clean data accordingly.

library(tidyverse)
library(ggplot2)
library(dplyr)
library(ggpubr)
library(extrafont)

load('countries.Rdata')
load('populationLifeExpectancy.Rdata')

# MERGE ASIAN COUNTRIES
countries$Region[countries$Region == "South Asia"] <- "Asia & Pacific"
countries$Region[countries$Region == "East Asia & Pacific"] <- "Asia & Pacific"

2. Create plot

countries %>%
  dplyr::mutate(Region = factor(Region, levels = c("Asia & Pacific", "Middle East & North Africa", "Sub-Saharan Africa", "Europe & Central Asia", "North America", "Latin America & Caribbean"))) %>%
  ggplot(aes(GDPperCapita, LifeExpectancy)) + 
  geom_point(aes(fill=Region, size=Population), shape=21, stroke=0.5, alpha=0.8) +
  scale_y_continuous(breaks = c(50, 55, 60, 65, 70,75, 80, 85), limits=c(50, 85)) +
  scale_x_log10(breaks = c(500, 1000, 2000, 4000, 8000, 16000, 32000, 64000,
                           128000), labels=c("$500", "1k", "2k", "4k", "8k", "16k", "32k", "64k", "128k")) +
  geom_smooth(method = "lm", colour="gray20", size=0.75, alpha=0.2) +
  stat_cor(aes(label=..rr.label..)) +
  
  ggtitle("People from wealthier countries appear to live longer, regardless of population.") +
  xlab("GDP per capita ($), adjusted for inflation") +
  ylab("Life expectancy (years)") +
  
  scale_fill_manual(values = c("Asia & Pacific" = "orangered",
                               "Europe & Central Asia" = "gold", 
                               "Middle East & North Africa" = "white",
                               "Sub-Saharan Africa" = "darkolivegreen3",
                               "Latin America & Caribbean" = "skyblue1",
                               "North America" = "skyblue4")) +
 
  theme(plot.title = element_text(lineheight = 3 , size=14)) +
  theme(plot.background = element_rect(fill = "#343D4C")) + 
  
  theme(text = element_text(size=12, family="Georgia",color="gray95")) +
  theme(axis.text = element_text(color="gray95")) +
  theme(legend.text = element_text(color="black"), legend.title = element_text(color="black")) +
  theme(legend.background = element_rect(fill="gray95")) +
  
  scale_size(range = c(0.5, 30)) +
  guides(size = FALSE) +
  theme(element_line(linetype = "dotted"))