0
votes

I am working on a ggplot + geom_sf map in which I display cities and additionally, I want to illustrate that the cities belong to specific regions. In short, I want to draw circles (comparable to geom_encircle()) that put together the cities to a region (e.g. "West"). enter image description here enter image description here

Unfortunately, I do not end up in the desired result with geom_encircle(). Do you have ideas/hints on how I could proceed?

The underlying map data is a sf object (geometry type: Multipolygon, dimension XY), the cities data is a sf object (geometry type: point, dimension XY) including a column called 'region' that shall define, which cities shall be encircled as a group.

The map data (Germany) is extracted from https://opendata.arcgis.com/datasets/2842f834961b4702a179bdcb08f7b6c9_0.zip?outSR=%7B%22latestWkid%22%3A3857%2C%22wkid%22%3A102100%7D where the shapefile "Bundesländer 2018 mit Einwohnerzahl" from ESRI Deutschland can be downloaded. The transformation was made with the code: Deutschland <- st_read("xn--Bundeslnder_2018_mit_Einwohnerzahl-h4c", "LAN_ew_18")

The cities data are from Google Maps (longitude and latitude), expanded by the "Region" entry (sorry, I could not upload the csv file, therefore please see the csv Screenshot). enter image description here Processing in R was done as follows:

Cities <- read.csv("Cities_regions.csv")
Cities_sf <- st_as_sf(Cities, coords = c("Long", "Lat"), crs = 4326)
Cities_crs <- st_transform(Cities_sf, crs = crs(Deutschland, asText=TRUE))

Plotting the map and the city data took place like that:

ggplot(Deutschland) + 
geom_sf(fill="#D9D9D9", col="#A6A6A6", lwd=0.1) + 
geom_sf(data = Cities_crs, size = 1, col = "black") + 
coord_sf()

Here is now where I am stuck since geom_encircle requires X and Y aesthetics that I could not specify correctly so far. Do you have a hint?

Best regards, Christoph

1

1 Answers

1
votes

I'm not 100% sure what's the expected output but maybe the following could work:

# packages
library(sf)
#> Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1
library(ggplot2)
library(ggalt)

# geodata
deutschland <- st_read("C:/Users/Utente/Downloads/xn--Bundeslnder_2018_mit_Einwohnerzahl-h4c/LAN_ew_18.shp") %>% 
  st_transform(crs = 4326)

# city data
cities <- data.frame(
  standort = c("koln", "bielefeld", "dusseldorf", "karlsruhe", "frankfurt", "munchen", "hannover"), 
  lat = c(50.9578353, 52.0149397, 51.2385861, 49.0159405, 50.1213479, 48.155004, 52.3797505), 
  lon = c(6.8272405, 8.3805278, 6.6742684, 8.3394944, 8.4964819, 11.4717963, 9.6914321), 
  region = c("W", "W", "W", "S", "S", "S", "N"), 
  stringsAsFactors = FALSE
)

# plot
ggplot(cities, aes(x = lon, y = lat)) + 
  geom_sf(data = deutschland, fill="#D9D9D9", col="#A6A6A6", lwd = 0.1, inherit.aes = FALSE) + 
  geom_point(size = 1, col = "black") + 
  geom_encircle(data = cities, aes(group = region, col = region))

Created on 2020-10-27 by the reprex package (v0.3.0)