forked from BjnNowak/TidyTuesday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSC_TimeZone.R
138 lines (127 loc) · 4.03 KB
/
SC_TimeZone.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
library(tidyverse)
library(camcorder)
library(showtext)
library(ggtext)
library(glue)
library(solartime)
library(sf)
# Set fonts
font_add_google("Raleway","ral")
font_add_google("Pacifico","pac")
font_add_google("Jost","jo")
showtext_auto()
# Plot size
gg_record(
dir = file.path(tempdir(),"recording"),
device = "png",
width = 40,
height = 30,
units = "cm",
dpi = 300
)
# Load data
###########
transitions <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-28/transitions.csv')
timezones <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-28/timezones.csv')
timezone_countries <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-28/timezone_countries.csv')
countries <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/master/data/2023/2023-03-28/countries.csv')
# World map
world_ne <- sf::read_sf("Data/Erosion/data/world_map/ne_110m_admin_0_countries_lakes.shp")%>%
sf::st_transform(crs="ESRI:54030")
# Data cleaning
###############
# Get last valid hour
tra <- transitions%>%
group_by(zone)%>%
mutate(da=lubridate::as_datetime(begin))%>%
filter(da<"2023-04-01")%>%
filter(begin==max(begin))
# Compute difference from solar time
# thanks to {solartime}
loc <- tra%>%
left_join(timezones)%>%
mutate(off_hour = offset/3600)%>%
mutate(diff_solar = 60 * computeSolarToLocalTimeDifference(longitude, timeZone = off_hour, doy = 90))%>%
select(zone,offset,latitude,longitude,diff_solar)%>%
drop_na()
# Convert to sf object
loc_sf <- sf::st_as_sf(loc,coords=c("longitude","latitude"))
# Set crs then transform to same crs as world map
st_crs(loc_sf) = "EPSG:4326"
loc_rob <- sf::st_transform(loc_sf,crs="ESRI:54030")
# Make plot
###########
# Create graticule
grat <- sf::st_graticule(lat = c(-89.9, seq(-90, 60, 20), 89.9))
# Set colors
col_back <- "#1D201F"
col_world <- "#073B4C"
pal <- c(
"A"="#45CAFF",
"B"="#A2CFDD",
"C"="#D1D1CC",
"D"="#FFD3BA",
"E"="#FF7793",
"F"="#FF497F",
"G"="#FF1B6B"
)
ggplot()+
geom_sf(
world_ne,mapping=aes(geometry=geometry),
fill=col_world,color=alpha("white",0.15))+
geom_sf(
loc_rob,
mapping=aes(geometry=geometry,color=diff_solar),
size=3
)+
geom_sf(
grat,mapping=aes(geometry=geometry),
color=alpha("white",0.5))+
binned_scale(
aesthetics = "color",
scale_name = "stepsn",
palette = function(x) pal,
breaks = seq(-50,50,20),
labels = glue::glue("{seq(-50,50,20)} min"),
show.limits = FALSE,
guide = guide_colorsteps(
nrow=1,
direction = "horizontal",
barheight = unit(4, units = "mm") ,
barwidth = unit(200, units = "mm"),
title.position = 'top',
label.position = "bottom"
)
)+
coord_sf(crs= "+proj=vandg4")+
labs(
title = "Right on time ?",
subtitle = "This map shows the **difference between local time and solar time** on Tuesday March 28, 2023",
color="Deviation from solar time",
caption="**Data** IANA tz database | **Plot** @BjnNowak")+
theme_void()+
theme(
plot.margin = margin(1,0,1,0,"cm"),
plot.title = element_text(
family = "pac",color="white",
size=90,hjust=0.5,face="bold",
margin=margin(0.5,0,0.5,0,"cm")
),
plot.subtitle = element_markdown(
family = "jo",color="white",
size=55,hjust=0.5
),
plot.background = element_rect(fill=col_back,color=NA),
legend.position = "bottom",
legend.title = element_text(
family = "ral",color="white",
size=60,hjust=0.5,face="bold",
margin = margin(0.5,0,-0.5,0,"cm"),
),
legend.text = element_text(
family = "jo",color="white",
size=40,margin=margin(-0.5,0,0,0,"cm")),
plot.caption= element_markdown(
family = "jo",color="white",
size=35,hjust=1,margin = margin(0.5,1,0,0,"cm"))
)