Filling polygons of a map using GGplot in R

心不动则不痛 提交于 2019-11-28 00:22:42

Assuming the shapefile is already downloaded, you could do something like the below. It probably needs a bit of tidying up in a cosmetic sense, but as a first approximation it seems okay.

library(rgdal)
library(ggplot2)

work.dir <- "your_work_dir"
mun.neth <- readOGR(work.dir, layer = 'gem_2012_v1')

mun.neth.fort <- fortify(mun.neth, region = "AANT_INW")
mun.neth.fort$id <- as.numeric(mun.neth.fort$id)
mun.neth.fort$id <- mun.neth.fort$id/1000 # optionally change to thousands?
mun.neth.fort[mun.neth.fort$id <= 0, 'id'] <- NA # some areas not numerically valid,
                                                 # presumably water zones

ggplot(data = mun.neth.fort, aes(x = long, y = lat, fill = id, group = group)) +
    geom_polygon(colour = "black") +
    coord_equal() +
    theme()

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