Skip to content

Instantly share code, notes, and snippets.

@chichacha
Created May 20, 2018 16:46
Show Gist options
  • Save chichacha/9d182d142fbe7c753f9c099539489933 to your computer and use it in GitHub Desktop.
Save chichacha/9d182d142fbe7c753f9c099539489933 to your computer and use it in GitHub Desktop.
Figuring out what gist is...
library(tidyverse)
library(suncalc) ## https://CRAN.R-project.org/package=suncalc
library(scales) ## So I can use date_breaks etc on chart
library(lubridate) ## for dealing with time!
library(ggrepel)
interest <- c("dawn","sunrise","goldenHourEnd","goldenHour","sunset","dusk")
## dawn - sunrise - goldenHourEnd ==> Great time for photography in the morning.
## goldenHour - sunset, desk ==> Great time for photography in the evening.
event.2018 <- tibble(
date = as.Date(c("2018-03-20","2018-06-21","2018-09-22","2018-12-21")),
name = c("Spring Equinox","Summer Solstice","Fall Equinox","Winter Solstice")
)
#https://www.suncalc.org/#/49.2827,-123.1207,8/2018.05.20/23:59/1/0
vancouver <-getSunlightTimes(date = seq.Date(as.Date("2018-01-01"), as.Date("2018-12-31"), by = 1),
keep = interest,
lat = 49.2827, lon = -123.1207, tz="US/Pacific")
#https://www.suncalc.org/#/35.6895,139.6917,8/2018.05.20/09:41/1/0
tokyo <- getSunlightTimes(date = seq.Date(as.Date("2018-01-01"), as.Date("2018-12-31"), by = 1),
keep = interest,
lat = 35.6895, lon = 139.6917, tz = "Japan")
## I want just hour:minute:seconds part NOT date part :)
vancouver <- vancouver %>%
mutate_at(.vars=interest, funs(hms::hms(second(.),minute(.),hour(.))))
tokyo <- tokyo %>%
mutate_at(.vars=interest, funs(hms::hms(second(.),minute(.),hour(.))))
## Convert above table to Long Format!
vancouver.long <- vancouver %>%
select(-lat,-lon) %>% ## i don't really need lat long
gather(key="property", value="time", -date)
tokyo.long <- tokyo %>%
select(-lat,-lon) %>% ## i don't really need lat long
gather(key="property", value="time", -date)
## Earliest Sunset & Latest Sunset
vancouver.anno <-vancouver.long %>%
filter(property %in% c("sunset","sunrise"))%>%
group_by(property) %>%
mutate(prop.rank=row_number(time)) %>% filter(prop.rank %in% c(1,365)) %>%
mutate(explain = paste(format(as.Date(date),"%b %d %a"),"@",time))
tokyo.anno <-tokyo.long %>%
filter(property %in% c("sunset","sunrise"))%>%
group_by(property) %>%
mutate(prop.rank=row_number(time)) %>% filter(prop.rank %in% c(1,365)) %>%
mutate(explain = paste(format(as.Date(date),"%b %d %a"),"@",time))
## y-axis. I want all 24 hours
hms.for.plot <- hms::hms(second=rep(0,24), minute=rep(0,24), hour=seq(0,23))
hms.for.plot <-c(hms.for.plot, hms::hms(second=0,minute=0,hour=24))
#hms.for.plot
hms.label <- c("midnight","","","3am","","","6am","","","9am","","","Noon","","","3pm","","","6pm","","","9pm","","","midnight")
## Plot for Vancouver
vancouver.long %>% ggplot(aes(x=as.Date(date))) +
geom_line(aes(y=time, group=property), linetype=1, size=0.2) +
geom_ribbon(data=vancouver, aes(ymin=sunrise, ymax=sunset), fill="#E6DF44", alpha=0.9) +
geom_ribbon(data=vancouver, aes(ymin=min(hms.for.plot), ymax=sunrise), fill="#011A27", alpha=0.9) +
geom_ribbon(data=vancouver, aes(ymax=dawn, ymin=goldenHourEnd), fill="#F0810F", alpha=0.3) +
geom_ribbon(data=vancouver, aes(ymax=max(hms.for.plot), ymin=sunset), fill="#011A27", alpha=0.9) +
geom_ribbon(data=vancouver, aes(ymax=goldenHour, ymin=dusk), fill="#F0810F", alpha=0.3) +
geom_vline(xintercept=as.numeric(event.2018$date), linetype=3, color="white") +
theme_minimal(base_family="Roboto Condensed", base_size=18) +
scale_y_time(breaks=hms.for.plot, labels=hms.label) +
expand_limits(y=c(hms.for.plot[1], hms.for.plot[25])) +
scale_x_date(breaks=date_breaks("month"), label=date_format("%b\n%Y")) +
labs(title="Vancouver, BC, Canada",subtitle="Daytime vs Nighttime during 2018", x="", y="") +
geom_point(data=vancouver.anno, aes(y=time)) +
geom_label_repel(data=vancouver.anno, aes(y=time, label=explain),
family="Roboto Condensed", fill="#ffffff80")
## Plot for Tokyo
tokyo.long %>% ggplot(aes(x=as.Date(date))) +
geom_line(aes(y=time, group=property), linetype=1, size=0.2) +
geom_ribbon(data=tokyo, aes(ymin=sunrise, ymax=sunset), fill="#E6DF44", alpha=0.9) +
geom_ribbon(data=tokyo, aes(ymin=min(hms.for.plot), ymax=sunrise), fill="#011A27", alpha=0.9) +
geom_ribbon(data=tokyo, aes(ymax=dawn, ymin=goldenHourEnd), fill="#F0810F", alpha=0.3) +
geom_ribbon(data=tokyo, aes(ymax=max(hms.for.plot), ymin=sunset), fill="#011A27", alpha=0.9) +
geom_ribbon(data=tokyo, aes(ymax=goldenHour, ymin=dusk), fill="#F0810F", alpha=0.3) +
geom_vline(xintercept=as.numeric(event.2018$date), linetype=3, color="white") +
theme_minimal(base_family="Roboto Condensed", base_size=18) +
scale_y_time(breaks=hms.for.plot, labels=hms.label) +
expand_limits(y=c(hms.for.plot[1], hms.for.plot[25])) +
scale_x_date(breaks=date_breaks("month"), label=date_format("%b\n%Y")) +
labs(title="Tokyo, Japan",subtitle="Daytime vs Nighttime during 2018", x="", y="") +
geom_point(data=tokyo.anno, aes(y=time)) +
geom_label_repel(data=tokyo.anno, aes(y=time, label=explain),
family="Roboto Condensed", fill="#ffffff80")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment