如何正确链接极地投影图周围的轨迹点? [英] How to link correctly track points around a polar projection map?
问题描述
由于轨道穿越国际日期线(即经度180°),ggplot2无法正确连接线路两侧的2个点。它确实将它们连接起来,但是一直绕着地球而行。
所以我正在寻找一种方法来强制ggplot通过180°的最短路线连接点。
这里是我的代码和结果
require(ggplot2)
require(rgdal)
data:
track< -read.table( NOGP_87470_track.txt,sep =\ t,h = T)
由75跟踪点以经度/纬度/日期(POSIXct)为特征。 PTT是鸟的名字。日期在这里没有用。
头(轨道)
:
PTT long lat date
1 87470 51.645 -46.334 2009-02-03 14:50:00
2 87470 52.000 -46.289 2009-02- 04 20:11:00
3 87470 52.556 -46.083 2009-02-06 07:15:00
4 87470 55.822 -44.667 2009-02-07 17:28:00
5 87470 60.679 -41.915 2009-02-09 04:03:00
6 87470 63.059 -41.649 2009-02-10 15:04:00
以下是轨道交叉线的数据部分(线:19& 20)
PTT long lat date
18 87470 160.907 -53.356 2009-02-28 06:25:00
19 87470 165.791 -54.588 2009-03-01 15:39:00
20 87470 -174.636 -50.893 2009-03-03 05:04:00
21 87470 -160.111 -50.832 2009-03-04 13:47:00
22 87470 -138.875 -53.474 2009-03 -06 01:49:00
地图(笛卡尔坐标系中世界海岸线的shapefile)
世界< -readOGR(Coastlines \\World_continental_WGS84_Proj.shp,World_continental_WGS84_Proj)
p < - ggplot(track,aes(x = long,y = lat))
p < - p + geom_polygon(data = world,aes(x = long,y = lat,group = group),color =gray,fill =gainsboro)+
theme(panel.grid.major.x = element_blank())+
coord_polar(theta =x,start = pi / 2,direction = 1)+#在极坐标中投影地图
ylim(-90,-20)#保留只有南半球b $ b pp < - p + geom_point(size = 3,shape = 19)+ geom_path(size = 1,col =gray)
以下是我得到的结果:
红色箭头是鸟的飞行方向:
我用绿色着色了2分我想链接acc罗斯180°线,并手动在他们之间建立一条绿色虚线。正如你所看到的,它们通过一条沿错误方向绕着杆子的线相连。
有没有人知道如何处理这个问题?
<你可以通过使用coord_map而不是coord_polar来实现这一点,并确保经度不会回到-180度,而是继续增加。我无法访问您的原始数据和您使用的地图,因此我制作了自己的虚拟数据集。
加载包并创建地图和轨迹数据:
library(ggplot2)
library(dplyr)
south_map< - map_data(world)%>%group_by(group)%>%filter(min(lat)< = -20)
track< - data.frame(long = cumsum c(210,rnorm(100,5,2)))%% 360-180,
lat = cumsum(c(-50,rnorm(100,0.1,2))))
初始极坐标图
ggplot(track,aes(x = long,y = lat))+
geom_polygon(aes(group = group),data = south_map,color =gray,fill =gainsboro)+
theme(panel.grid.major.x = element_blank())+
coord_polar(theta =x)+#以极坐标投影地图
ylim(-90,-20)+#to只保留南半球
geom_point(size = 3,shape = 19,color =red)+ geom_path(size = 1,color =gray)
修正经度,使它们不断增加
track_new< - track
long_diff< - diff(track $ long)
long_diff [long_diff< -180]< - long_diff [long_diff< -180] + 360
track_new $ long< - cumsum(c(track $ long [1],long_diff))
使用地图投影绘制
ggplot(track_new,aes(x = long,y = -lat ))+
geom_polygon(aes(group = group),data = south_map,color =gray,fill =gainsboro)+
coord_map(azequidistant)+
geom_point( size = 3,shape = 19,color =red)+
geom_path(size = 1,col =gray)+
scale_x_continuous(breaks = NULL)+
scale_y_continuous( latitude,breaks = 25 * 0:3,labels = -25 * 0:3)
< img src =https://i.stack.imgur.com/exia6.pngalt =地图投影图>
我不确定它是否有可能将南极的azequidistant投影集中在一起,所以我采用了纬度的负值,并在贴标签阶段对此进行了整理。据我所知,我还删除了x尺度,因为它目前对coord_map没有真正的效果。如果需要,您可以使用 geom_text
手动添加一些标记。
I'm trying to plot with ggplot2 the track of a bird around the antarctic. So far I have a map projected in polar coordinates, I also managed to plot the track points correctly and I almost link them correctly but...
As the track crosses the international date line (i.e. longitude 180°) ggplot2 is not able to correctly link the 2 points in either sides of the line. It does connect them but by going all the way around the earth. So I'm looking for a way to force ggplot to link the points by the shortest road across the 180°.
Here is my code and the resulting graph.
require(ggplot2)
require(rgdal)
data:
track<-read.table("NOGP_87470_track.txt", sep="\t",h=T)
consists of 75 track points characterized by longitude / latitude / date (POSIXct). PTT is the name of the bird. Date is of no use here.
head(track)
:
PTT long lat date
1 87470 51.645 -46.334 2009-02-03 14:50:00
2 87470 52.000 -46.289 2009-02-04 20:11:00
3 87470 52.556 -46.083 2009-02-06 07:15:00
4 87470 55.822 -44.667 2009-02-07 17:28:00
5 87470 60.679 -41.915 2009-02-09 04:03:00
6 87470 63.059 -41.649 2009-02-10 15:04:00
Here is the part of the data where track cross the line (line: 19 & 20)
PTT long lat date
18 87470 160.907 -53.356 2009-02-28 06:25:00
19 87470 165.791 -54.588 2009-03-01 15:39:00
20 87470 -174.636 -50.893 2009-03-03 05:04:00
21 87470 -160.111 -50.832 2009-03-04 13:47:00
22 87470 -138.875 -53.474 2009-03-06 01:49:00
map (shapefile of the world coastlines in cartesian coordinates)
world<-readOGR("Coastlines\\World_continental_WGS84_Proj.shp", "World_continental_WGS84_Proj")
plot
p <- ggplot(track, aes(x=long, y=lat))
p <- p + geom_polygon( data=world, aes(x=long, y=lat, group = group),colour="grey", fill="gainsboro" ) +
theme( panel.grid.major.x = element_blank()) +
coord_polar(theta="x", start=pi/2,direction=1) + # project the map in polar coordinates
ylim (-90, -20) # to keep only south hemisphere
pp <- p + geom_point(size=3,shape=19) + geom_path(size=1,col="grey")
And here is what I get:
red arrows are the flight direction of the bird:
I colored in green the 2 points I want to link accross the 180° line and manually made a green dotted line between them. As you can see they are connected by a line which goes all around the pole in the wrong direction.
Does anyone got an idea on how do deal with that?
You can achieve this by using coord_map rather than coord_polar, and ensuring that the longitudes don't wrap round to -180 degrees but rather continue to increase. I don't have access to your original data nor the map you're using, so I've produced my own dummy dataset.
Load packages and create map and track data:
library("ggplot2")
library("dplyr")
south_map <- map_data("world") %>% group_by(group) %>% filter(min(lat) <= -20)
track <- data.frame(long = cumsum(c(210, rnorm(100, 5, 2))) %% 360 - 180,
lat = cumsum(c(-50, rnorm(100, 0.1, 2))))
Initial polar plot
ggplot(track, aes(x=long, y=lat)) +
geom_polygon(aes(group = group), data = south_map, colour = "grey", fill = "gainsboro") +
theme(panel.grid.major.x = element_blank()) +
coord_polar(theta="x") + # project the map in polar coordinates
ylim(-90, -20) + # to keep only south hemisphere
geom_point(size = 3, shape = 19, colour = "red") + geom_path(size = 1, colour = "grey")
Fix longitudes so that they keep increasing
track_new <- track
long_diff <- diff(track$long)
long_diff[long_diff < -180] <- long_diff[long_diff < -180] + 360
track_new$long <- cumsum(c(track$long[1], long_diff))
Plot using map projection
ggplot(track_new, aes(x = long, y = -lat)) +
geom_polygon(aes(group = group), data = south_map, colour = "grey", fill = "gainsboro") +
coord_map("azequidistant") +
geom_point(size = 3, shape = 19, colour = "red") +
geom_path(size = 1, col = "grey") +
scale_x_continuous(breaks = NULL) +
scale_y_continuous("latitude", breaks = 25 * 0:3, labels = -25 * 0:3)
I'm not sure if it's possible to centre an azequidistant projection on the South Pole so I've taken the negative of the latitude and sorted this out in the labelling stage. I've also removed the x scale since it currently doesn't really work for coord_map as far as I can tell. You could use geom_text
to manually add some markers in if needed.
这篇关于如何正确链接极地投影图周围的轨迹点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!