How to correctly connect tracking points around a map of polar projections?

I am trying to build with ggplot2 a bird track around Antarctica. As long as I have a map projected in polar coordinates, I also managed to plot the points on the track correctly, and I connect them almost correctly, but ...

When a track crosses an international date line (i.e. 180 ° longitude), ggplot2 cannot correctly connect 2 points on either side of the line. He connects them, but, having gone all the way on the earth. So I’m looking for a way to get ggplot to connect the points along the shortest path 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 reference points characterized by longitude / latitude / date (POSIXct). PTT is the name of the bird. The date here is useless.

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 piece of data where the track crosses the line (line: 19 and 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 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 - this is the direction of flight of the bird:

image of bird

I green 2 points that I want to connect along the 180 ° line, and manually made a green dotted line between them. As you can see, they are connected by a line that goes around the pole in the wrong direction.

Has anyone got an idea on how to deal with this?

+3
dictionary r ggplot2 polar-coordinates
source share
1 answer

You can achieve this using coord_map rather than coord_polar, and make sure that the longitudes do not wrap up to -180 degrees, but rather continue to increase. I do not have access to the source data or the map you are using, so I created my own dummy dataset.

Download 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 graph

 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") 

Initial polar plot

Correct longitudes so that they continue to grow

 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)) 

Chart 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) 

Map projected plot

I am not sure whether it is possible to center the aze-vivid projection onto the South Pole, so I took the negative value of latitude and disassembled it at the marking stage. As far as I can tell, I also removed the x scale, since currently it really doesn't work for Coord_Map. You can use geom_text to manually add some tokens if necessary.

+1
source share

All Articles