GGmap: geom_polygon | ggplot examples

GGmap: geom_polygon | ggplot examples

主要参考: https://blog.csdn.net/kMD8d5R/article/details/86582019

install.packages('mapproj')

快速上手

library(ggplot2)
library(ggthemes)

## 世界地图,比较好的配色
world <- map_data("world")
worldmap <- ggplot(world, aes(x = long, y = lat, group = group)) +
geom_polygon(fill = "#00518E",color = "#317DA4") +
scale_y_continuous(breaks = (-2:2) * 30) +
scale_x_continuous(breaks = (-4:4) * 45)

## 正射投影
worldmap + coord_map("ortho") # 默认北极为中心点。  
## 南极为中心点
worldmap + coord_map("ortho", orientation = c(30, 100, 0))# 中国 中心
worldmap + coord_map("ortho", orientation = c(-90, 0, 30)) # 顺时针旋转30度
worldmap + coord_map("ortho", orientation = c(41, -74, 0)) # 随便取一点

##下图
worldmap + coord_map("ortho", orientation = c(30, 100, 0))+ theme_map()

1

原理

x=c(0,0,6,6)
y=c(0,7,0,8)
TB=data.frame(X=x,Y=y)

gplot(TB,aes(X,Y)) + geom_polygon(aes(fill='red')) + geom_point() + theme_light()

123

动画

1. gganimate

gganimate 只能画平面图的动画,不能画球状以后的动画
gps.txt
随便测试一下

library(gganimate)

City = read.table('gps.txt')
City$Group=1
Num=nrow(City)
for(i in c(2:10)){
tmp = City[1:Num,1:3]
tmp$Group=i
tmp[[3]]=tmp[[3]]+i-1
City =rbind(City,tmp)}

ggplot(head(City,30))+
geom_polygon(data=world,aes(x = long, y = lat, group = group),
fill = "#00518E",color = "#317DA4")+
geom_point(data=City,aes(x=V2,y=V3)) +
geom_text(data=head(City,Num),aes(x=V2,y=V3,label=V1))+
coord_cartesian(xlim=c(70,150),ylim = c(10,65))+
transition_time(City$Group) + theme_map()

## 简单示范

123

2.多 png 转 gif

先画出一系列的png,在叠加成gif

##旋转
library(ggrepel)
p <- ggplot(head(City,15))+ geom_polygon(data=world,aes(x = long, y = lat, group = group),fill = "#00518E",color = "#317DA4")+
geom_point(data=City,aes(x=V2,y=V3)) + coord_map("ortho")+
geom_text(data=head(City,Num),aes(x=V2,y=V3,label=V1))+ theme_map()
for(i in c(1:5)){
p = p + coord_map("ortho", orientation = c(30, 50+i*20, 0))
ggsave(paste(i,'_map.png',sep=''))
}

#bash 下完成
#convert $(ls *_map.png| sort -n) map.gif

各种杂耍

加上经纬线

[经纬线函数链接](https://www.yuque.com/liuwenkan/bni63i/bwkcrz#rlfqy)/

##杂技函数
经纬线() +geom_polygon(data=world, aes(x = long, y = lat, group = group),
fill = "#00518E",color = "#317DA4") +
scale_y_continuous(breaks = (-2:2) * 30) +
coord_map("ortho", orientation = c(30, 100, 0)) +
scale_x_continuous(breaks = (-4:4) * 45)+ theme_map()+


1

链接两点

这里用到Connet函数,详见“曲线链接两点” 
创建地点

echo "巴黎  2.294694  48.856958
墨尔本 144.958832 -37.812164
马德里 -3.677528 40.390197
SD -117.071944 32.775554
马塞卢 27.512883 -29.363468
罗萨里奥 -60.62623 -32.949375
东京 139.752725 35.68461" > 20City
北京= c(116.4167, 39.91667)                                                                                                   

City2=read.table('20City')

建立链接
Num=0
Result=data.frame()
for(i in c(1:nrow(City2))){
Num=Num+1
tmp = Connet(北京,c(City2[i,][[2]],City2[i,][[3]]),Space=pi/10)
tmp$Group=Num
Result = rbind(Result,tmp)
}

经纬线() +
geom_polygon(data=world, aes(x = long, y = lat, group = group), fill = "#00518E",color = "white",size=1.2)+
coord_map("ortho", orientation = c(30, 100, 0)) +theme_map()+
geom_polygon(data=world, aes(x = long, y = lat, group = group), fill = "#00518E",color = "#317DA4",alpha=0.25,size=0.5,linetype=6) +
geom_point(data=Result,aes(x=X,y=Y))

1

NlTFvd.gif

More

GGmap: geom_map

123

geom_sf

123

GGmap: geom_polygon | ggplot examples

https://karobben.github.io/2020/06/20/R/GGmap_polygon/

Author

Karobben

Posted on

2020-06-20

Updated on

2024-01-22

Licensed under

Comments