美文网首页
R package:ggplot2画中国地图

R package:ggplot2画中国地图

作者: 佳名 | 来源:发表于2020-02-10 11:51 被阅读0次

1.安装包

install.packages("rgdal")
install.packages("ggplot2")

2.读取地图

2.1读取省界地图

m1 <-rgdal::readOGR("../bou2_4p.shp")

2.2读取市界地图

m2 <-rgdal::readOGR("市界_region.shp")

2.3读取县界地图

m3 <-rgdal::readOGR("县界_region.shp")
length(m1)
names(m1)
table(iconv(m1$NAME, from = "GBK"))
iconv(m1$NAME, from = "GBK")

面积(AREA),周长(PERIMETER),各种编号、中文名(NAME)等字段。
其中中文名(NAME)字段是以GBK编码的。利用iconv 格式转换函数来转换各省名称

3. 画图

library(ggplot2)
ggplot(data = m1, aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="white",col = "grey") +
  coord_map("polyconic")#指定投影方向
省界.png
ggplot(data = m2[c(101:111),], aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="white",col = "grey") +
  coord_map("polyconic")
山西.png
ggplot(data = m3[c(310:322),], aes(x = long, y = lat, group = group)) +
  geom_polygon(fill="white",col = "grey") +
  coord_map("polyconic")
吕梁.png

另一种画法

m = sf::st_read("../bou2_4p.shp")
m = sf::st_read("市界_region.shp")
m = sf::st_read("县界_region.shp")
plot(m)

相关文章

网友评论

      本文标题:R package:ggplot2画中国地图

      本文链接:https://www.haomeiwen.com/subject/cucaxhtx.html