Home > Software design >  Overlay geofacet on basemap
Overlay geofacet on basemap

Time:03-10

I'm trying to make a simple faceted plot of the EU, but overlaying a basemap of the EU. I'm just wondering if it's possible to accomplish this?

Right now, my code is as follows, which produces the following:

mydf %>%
  ggplot(aes(x = x, y = y, group = name))   
  geom_line()   
  coord_polar()   
  facet_geo(~country, grid = "eu_grid1")

enter image description here

I am able to easily produce a map of Europe using the following:

world <- map_data("world")

world %>%
  filter(region %in% eu_grid1$name) -> europe

ggplot()   
  geom_polygon(data=europe, aes(x=long, y=lat, group=group), color="black", fill="white")  

But cannot figure out how to sync (if possible) and layer them. Thank you!

CodePudding user response:

Yes, this is possible. I'm not sure how well it works visually though. Obviously I don't have your data, but I'll use a built in dataset from geofacet to demonstrate.

library(geofacet)
library(grid)
library(tidyverse)

facets <- eu_gdp %>%
  ggplot(aes(x = year, y = gdp_pc))   
  geom_line()   
  facet_geo(~name, grid = "eu_grid1")  
  theme(plot.background = element_blank())

map_data("world") %>%
  filter(region %in% eu_grid1$name) %>%
  ggplot()   
  geom_polygon(aes(x=long, y=lat, group=group), 
               color="black", fill="white")  
  theme_void()

print(facets, newpage = FALSE)

Created on 2022-03-09 by the reprex package (v2.0.1)

  • Related