library(ggplot2)
library(dplyr)
library(lubridate)
data(bats)
monitoring <- attr(bats, "monitoring")
## A lunar annotation can be added to a geom_hourglass layer
ggplot(mutate(bats, YEAR = year(RECDATETIME), MONTH = month(RECDATETIME)) |>
filter(YEAR == 2018, MONTH == 5),
aes(x = RECDATETIME, col = SPECDESCSCI)) +
geom_hourglass() +
annotate_lunarphase(
longitude = monitoring$longitude[[1]],
latitude = monitoring$latitude[[1]],
placement = 0.8) +
scale_x_datetime(limits = as_datetime(c("2018-04-27", "2018-05-31")))
## In fact, it can be added to any plot with a continuous datetime scale
ggplot(data.frame(stamp = seq(as_datetime("2025-04-01 UTC"),
as_datetime("2025-04-30 UTC"),
length.out = 20),
value = 1:20), aes(x = stamp, y = value)) +
geom_point() +
annotate_lunarphase()
## Moreover, you can add it to an arbitrary plot without such scales,
## but then you need to specify the date
ggplot(data.frame(stamp = 1:20,
value = 1:20), aes(x = stamp, y = value)) +
geom_point() +
annotate_lunarphase(date = "2020-01-01", placement = c(0.1, 0.9))
Run the code above in your browser using DataLab