ggplot2:如何正确跟踪极地投影地图附近的点? [英] ggplot2: How to link correctly track points around polar projection map?

查看:200
本文介绍了ggplot2:如何正确跟踪极地投影地图附近的点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图用ggplot2绘制一只在南极周围的鸟的轨迹。到目前为止,我得到了极坐标中的地图,我也设法正确绘制轨迹,我几乎可以正确地链接它们。但是,当轨迹穿过国际日期行,即经度180°,ggplot2是无法正确链接线路两侧的2个点。它连接他们,但一直在地球上。
所以我正在寻找一种方法来强制ggplot将点与180°的最短路连接。



这是我的代码,图表。

  require(ggplot2)
require(rgdal)
/ pre>

数据:



track< -read.table(NOGP_87470_track。 txt,sep =\t,h = T)



由以经度/纬度/日期为特征的75个轨迹点POSIXct)。 PTT是鸟的名字。日期在这里是没有用的。



head(track)

  PTT长尾日期
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长期日期
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)

  w orld< -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)只有南半球
pp< - p + geom_point(size = 3,shape = 19)+ geom_path(size = 1,col =gray)

这里是我得到的:



红色箭头是鸟的飞行方向:





我在绿色中点了2点,我想链接180°线,并在它们之间手工制作一条绿色的虚线。正如你所看到的那样,它们是通过一条直线连接在一起的方向,方向是错误的。有没有人想到如何处理这个问题?/ p>


谢谢!

解决方案

您可以通过使用coord_map而不是coord_polar来实现这一点,并确保经度不是一直到-180度,而是继续增加。我没有访问您的原始数据和您使用的地图,所以我已经生成了我自己的虚拟数据集。



加载包并创建地图和轨道数据:

 库(ggplot2)
库(dplyr)
south_map< map_data(world)%>%group_by(group)%>%filter(min(lat)< = -20)

track< - data.frame(long = 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)+
主题(panel.grid.major.x = element_blank())+
coord_polar(θ=x)+#在极坐标中投影地图
ylim(-90,-20)+#只保留南半球
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 = )+ 
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纬度,中断= 25 * 0:3,标签= -25 * 0:3)

< img src =https://i.stack.imgur.com/exia6.pngalt =地图预测图>



我不知道是否可能将南极的一个等级投影集中在一起,所以我采取了纬度的负面,并在标签阶段排序。我也删除了x尺度,因为它目前对于coord_map并不真正有效,就我所知。如果需要,您可以使用 geom_text 手动添加一些标记。


I'm trying to plot with ggplot2 the track of a bird around the antarctic. So far I got 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? Thanks anyway !!

解决方案

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.

这篇关于ggplot2:如何正确跟踪极地投影地图附近的点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