用R画中国地图...


# install.packages("mpas")
# install.packages('mapdata')
# install.packages('ggmap')
# install.packages('plyr')
# install.packages('maptools')
# library(ggmap)

library(maps)
library(mapdata)
library(sp)

library(ggplot2)
library(plyr)
library(maptools);
library(mapproj)

setwd('d:/software/workplace')
cm=readShapePoly('bou2_4p.shp');

dat = read.csv(text = "provname,jd,wd
               北京,116.4666667,39.9
               上海,121.4833333,31.23333333
               天津,117.1833333,39.15
               重庆,106.5333333,29.53333333
               哈尔滨 ,126.6833333,45.75
               长春,125.3166667,43.86666667
               沈阳,123.4,41.83333333
               呼和浩特,111.8,40.81666667
               石家庄 ,114.4666667,38.03333333
               太原,112.5666667,37.86666667
               济南,117,36.63333333
               郑州,113.7,34.8
               西安,108.9,34.26666667
               兰州,103.8166667,36.05
               银川,106.2666667,38.33333333
               西宁,101.75,36.63333333
               乌鲁木齐,87.6,43.8
               合肥,117.3,31.85
               南京,118.8333333,32.03333333
               杭州,120.15,30.23333333
               长沙,113,28.18333333
               南昌,115.8666667,28.68333333
               武汉,114.35,30.61666667
               成都,104.0833333,30.65
               贵阳,106.7,26.58333333
               福州,119.3,26.08333333
               台北,121.5166667,25.05
               广州,113.25,23.13333333
               海口,110.3333333,20.03333333
               南宁,108.3333333,22.8
               昆明,102.6833333,25
               拉萨,91.16666667,29.66666667
               香港,114.1666667,22.3
               澳门,113.5,22.2", sep=",")

dat$provname = gsub(' ', '', dat$provname)

NAME <- c("北京市","广东省","福建省","广西壮族自治区","河北省","湖南省","浙江省","山东省","江苏省","河南省","江西省","辽宁省","香港特别行政区","四川省","湖北省","上海市","山西省","海南省","吉林省","黑龙江省","陕西省","云南省","贵州省","安徽省","内蒙古自治区","台湾省","重庆市","天津市","宁夏回族自治区","甘肃省","新疆维吾尔自治区","西藏自治区","青海省")
provname<-c("北京","广东","福建","广西","河北","湖南",  "浙江","山东","江苏","河南","江西","辽宁","香港","四川","湖北","上海","山西","海南","吉林","黑龙江","陕西","云南","贵州","安徽","内蒙古","台湾","重庆","天津","宁夏","甘肃","新疆","西藏","青海")
pop=c(1633,1115,6943,3393,2405,4298,2730,3824,1858,7625,
      5060,6118,3581,4368,9367,9360,5699,6355,9449,
      4768,845,2816,8127,3762,4514,284,3748,2617,
      552,610,2095,2296,693);
df <- data.frame(NAME, provname, pop)

xs <- data.frame(cm@data ,id=seq(0:924)-1)          #含岛屿共925个形状
china_map1 <- fortify(cm)           #转化为数据框
china_map_data <- join(china_map1, xs, type = "full") 

china_map <- join(china_map_data, df, type="full")
head(china_map)

midpos <- function(x) mean(range(x,na.rm=TRUE)) #取形状内的平均坐标
centres <- ddply(china_map,.(NAME),colwise(midpos,.(long,lat)))

p <- ggplot(china_map,aes(long,lat))+
  geom_polygon(aes(group=group,fill=pop),colour="grey60")+
  scale_fill_gradient(low="white",high="steelblue") +
  coord_map("polyconic") +
  # geom_text(aes(x = long,y = lat,label = NAME), data =centres,position = "identity")+
  geom_point(data = dat, aes(x=jd, y=wd), colour = "red") +
  geom_text(data=dat,aes( x=jd, y=wd), label = dat$provname, size =3) +
  theme(
    panel.grid = element_blank(),
    panel.background = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    axis.title = element_blank()
  )

ggsave(p, filename = 'd:/chinamap2.png', width = 16, height = 12)

merge(df, dat )


你可能感兴趣的:(用R画中国地图...)