用R画中国地图

| category RStudy  | tag ggplot2  map 

虽然只读取.shp 文件,.shx.dbf文件也必须在同一个文件目录下才能读取成功。

library(maptools)
library(ggplot2)
library(plyr)

str.crs <- "+proj=longlat +ellps=clrk66"
china_map <- readShapePoly("bou2_4p.shp",proj4string=CRS(str.crs))

x <- china_map@data                          #读取行政信息
xs <- data.frame(x,id=seq(0:924)-1)          #含岛屿共925个形状
china_map1 <- fortify(china_map)             #转化为数据框
china_map_data <- join(china_map1, xs, type = "full")       #合并两个数据框

p <- ggplot(china_map_data, aes(x = long, y = lat))
p <- p +
  theme(               
      panel.grid = element_blank(),
      panel.background = element_blank(),
      axis.text = element_blank(),
      axis.ticks = element_blank(),
      axis.title = element_blank(),
      legend.position = "none"
  )

边线设置为灰色,每个省级地区用白色填充

p + geom_polygon(aes(group=group), fill= "white", colour="grey60")

边线设置为灰色,每个省级地区用不同颜色填充

p1 <- p + geom_polygon(aes(group = group,fill=NAME),colour="grey60")
p1

添加省会级城市

capital <- readShapePoints("res1_4m.shp", proj4string = CRS(str.crs))
cap.name <- iconv(capital@data$NAME, "gbk", "utf8")
cap.coords <- coordinates(capital)
n <- length(cap.name)
pos <- rep(4, times = n)
spec.name <- c("呼和浩特", "成都", "西宁", "太原", "合肥"
               , "武汉","长沙", "银川", "南宁", "澳门")
pos[cap.name %in% spec.name] <- 2
spec.name <- c("北京", "南京", "上海", "广州")
pos[cap.name %in% spec.name] <- 3

cap_map_data <- data.frame(name=cap.name, 
                           long=as.data.frame(cap.coords)$coords.x1,
                           lat=as.data.frame(cap.coords)$coords.x2,
                           pos=pos)
p1 + geom_text(data=cap_map_data, aes(x=long, y= lat, label=name),family='STKaiti')

添加省会级城市的坐标(白色空心圆圈)

p1 + 
  geom_point(data=cap_map_data,aes(x=long, y= lat),shape=1,colour="white") +
  geom_text(data=cap_map_data,aes(x=long, y= lat,label=name),family='STKaiti')

使用ggrepel解决名字重叠问题

library(ggrepel)
p1 + geom_text_repel(data=cap_map_data,aes(x=long, y= lat,label=name),family='STKaiti')

只使用ggrepel解决香港和澳门名字重叠问题,其它城市给出名字和坐标

spec.city <- c("香港","澳门")
cap_map_data1 <- cap_map_data[cap_map_data$name %in% spec.city,]
cap_map_data2 <- cap_map_data[!cap_map_data$name %in% spec.city,]
p1 + geom_point(data=cap_map_data2,aes(x=long, y= lat),shape=1,colour="white") +
  geom_text(data=cap_map_data2,aes(x=long, y= lat,label=name),family='STKaiti')+
  geom_text_repel(data=cap_map_data1,aes(x=long, y= lat,label=name),family='STKaiti')

sessionInfo()
R version 3.2.3 (2015-12-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.11.2 (El Capitan)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] ggrepel_0.4     plyr_1.8.3      ggplot2_2.0.0   maptools_0.8-37
[5] sp_1.2-1       

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.3      lattice_0.20-33  digest_0.6.9     grid_3.2.3      
 [5] gtable_0.1.2     formatR_1.2.1    magrittr_1.5     scales_0.3.0    
 [9] evaluate_0.8     stringi_1.0-1    rmarkdown_0.9.2  labeling_0.3    
[13] tools_3.2.3      stringr_1.0.0    foreign_0.8-66   munsell_0.4.2   
[17] yaml_2.1.13      colorspace_1.2-6 htmltools_0.3    knitr_1.12      

Previous     Next