在R中绘制省界

时间:2016-05-17 04:13:43

标签: r spatial choropleth

我想为我的报告绘制等值线图。我从GADM下载了数据。我成功地绘制了等值线图,但是当我添加边界时,发生了一些错误。出现了很多蓝线。

我的代码是:

#Read Database
HCM <- read.csv("HCM.csv")

#Load Packages
library(plyr)
library(sp)
library(ggplot2)
library(rgeos)
library(maptools)
library(scales)

#Read Map (Map downloaded from GADM)
vie_map1 <- readRDS('VNM_adm1.rds')
vie_map <- fortify(vie_map1, region = "ID_1")

#Draw the map
ggplot() +
  geom_map(data = HCM, aes(map_id = ID, fill = WeightRange), 
           map = vie_map) + expand_limits(x = vie_map$long, y = vie_map$lat)

I want this

但是当我想添加边界时,发生了一些错误(蓝色的直线)。添加边界的代码是:

  # Add the boundaries
 ggplot() +
  geom_map(data = HCM, aes(map_id = ID, fill = WeightRange), 
           map = vie_map) + expand_limits(x = vie_map$long, y = vie_map$lat)+
 geom_path() +
    geom_path(data = vie_map, aes(x = long, y = lat), color = "blue") + 
  theme_bw()

请告诉我如何修理它(删除直线)。

I got this

1 个答案:

答案 0 :(得分:0)

同时在group的美学映射中设置geom_path,不仅xy

library(ggplot2)
library(raster)
library(maptools)
vie_map1 <- getData("GADM", country="VNM", level=1)
# trying URL 'http://biogeo.ucdavis.edu/data/gadm2.8/rds/VNM_adm1.rds'
# Content type '' length 2765340 bytes (2.6 MB)
# downloaded 2.6 MB
vie_map <- fortify(vie_map1, region = "ID_1")
ggplot() +
  geom_polygon(data = vie_map, aes(x = long, y = lat, group = group), fill = "grey90") +
  geom_path(data = vie_map, aes(x = long, y = lat, group = group), color = "blue") + 
  theme_bw()