How to link correctly track points around a polar projection map?

落花浮王杯 提交于 2019-12-01 00:20:55

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!