分享

用R做中国地图并实现地图编注---制作一幅自己的旅游足迹地图

 TS的美梦 2022-07-25 发布于重庆

很早以前,吸引我做生信的其实是一篇文章的地图,在得知是R语言做的,还能够对某些地名定位后,对我很有吸引力。

此外,之前在西部旅行时,很想做一个关于自己足迹的地图,当时使用的AI标注的,这里使用R实现一下,形成一幅自己的足迹地图。

最后,很多研究涉及地域,能够有一副地图展示,效果分拉满!

(2018)

(2022)

加载需要的R包。

library(mapdata)library(maptools)library(plyr)library(mapproj)install.packages('rgdal')library(rgdal)library(ggplot2)

这里我们展示中国地图的绘制,地图信息文件已上传群文件。(一定要注意,地图信息不能错,一点也不能少,此外有些老的地图文件也不能用)

setwd('/cloud/project')china=rgdal::readOGR("bou2_4p.shp")china <- fortify(china)

ggplot2作图即可。

ggplot(china,aes(x=long,y=lat,group=group))+  geom_polygon(fill="white",colour="black")+  coord_map("gilbert")+  theme(    panel.grid=element_blank(),    panel.background=element_blank(),    axis.text=element_blank(),    axis.ticks=element_blank(),    axis.title=element_blank()  )

上面提到的mapdata这个包,包含很多地图信息,例如做一下世界地图。但是这个包里面的中国地图信息却很老,所以这里不展示了。

world_map <- map_data("world")ggplot(world_map, aes(x = long, y = lat, group = group)) +  geom_polygon(fill="white", colour = "black")+  theme(panel.grid=element_blank(),    panel.background=element_blank(),    axis.text=element_blank(),    axis.ticks=element_blank(),    axis.title=element_blank())

画出地图是我们的第一步,因为都是ggplot2下的作图,所以我们可以做很多事情,做一些有意思的图。最近不论是疫情地图还是高温地图,其实都是热图的形式出现,重点是数据结合。这里我们示范一个人口小例子(数据有误差)。

china=rgdal::readOGR("bou2_4p.shp")A <- china@dataC <- data.frame(A, id=rownames(A))china_map_frame <- fortify(china)
chinamap<-join(C,china_map_frame,type="full")

people <- read.csv("people.csv",header = T, as.is=T)china_people_data <- join(chinamap, people, type="full")

ggplot(china_people_data,aes(x=long,y=lat, group=group, fill=Ratio))+ geom_polygon(colour="black")+ scale_fill_gradient(low="white", high="#EE2627")+ coord_map("gilbert")+ theme( panel.grid=element_blank(), panel.background=element_blank(), axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank(), legend.position=c(0.8,0.5) )

接下来,还是来实现我的旅游足迹地图吧。先做完整地图,再局部放大,其实就是调整坐标轴范围。还有另外一种思路,那就是提取相应位置范围经纬度文件作图,效果是一样的。

#局部放大china_all <- ggplot(china,aes(x=long,y=lat,group=group))+  geom_polygon(fill="white",colour="black")+  coord_map("gilbert")+  theme(    panel.grid=element_blank(),    panel.background=element_blank()  )

china_all+ coord_sf(xlim=c(85,108),ylim=c(30,50))+ labs(x="",y="",title="")+ theme(panel.border = element_rect(fill="NA"), legend.position='none', axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank())

为各个省份填充不一样的颜色,更有辨识度。

p <- ggplot(china,aes(x=long,y=lat,fill=group,alpha=0.1))+  geom_polygon(colour="black")+  coord_map("gilbert")+  theme(    panel.grid=element_blank(),    panel.background=element_blank(),    legend.position = 'none'  )+  coord_sf(xlim=c(85,108),ylim=c(30,50))+  labs(x="",y="",title="")+  theme(panel.border = element_rect(fill="NA"),        axis.text=element_blank(),        axis.ticks=element_blank(),        axis.title=element_blank())

查询主要去过城市经纬度信息,这里发现一个网站。经纬度网站:

#http://www./chaxun/zhongguochengshijingweidu/

读入坐标数据,在地图上标注出来。

