Last active
January 26, 2022 22:28
-
-
Save daroczig/3670f03332556e3ab03499d44c8bd5c2 to your computer and use it in GitHub Desktop.
Read and render shapefiles in R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## Download some shapefiles | |
download.file( | |
'https://biogeo.ucdavis.edu/data/diva/adm/HUN_adm.zip', | |
'Hungary_shapefile.zip') | |
unzip('Hungary_shapefile.zip') | |
## Look around what we have | |
library(rgdal) | |
ogrInfo('.') | |
## Read a shapefile from the current working directory (refer to the filename without file extension) | |
adm1 <- readOGR('.', 'HUN_adm1') | |
## Convert to data.frame so that we can pass to ggplot2 soon | |
adm1 <- fortify(adm1) | |
## Now let's load smaller administrative areas as well | |
adm2 <- fortify(readOGR('.', 'HUN_adm2')) | |
## And some points to be added to the map as well | |
cities <- fread('https://simplemaps.com/static/data/country-cities/hu/hu.csv') | |
## Party time | |
library(ggplot2) | |
ggplot() + | |
geom_path(data = adm1, | |
aes(x = long, y = lat, group = group), | |
color = 'gray', size = 1) + | |
geom_path(data = adm2, | |
aes(x = long, y = lat, group = group), | |
color = 'gray', size = .2) + | |
geom_point(data = cities, | |
aes(lng, lat, size = population)) + | |
theme_void() + | |
theme(legend.position = 'top') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment