显示昼夜区域的世界地图

World map showing day and night regions

我正在尝试使用 ggplot 在世界地图上添加一条 daytime/nighttime 线,以指示白天和夜晚的区域;像这样:

计划是像这样在 24 小时周期内为我的地图制作动画:

上面的动画是用正弦波实现的,我知道这是完全不准确的。我知道 geosphere::gcIntermediate 允许我画大圆线,像这样:

library(ggplot2)
library(ggthemes)
library(geosphere)

sunPath1 <- data.frame(gcIntermediate(c(-179, -30), c(0, 30), n=100))
sunPath2 <- data.frame(gcIntermediate(c(0, 30), c(179, -30), n=100))
sunPath <- rbind(sunPath1, sunPath2)

ggplot(sunPath) +
  borders("world", colour = "gray95", fill = "gray90") +
  geom_ribbon(aes(lon, ymax = lat), ymin=-180, fill="black", alpha=0.2) +
  theme_map()

虽然我不确定是否可以在一年中的不同时间点绘制所需的线条,例如三月份看起来像这样:


我没能找到解决方案,但我猜我不知道要搜索的关键字,因为这超出了我的知识范围。我认为答案可能在 sunrise equation, but I've no idea how to apply these to find a solution, nor do I know how to vary these parameters over the course of the year. This website 中的某处(用于上图)似乎也很有用,但我还不确定如何!

我已经在@jazzurro 的帮助下解决了这个问题,他将我指向 Leaflet R package. I've ported their javascript plugin, L.Terminator.js,指向 R,以便在交互式传单地图之外使用。

功能可用here

这是一个 24 小时动画的示例:

library(dplyr)
library(ggplot2)
library(ggthemes)
library(gganimate)
library(animation)

terminatorLatLon <- lapply(seq(0, 23, 1), function(x) {

  t0 <- as.POSIXct(Sys.Date()) + (60*60*x)

  terminator(t0, -180, 190, 0.5) %>%
    mutate(frame = x)
}) %>%
  plyr::rbind.fill()

chart <- ggplot(terminatorLatLon, aes(frame = frame)) +
  borders("world", colour = "gray90", fill = "gray85") +
  geom_ribbon(aes(lat, ymax = lon), ymin = 90, alpha = 0.2) +
  coord_equal(xlim = c(-180, 190), ylim = c(-58, 85), expand = 0) +
  theme_map()

gganimate(chart, interval = 0.1, ani.width=1000, ani.height=600, filename = "terminator-animation.gif")