Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
---
title: "Day length plot"
author: "Kenneth Wong"
date: "6/21/2021"
output:
md_document:
variant: markdown_github
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```
```{r}
library(readr)
library(dplyr)
library(tidyr)
library(ggplot2)
library(lubridate)
library(scales)
```
```{r}
# https://data.gov.hk/en-data/dataset/hk-hko-rss-times-of-sunrise-suntransit-sunset
sunriseset_2021 = read_csv("./data/data-raw/Sun_rise_set_2021.csv")
```
```{r}
sunriseset_2021_daylength = sunriseset_2021 %>%
mutate(daylength = difftime(SET, RISE, units = "hours"))
sunriseset_2021_plotdata = sunriseset_2021 %>%
pivot_longer(
cols = c(RISE, TRAN, SET),
names_to = "phenomenon",
values_to = "TIME"
) %>%
filter(phenomenon != "TRAN")
```
```{r}
DAY_EARLIEST_SUNRISE = as.Date("2021-06-07")
DAY_LATEST_SUNSET = as.Date("2021-07-04")
DAY_LONGEST_DAYTIME = as.Date("2021-06-21")
earliest_sunrise = filter(sunriseset_2021_plotdata, DATE == DAY_EARLIEST_SUNRISE & phenomenon == "RISE")
latest_sunset = filter(sunriseset_2021_plotdata, DATE == DAY_LATEST_SUNSET & phenomenon == "SET")
highlight_lines = filter(sunriseset_2021_plotdata, DATE %in% c(DAY_EARLIEST_SUNRISE, DAY_LATEST_SUNSET, DAY_LONGEST_DAYTIME))
```
```{r}
# Reverse the draw order of date
# https://stackoverflow.com/questions/43625341/reverse-datetime-posixct-data-axis-in-ggplot/43626186
c_trans <- function(a, b, breaks = b$breaks, format = b$format) {
a <- as.trans(a)
b <- as.trans(b)
name <- paste(a$name, b$name, sep = "-")
trans <- function(x) a$trans(b$trans(x))
inv <- function(x) b$inverse(a$inverse(x))
trans_new(name, trans, inverse = inv, breaks = breaks, format=format)
}
reverse_date <- c_trans("reverse", "date")
```
```{r, fig.width=7, fig.height=20, dpi=300}
ggplot(sunriseset_2021_plotdata, aes(x = as.POSIXct(TIME), y = DATE, color = DATE)) +
geom_line(aes(group = DATE), color = "#aaaaaa", size = 0.5, lineend = "butt") +
geom_point(data = bind_rows(earliest_sunrise, latest_sunset), pch = 16, size = 3) +
geom_line(data = highlight_lines, aes(group = DATE), size = 1, lineend = "butt") +
scale_y_continuous(trans = reverse_date) +
# as.POSIXct() assumes time without date has date of unix epoch?
# https://stackoverflow.com/questions/60068798/how-to-set-axis-daily-time-limits-with-facet-wrapday-in-ggplot2
expand_limits(x = c(ymd_h(1970010103), ymd_h(1970010121))) +
scale_x_datetime(date_labels = "%H:%M") +
theme_minimal() +
theme(
plot.margin = margin(t = 0.2, r = 0.2, b = 0.2, l = 0.2, unit = "in"),
plot.background = element_rect(fill = "#f8f1eb"),
plot.title = element_text(size = 24, face = "bold"),
plot.title.position = "plot",
plot.subtitle = element_text(size = 14),
legend.position = "none",
plot.caption = element_text(hjust = 0),
plot.caption.position = "plot",
axis.title.x = element_blank(),
axis.title.y = element_blank()
) +
labs(
title = "Longest day? Earliest day? Latest day?",
subtitle = "Daytime of Hong Kong in 2021",
caption = "credits here"
)
```
```{r}
ggsave("daylength_plot.pdf", width = 7, height = 20, units = "in")
```
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment