World map showing day and night regions : <p>I'm trying to add a daytime/nighttime line to a world map using <code>ggplot</code> in order to indicate day and night regions; something like this:</p>

<p><a href="https://i.stack.imgur.com/pYf7o.png" rel="noreferrer"><img src="https://i.stack.imgur.com/pYf7o.png" alt="daynightexample"></a></p>

<p>The plan is to animate my map over a 24 hour cycle like this:</p>

<p><a href="https://i.stack.imgur.com/K1izu.gif" rel="noreferrer"><img src="https://i.stack.imgur.com/K1izu.gif" alt="sinewavetest"></a></p>

<p>The above animation is achieved using a sine wave, which I know is totally inaccurate. I'm aware that <code>geosphere::gcIntermediate</code> allows me to draw great-circle lines, like so:</p>

<pre><code>library(ggplot2)
library(ggthemes)
library(geosphere)

sunPath1 &lt;- data.frame(gcIntermediate(c(-179, -30), c(0, 30), n=100))
sunPath2 &lt;- data.frame(gcIntermediate(c(0, 30), c(179, -30), n=100))
sunPath &lt;- 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()
</code></pre>

<p><a href="https://i.stack.imgur.com/9BGEn.png" rel="noreferrer"><img src="https://i.stack.imgur.com/9BGEn.png" alt="greatcircletest"></a></p>

<p>Although I'm not sure if it will be possible to draw the desired lines at different points during the year, e.g. in March it looks like so:</p>

<p><a href="https://i.stack.imgur.com/z5FQJ.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/z5FQJ.jpg" alt="inthesky"></a></p>

<hr>

<p>I've had no luck finding a solution but am guessing I don't know the keywords to search for as this is way outside my sphere of knowledge. I think the answer may lie somewhere in the <a href="https://en.wikipedia.org/wiki/Sunrise_equation" rel="noreferrer">sunrise equation</a>, 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. <a href="https://in-the-sky.org/twilightmap.php" rel="noreferrer">This website</a> (used for the plot above) also seems useful but I'm not yet sure how!</p>
