library(tidyverse)
library(reshape2)
library(rnaturalearth)
library(rvest)
library(countrycode)
library(ggflags)
library(ggimage)
library(ggthemes)
Overview
This map represent the NATURAL EARTH GDP ESTIMATION
, data is from the {rnaturalearth}
package - View: EPSG:5368.
The resources I’ll to use for this map are:
library(readr)
<- read_csv(here::here("day13_naturalearth/coldat/COLDAT_colonies.csv"))
COLDAT_colonies <- COLDAT_colonies #%>%head
col_df <- data.frame(col_df[1:9])
col_df %>%names col_df
Tidy data from wide to long:
<- col_df %>%
long_col pivot_longer(cols=c(2:9), names_to="colony",values_to="value")%>%
filter(!value==0)
Encoding(long_col$country) <- "latin1"
<- long_col%>%
long_col mutate(country=tolower(country))
Get natural earth world country polygons:
require(rnaturalearth)
<- ne_countries(scale = "medium", returnclass = "sf")
map
<- map %>%
map mutate(country=tolower(name_long))
%>%select(contains("iso"),country,name_long)%>%head map
<- unique(long_col$country)
country
require(countrycode)
::codelist%>%head
countrycode
$iso2c<- countrycode::countrycode(long_col$country,
long_colorigin = 'country.name',
destination = 'iso2c')
<- sp::merge(map, long_col,
col_map by.x = "iso_a2",
by.y = "iso2c",
all.x = TRUE)
<- col_map%>%
col_map separate(colony,into=c("x","colony"))%>%
mutate(colony=as.factor(colony))
<- col_map%>%select(-x) col_map
library(rvest)
<- read_html("https://developers.google.com/public-data/docs/canonical/countries_csv")
coord
<- coord %>% html_table(header = TRUE, fill = TRUE)
coord_tables
<- coord_tables[[1]]
coord
<- merge(col_map, coord, by.x= "iso_a2", by.y = "country", all.y = TRUE)
col_map
<- col_map%>%mutate(country_iso=tolower(iso_a2))
col_map
%>%select(latitude,longitude)%>%head col_map
library(RColorBrewer)
::display.brewer.all()
RColorBrewer::display.brewer.pal(9,"BrBG") RColorBrewer
%>%select(subregion,country_iso)%>%head col_map
<-col_map[which(col_map$subregion=="Central America"|col_map$subregion=="South America"),]
cs_america%>%select(country_iso)
cs_america%>%head cs_america
library(ggflags)
library(RColorBrewer)
# RColorBrewer::display.brewer.pal(9,"BrBG")
<- setNames(c(brewer.pal(name = "BrBG", n = 4),
cut_colors "darkred"),
levels(cs_america$colony_factor))
<- cs_america%>% # select(contains("colony"))
flags count(longitude,latitude,colony)
<- cs_america%>%
labels count(longitude,latitude,sovereignt,country_iso)
%>%
cs_america filter(!is.na(subregion))%>%
ggplot()+
geom_sf(aes(fill=gdp_md_est),position="identity") + # gdp_md_est
scale_fill_distiller(type = "seq",
palette = 2,
direction = -1,
values = NULL,
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "fill")+
#scale_fill_manual(values = cut_colors) +
::geom_flag(data=labels,aes(x=longitude,y=latitude,country=factor(sovereignt)),size=4) +
ggflags::geom_text_repel(data=labels,
ggrepelaes(x=longitude+1,y=latitude+1,label=factor(sovereignt)),
fill="darkorange",max.overlaps = 100,
label.padding = unit(1,"pt"))+
::theme_map()+
ggthemestheme(legend.position = c(-0.1,0.4))
library(showtext)
library(extrafont)
::font_info_google("Josefin Sans")
sysfonts#fonts()
#loadfonts()
font_add_google("Josefin Sans","josefin")
showtext_opts(dpi = 320)
showtext_auto(enable = T)
options(scipen = 999)
<- col_map %>%
naturalearth filter(!is.na(subregion))%>%
ggplot()+
geom_sf(aes(fill=gdp_md_est),color="#e3bf86",size=0.2,position="identity") + # gdp_md_est
scale_fill_distiller(type = "seq",
label = scales::unit_format(unit = "M", scale = 1e-6),
palette = 2,
direction = -1,
values = NULL,
space = "Lab",
na.value = "grey50",
guide = "colourbar",
aesthetics = "fill")+
coord_sf(crs = sf::st_crs(5368))+ # https://epsg.io/
labs(title = "NATURAL EARTH GDP ESTIMATION",
subtitle="rnaturalearth data #30DayMapChallenge",
caption = "Data: rnaturalearth - View: EPSG:5368 - Graphics: Federica Gazzelloni",
fill="GDP est.")+
::theme_map()+
ggthemestheme(text = element_text(family = "josefin",face = "bold"),
plot.title = element_text(size=25),
plot.subtitle = element_text(size=15),
plot.caption = element_text(size=12),
legend.position = c(0,0),
legend.background = element_blank(),
legend.title = element_text(vjust=1),
plot.background = element_rect(colour = "#7d9ac7",fill="#7d9ac7"),
panel.background = element_rect(colour = "#7d9ac7",fill="#7d9ac7")
)
# save final plot
::agg_png(here::here("day13_naturalearth/naturalearth2.png"),
raggres = 320, width = 12, height = 8, units = "in")
naturalearthdev.off()