library(tidyverse)
library(sf)
library(rvest)
library(stringr)
library(scales)
df.oil <- read_html("https://en.wikipedia.org/wiki/List_of_countries_by_oil_production") %>%
html_nodes("table") %>%
.[[1]] %>%
html_table()
colnames(df.oil) <- c('rank', 'country', 'oil_bbl_per_day')
df.oil <- df.oil %>% mutate(rank = as.integer(rank))
df.oil <- df.oil %>%
mutate(oil_bbl_per_day = str_replace_all(oil_bbl_per_day,',','')
%>% as.integer())
df.oil <- df.oil %>%
mutate(opec_ind = if_else(str_detect(country, 'OPEC'), 1, 0))
df.oil <- df.oil %>%
mutate(country = country %>% str_replace(' \\(OPEC\\)', '') %>% str_replace('\\s{2,}',' '))
df.oil <- df.oil %>%
select(rank, country, opec_ind, oil_bbl_per_day)
map.world <- map_data('world')
anti_join(df.oil, map.world, by = c('country' = 'region'))
## rank country opec_ind oil_bbl_per_day
## 1 3 United States 0 8875817
## 2 20 United Kingdom 0 939760
## 3 30 Congo, Republic of the 0 308363
## 4 34 Sudan and South Sudan 0 255000
## 5 47 Trinidad and Tobago 0 60090
## 6 67 Congo, Democratic Republic of the 0 20000
map.oil <- left_join( map.world, df.oil, by = c('region' = 'country'))
ggplot(map.oil, aes( x = long, y = lat, group = group )) +
geom_polygon(aes(fill = oil_bbl_per_day))

# PLOT with red highlight around OPEC countries
ggplot(map.oil, aes( x = long, y = lat, group = group )) +
geom_polygon(aes(color = as.factor(opec_ind))) +
scale_color_manual(values = c('1' = 'red', '0' = NA))

ggplot(map.oil, aes( x = long, y = lat, group = group )) +
geom_polygon(aes(color = as.factor(opec_ind), fill = oil_bbl_per_day)) +
scale_color_manual(values = c('1' = 'red', '0' = NA))

ggplot(map.oil, aes( x = long, y = lat, group = group )) +
geom_polygon(aes(fill = oil_bbl_per_day, color = as.factor(opec_ind))) +
scale_fill_gradientn(colours = c('#461863','#404E88','#2A8A8C','#7FD157','#F9E53F')
,values = scales::rescale(c(100,96581,822675,3190373,10000000))
,labels = comma
,breaks = c(100,96581,822675,3190373,10000000)
) +
guides(fill = guide_legend(reverse = T)) +
labs(fill = 'Barrels per day\n2016'
,color = 'OPEC Countries'
,title = 'OPEC countries produce roughly 44% of world oil'
,x = NULL
,y = NULL) +
theme(text = element_text(family = 'Gill Sans', color = '#EEEEEE')
,plot.title = element_text(size = 28)
,plot.subtitle = element_text(size = 14)
,axis.ticks = element_blank()
,axis.text = element_blank()
,panel.grid = element_blank()
,panel.background = element_rect(fill = '#333333')
,plot.background = element_rect(fill = '#333333')
,legend.position = c(.18,.36)
,legend.background = element_blank()
,legend.key = element_blank()
) +
annotate(geom = 'text'
,label = 'Source: U.S. Energy Information Administration\nhttps://en.wikipedia.org/wiki/List_of_countries_by_oil_production\nhttps://en.wikipedia.org/wiki/OPEC'
,x = 18, y = -55
,size = 3
,family = 'Gill Sans'
,color = '#CCCCCC'
,hjust = 'left'
) +
scale_color_manual(values = c('1' = 'orange', '0' = NA), labels = c('1' = 'OPEC'), breaks = c('1'))
