I want to add an actual map of Europe in the background of the graph on the left:
The code for the original graph is taken from here (producing this graph at "03/08/2020 European Power") and there is a slightly shorter version with my suggested solution that does not work as intended at the end of this post. In short, my suggestion is this:
Step 1: Get the image
img_path <- "https://upload.wikimedia.org/wikipedia/commons/thumb/6/66/Blank_map_of_Europe_cropped.svg/1000px-Blank_map_of_Europe_cropped.svg.png"
img <- readPNG(img_path)
g <- rasterGrob(img, interpolate = FALSE)
Step 2: Add image to ggplot-background as suggested here
annotation_custom(g, -Inf, Inf, -Inf, Inf)
But it does not do the job. Anybody know a solution to this?
EDIT: For clarity, once the map is there, I don't want to further adjust the grid so that each group of bars exactly matches the location of a country. I really only want to replace the dark background on the left with the map on the right.
Full example code
pacman::p_load(tidyverse, tidytuesdayR, geofacet, grid, png)
## import image
img_path <- "https://upload.wikimedia.org/wikipedia/commons/thumb/6/66/Blank_map_of_Europe_cropped.svg/1000px-Blank_map_of_Europe_cropped.svg.png"
img <- readPNG(img_path)
g <- rasterGrob(img, interpolate = FALSE)
## Load in data
tuesdata <- tidytuesdayR::tt_load('2020-08-04')
## Match countries in data w/ grid, do some pivoting
energy_raw <- tuesdata$energy_types %>%
mutate(country_name = case_when(country_name == "North Macedonia" ~ "N. Macedonia",
country_name == "Bosnia & Herzegovina" ~ "Bosnia & H.",
country == "UK" ~ "UK",
country == "EL" ~ "Greece",
TRUE ~ country_name)) %>%
filter(country_name %in% europe_countries_grid2$name) %>%
pivot_longer(cols = c(`2016`,`2017`,`2018`), names_to = "year") %>%
pivot_wider(names_from = type, values_from = value) %>%
select(-country)
## Work out percentages of the total and tidy the data
energy_plot <- left_join(energy_raw %>% filter(level == "Level 1") %>% select(-level) %>% janitor::remove_empty("cols"),
energy_raw %>% filter(level == "Level 2") %>% select(-level) %>% janitor::remove_empty("cols")) %>%
janitor::clean_names() %>%
rowwise() %>%
mutate(tot = sum(conventional_thermal, nuclear, hydro, wind, solar, geothermal, other, pumped_hydro_power),
across(where(is.numeric), ~ 100 * . / tot)) %>%
select(-tot) %>%
ungroup() %>%
pivot_longer(names_to = "energy", values_to = "value",
c(conventional_thermal, nuclear, hydro, wind, solar, geothermal, other, pumped_hydro_power)) %>%
rbind(expand.grid(country_name = setdiff(europe_countries_grid2$name, energy_raw$country_name),
year = 2016:2018, energy = "No Data Available", value = 100)) %>%
mutate(energy = case_when(energy == "conventional_thermal" ~ "Conventional Thermal",
energy == "nuclear" ~ "Nuclear",
energy == "No Data Available" ~ energy,
TRUE ~ "Renewable"),
energy = factor(energy,
levels = c("Conventional Thermal", "Nuclear",
"Renewable", "No Data Available"))) %>%
group_by(energy, year, country_name) %>%
summarise(value = sum(value)) %>% mutate(country_name = str_trunc(country_name, 11))
## Plot!
ggplot(energy_plot) +
# background image
annotation_custom(g, -Inf, Inf, -Inf, Inf) +
# The actual proper plotting stuff here isn't actually that complicated!
aes(x = year, y = value, fill = energy) +
geom_col(color = "#30332E") +
coord_flip() +
scale_y_reverse() +
# facet_geo from the "geofacet" package - I truncate the names here just to be sure
facet_geo( ~ country_name,
grid = europe_countries_grid2 %>% mutate(name = str_trunc(name, 11))) +
# colours and themes
scale_fill_manual(values = c("#EF476F", "#FFD166", "#06D6A0", "#4B5446")) +
theme_minimal() +
theme(
panel.grid = element_blank(),
axis.text.x = element_blank(),
plot.margin = unit(c(1, 1, 1.5, 1.2), "cm"),
legend.position = c(0.075, .125)
)