city <- read.csv("city.csv", header = T)library(ggrepel)#china=rgdal::readOGR("bou2_4p.shp")#A <- china_map@data#C <- data.frame(A, id=rownames(A))#china_map_frame <- fortify(china)#chinamap<-join(C,china_map_frame,type="full")
ggplot()+ geom_path(data = chinamap, aes(long, lat, group=group))+ geom_polygon(colour="black",aes(long, lat,fill=group, alpha=0.05), data = chinamap)+ coord_map("gilbert")+ theme( panel.grid=element_blank(), panel.background=element_blank(), legend.position = 'none' )+ scale_fill_manual(values = my36colors)+ coord_sf(xlim=c(85,108),ylim=c(30,50))+ labs(x="",y="",title="Northwest China")+ theme(panel.border = element_rect(fill="NA"), axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank(), plot.title = element_text(hjust = 0.5, size = 15))+ geom_point(data=city, aes(x,y),color='red', shape=16, alpha=0.5,size=3)+ geom_text_repel(data=city, aes(x,y,label=city), color="black", size=4, fontface="italic", #标签设置,颜色、大小、字体、指示箭头设置 arrow = arrow(ends="first", length = unit(0.01, "npc")), box.padding = 0.2,                  point.padding = 0.3, segment.color = 'black', segment.size = 0.3, force = 1, max.iter = 3e3)

接下来继续修饰,连线+每个地方拍的代表性美照放一张。连线如果是按照行车路线的话,需要收集更多的沿途坐标文件。这里我去世画的蓝色线,来时是绿色。

ggplot()+  geom_path(data = chinamap, aes(long, lat, group=group))+  geom_polygon(colour="black",aes(long, lat,fill=group, alpha=0.05), data = chinamap)+  coord_map("gilbert")+  theme(    panel.grid=element_blank(),    panel.background=element_blank(),    legend.position = 'none'  )+  scale_fill_manual(values = my36colors)+  coord_sf(xlim=c(85,108),ylim=c(30,50))+  labs(x="",y="",title="Northwest China")+  theme(panel.border = element_rect(fill="NA"),        axis.text=element_blank(),        axis.ticks=element_blank(),        axis.title=element_blank(),        plot.title = element_text(hjust = 0.5, size = 15))+  geom_line(data = city[-6,], aes(x, y), size = 1,color = '#8BB6D6')+  geom_line(data = city[5:6,], aes(x, y), size = 1,color = '#EE2627')+  geom_line(data = city %>% slice(1,8), aes(x, y), size = 1,color = '#35A132')+  geom_point(data=city, aes(x,y),color='red', shape=16, alpha=0.5,size=3)+  geom_text_repel(data=city, aes(x,y,label=city), color="black", size=4, fontface="italic", #标签设置,颜色、大小、字体、指示箭头设置                  arrow = arrow(ends="first", length = unit(0.01, "npc")), box.padding = 0.2,                  point.padding = 0.3, segment.color = 'black', segment.size = 0.3, force = 1, max.iter = 3e3)

添加图片。

library(ggimage)city$image <- c('wushan.png','lanzhou.png',                'jiayuguan.png',                'yumen.png',                'guazhou.png',                'dunhuang.png',                'wu.png',                'shi.png')


ggplot()+ geom_path(data = chinamap, aes(long, lat, group=group))+ geom_polygon(colour="black",aes(long, lat,fill=group, alpha=0.05), data = chinamap)+ coord_map("gilbert")+ theme( panel.grid=element_blank(), panel.background=element_blank(), legend.position = 'none' )+ scale_fill_manual(values = my36colors)+ coord_sf(xlim=c(85,108),ylim=c(30,50))+ labs(x="",y="",title="Northwest China")+ theme(panel.border = element_rect(fill="NA"), axis.text=element_blank(), axis.ticks=element_blank(), axis.title=element_blank(), plot.title = element_text(hjust = 0.5, size = 15))+ geom_image(data=city,aes(x=x,y=y,image=image),size=0.1)+ geom_line(data = city[-6,], aes(x, y), size = 1,color = '#8BB6D6')+ geom_line(data = city[5:6,], aes(x, y), size = 1,color = '#EE2627')+ geom_line(data = city %>% slice(1,8), aes(x, y), size = 1,color = '#35A132')+ geom_point(data=city, aes(x,y),color='red', shape=16, alpha=0.5,size=3)+ geom_text_repel(data=city, aes(x,y,label=city), color="black", size=4, fontface="italic", #标签设置,颜色、大小、字体、指示箭头设置 arrow = arrow(ends="first", length = unit(0.01, "npc")), box.padding = 0.2, point.padding = 0.3, segment.color = 'black', segment.size = 0.3, force = 1, max.iter = 3e3)

当然了,除了以上办法,还有较多资源可以获取地图信息,比如阿里云平台就提供了地图信息json文件,不仅有全国的,还包含各个省份的。

各省地图文件:

#http://datav.aliyun.com/portal/school/atlas/area_selector#&lat=31.728167146023935&lng=104.32617187499999&zoom=3

以我的家乡天水为例,我们下载文件并读入数据作图:

library(sf)library(tidyverse)TS <- read_sf("天水市.json") TS <- as.data.frame(TS) #格式转换
y= c(34.57865, 34.5635, 34.75287, 34.86235, 34.74733, 34.72196, 34.99324)
x= c(105.72448, 105.89763, 106.13988, 105.67330, 105.33235, 104.89170, 106.21242) TS$x <- xTS$y <- y library(ggrepel)TS %>% ggplot()+ geom_sf()+ geom_point(aes(x,y),color='grey40', size=5, alpha=0.4)+ geom_text_repel(data=TS, aes(x=x, y=y,label=name), color="black", size=4)+ labs(x="",y="",title="")+ theme(panel.border = element_rect(fill="NA"), legend.position='none', axis.title=element_blank())

详情请联系作者:

    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多