R2-03 第九次作业 地图

微思微丝 2017-12-30 13:43:42 阅读: 1246

R语言之ggplot2画地图

任务1:

    画自己所在省份的地图,并标记城市。

install.packages("maptools")
install.packages("plyr")
setwd("E:/PNG/R2/R2-9-地图绘图")##在存放了地图数据的文件夹设置工作目录,便于调用数据。
library(maptools)
library(ggplot2)
library(plyr)
mytheme<-theme( panel.background=element_blank(),
          panel.grid=element_blank(),
          axis.text=element_blank(),
          axis.ticks=element_blank(),
          axis.title=element_blank(), legend.position = "none")
china_map<-readShapePoly("bou2_4p.shp")##载入数据并转换成该函数数据框
x<-china_map@data##经纬度等数据信息
xs<-data.frame(x,id=seq(0:924)-1)##行政区域信息
china_map1<-fortify(china_map)##转换成ggplot2可用的数据框 
china_mapdata<-join(china_map1, xs, type = "full") ##合并两组信息 
anhui<-subset(china_mapdata,NAME=="安徽省")
ggplot(anhui, aes(x = long, y = lat, group = group,fill=NAME))+
  geom_polygon(fill="yellow" )+
  geom_path(colour = "grey40")+
  ggtitle("R2-03-安徽-安庆")+
  geom_text(mapping = aes(x=117,y=33,label=NAME),color="red",cex=5)+
  geom_point(aes(x=117.17,y=31.52),colour="blue",size=3,pch=19)+
  geom_point(aes(x=117.02,y=30.31),colour="blue",size=1.8)+
  annotate("text",x=117.3,y=31.3,label="合肥")+
  annotate("text",x=117.2,y=30.1,label="安庆")+
  coord_map("polyconic")+  ##获得正常视角的地图
  mytheme

Rplot-6.png

任务2:

    画融合了人口数据的中国地图

library(maptools)
library(ggplot2)
library(plyr)
mytheme<-theme( panel.background=element_blank(),
          panel.grid=element_blank(),
          axis.text=element_blank(),
          axis.ticks=element_blank(),
          axis.title=element_blank(), legend.position = c(0.9,0.4))
china_map<-readShapePoly("bou2_4p.shp")
x<-china_map@data
xs<-data.frame(x,id=seq(0:924)-1)
china_map1<-fortify(china_map) 
china_mapdata<-join(china_map1, xs, type = "full")
data<-read.csv("E:/PNG/R2/R2-9-地图绘图/R2-9-1.csv")
mydata<-as.data.frame(data)
pop<-join(china_mapdata, mydata, type = "full")
  ggplot(pop,aes(x = long, y = lat, group = group,fill=pop))+
  geom_polygon()+ 
  coord_map("polyconic") +##获得正常视角的地图
  scale_fill_gradient(low="white",high="red")+
  geom_path(colour = "green")+
  ggtitle("R2-03")+mytheme

Rplot-3.png

 

 

 

 

 
邀请讨论

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
{{item.nick_name}} 受邀请回答 {{item.create_time}}
{{item.refer_comment.nick_name}} {{item.refer_comment.create_time}}

附件

{{f.title}} 大小 {{f.file_size}} 下载 {{f.count_download}} 金币 {{f.count_gold}}
切换到完整回复 发送回复
赞({{item.count_zan}}) 踩({{item.count_cai}}) 删除 回复 关闭
科研狗©2015-2025 科研好助手,京ICP备20005780号-1 建议意见

服务热线

178 0020 3020

微信服务号