Skip to content

Instantly share code, notes, and snippets.

@ianmcook
Last active March 5, 2017 16:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ianmcook/41468bb4d1e8b6d94c9f8261dd402788 to your computer and use it in GitHub Desktop.
Save ianmcook/41468bb4d1e8b6d94c9f8261dd402788 to your computer and use it in GitHub Desktop.
Ribbon plot of 2017 daily sunrise and sunset at RDU airport
# load packages
library(maptools)
library(lubridate)
library(ggplot2)
# vector of all the days in 2017
days <- seq(from = as.POSIXct("2017-01-01"), to = as.POSIXct("2017-12-31"), by = "days")
# coordinates of RDU airport
lon <- -78.788
lat <- 35.8801
coord <- matrix(c(lon, lat), nrow = 1)
# compute sunrise and sunet times for every day
ds <- data.frame(
date = days,
sunrise <- sunriset(coord, days, direction = "sunrise", POSIXct.out = TRUE)$time,
sunset <- sunriset(coord, days, direction = "sunset", POSIXct.out = TRUE)$time
)
# strip date information from sunrise and sunset columns
ds$sunrise <- hms(as.character(ds$sunrise, format = "%H:%M:%S"))
ds$sunset <- hms(as.character(ds$sunset, format = "%H:%M:%S"))
# plot the result
ggplot(ds, aes(x = date, ymin = sunrise, ymax = sunset)) +
labs(
title = "2017 Daily Sunrise and Sunset",
subtitle = "Raleigh-Durham International Airport, North Carolina, United States"
) +
geom_ribbon(fill = "orange") +
scale_x_datetime(
name = "Date",
date_labels = "%b",
date_breaks = "1 month",
minor_breaks = NULL
) +
scale_y_time(
name = "Time",
breaks = hm(paste(seq(from = 0, to = 24, by = 2), 0)),
labels = function(t) sprintf("%02d:%02d", hour(t), minute(t)),
limits = c(hm("0 45"), hm("23 15"))
)
@ianmcook
Copy link
Author

ianmcook commented Mar 5, 2017

sunrise-sunset-rdu-2017

@ianmcook
Copy link
Author

ianmcook commented Mar 5, 2017

The discontinuities represent the beginning and end of daylight saving time (Mar 12 and Nov 5). They are slightly off from vertical because ggplot2::geom_ribbon cannot plot step functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment