library(readr)
library(tidyverse)
library(dplyr)
rainbow_index <- read_csv("data/exportmap.csv") %>%
select(Country, Score)
Country Score
<chr> <dbl>
1 Albania 33
2 Andorra 35
3 Armenia 7
4 Austria 56rainbow_index %>% summary()
Country Score
Length:53 Min. : 5.00
Class :character 1st Qu.:21.00
Mode :character Median :34.00
Mean :39.29
3rd Qu.:56.75
Max. :91.00
NA's :5
library(ggplot2)
rainbow_index %>% ggplot(aes(Score)) +
geom_histogram(binwidth = 3)Very simple solution to download and map country borders
library(rworldmap)
plot(getMap())getMap give us loads of informations about countries: their names, abbreviations, the continent they’re on, their population and GDP estimates, etc.
We use ggplot here to be able to compose our layers: getMap is our base map
library(sf)
worldMap <- getMap(resolution = "low")
worldMap <- st_as_sf(worldMap)
ggplot(worldMap) + geom_sf()Our task is to find a way to merge our two datasets. We remember this helpful cheatsheet:
And it seems that the SOVEREIGNT column of our map contains full country names:
library(sf)
worldMap <- getMap(resolution = "high")
worldMap$SOVEREIGNT %>% head()
[1] Netherlands Afghanistan Angola United Kingdom Albania Finland
204 Levels: Afghanistan Albania Algeria Andorra Angola Antarctica ... ZimbabweWe can merge with our rainbow_index$Country.
# get the map again!!!
worldMap <- getMap(resolution = "low")
worldMap@data <- left_join(worldMap@data, rainbow_index,
by = c("SOVEREIGNT" = "Country"))
worldMap <- st_as_sf(worldMap)Technically, this is a map of our data…
worldMap %>%
ggplot() +
geom_sf(aes(fill = Score))We zoom in by specifying latitude and longitude minimum and maximums to coord_sf
worldMap %>%
ggplot() +
geom_sf(aes(fill = Score)) +
coord_sf(xlim = c(-10, 50), ylim = c(30, 75), expand = FALSE)worldMap %>% filter(!is.na(Score)) %>%
ggplot() +
geom_sf(aes(fill = Score)) +
coord_sf(xlim = c(-10, 50), ylim = c(30, 75), expand = FALSE)Excellent Datawrapper guide on colours
One important tool among other is ColorBrewer.
From there we can grab colours we like and feed them like so:
scale_fill_gradient(low, high)library(RColorBrewer)
worldMap %>% filter(!is.na(Score)) %>%
ggplot() +
geom_sf(aes(fill = Score,
colour = I("gray80")), size = 1/100) +
coord_sf(xlim = c(-10, 50), ylim = c(30, 75), expand = FALSE) +
scale_fill_gradient(low = I("#d7191c"), high = I("#1a9641")) +
theme_minimal()theme_opts<-list(theme(panel.grid.minor = element_blank(),
panel.grid.major = element_line(colour = "transparent"),
panel.background = element_blank(),
plot.background = element_blank(),
axis.line = element_blank(),
axis.text.x = element_blank(),
axis.text.y = element_blank(),
axis.ticks = element_blank(),
axis.title.x = element_blank(),
axis.title.y = element_blank(),
legend.position = "right"))worldMap %>% filter(!is.na(Score)) %>%
ggplot() +
geom_sf(aes(fill = Score, colour = I("gray80")), size = 1/100) +
coord_sf(xlim = c(-10, 50), ylim = c(30, 75), expand = FALSE) +
scale_fill_distiller(type = "div", palette = "RdYlGn", direction = 1) +
theme_minimal() + theme_opts +
ggtitle("LGBT rights in eastern and western Europe")







