specific country map with district/cities using R

蓝咒 提交于 2021-02-17 05:33:23

问题


I am trying to draw some specific countries map such as, Bangladesh, Bhutan etc. with its district/cities in R. As an example, I can draw US map using the following lines of codes. Is there any such library/package that can give me any countries map with its cities/district/province? Any clue is appreciated.

library(maps)
states <- map_data("state")

回答1:


You can download shapefile of any country from the following website https://www.diva-gis.org/gdata Then read and plot them in R using following code

library(sf)
library(ggplot2)
library(rgdal)
library(rgeos)

#Reading the shapefiles
sf <- st_read(dsn="C:\\Users\\nn\\Desktop\\BGD_adm", layer="BGD_adm2")
shape <- readOGR(dsn="C:\\Users\\nn\\Desktop\\BGD_adm", layer="BGD_adm2")

#To view the attributes
head(shape@data)
summary(sf)

#Plotting the shapefile
plot(shape)
plot(sf)

#Plotting the districts only
plot(sf["NAME_2"], axes = TRUE, main = "Districts")

#Plotting Using ggplot2
ggplot() + 
  geom_sf(data = sf, aes(fill = NAME_2)) + theme(legend.position = "none")



来源:https://stackoverflow.com/questions/61182881/specific-country-map-with-district-cities-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!