forked from BjnNowak/TidyTuesday
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSC_Locust.R
144 lines (124 loc) · 3.31 KB
/
SC_Locust.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
139
140
141
142
143
144
library(tidyverse)
library(camcorder)
library(patchwork)
library(showtext)
library(ggtext)
library(glue)
library(sf)
library(units)
library(lubridate)
library(ggbeeswarm)
# Set fonts
font_add_google("Fira Sans Extra Condensed","cond")
font_add_google("Fira Sans","fira")
font_add_google("Raleway","ral")
font_add_google("Bitter","bit")
showtext_auto()
# Plot size
gg_record(
dir = file.path(tempdir(),"recording"),
device = "png",
width = 32,
height = 16.6,
units = "cm",
dpi = 300
)
# Short function to create %!in% operator
'%!in%' <- function(x,y)!('%in%'(x,y))
# Load data
###########
# Data about locust:
# https://locust-hub-hqfao.hub.arcgis.com/
# Info :
# https://www.france24.com/fr/20200125-des-essaims-de-criquets-d-une-ampleur-historique-ravagent-de-larges-zones-d-afrique-de-l-est
data <- read_sf('Data/Locust/map/Swarm_Master_with_small_grid_id.shp')
grd <- read_sf('Data/Locust/map/grid_swarm_small.shp')
world_ne <- sf::read_sf("Data/Erosion/data/world_map/ne_110m_admin_0_countries_lakes.shp")
clean_whole<-data%>%
mutate(
yr=lubridate::year(data$STARTDATE),
ct=1
)%>%
group_by(id)%>%
summarize(
sm=sum(ct)
)
whole<-grd%>%
left_join(st_drop_geometry(clean_whole))
# Make graticule
grat <- sf::st_graticule(lat = c(-89.9, seq(-80, 80, 20), 89.9))
# Change crs
target_crs<-'+proj=eck4'
worldmap_trans <- st_transform(world_ne, crs = target_crs)
whole_trans <- st_transform(whole, crs = target_crs)
grat_trans <- st_transform(grat, crs = target_crs)
# bbox
disp_win_wgs84 <- st_sfc(
st_point(c(-38, -60)), st_point(c(120, 70)),
crs = 4326)
disp_win_trans <- st_transform(disp_win_wgs84, crs = target_crs)
disp_win_coord <- st_coordinates(disp_win_trans)
# Make plots
pal<-c(
"#FFEBF2",
"#FFADCA",
"#FF70A2",
"#FF337A",
"#F50056"
)
ggplot()+
geom_sf(
worldmap_trans,
mapping=aes(geometry=geometry),
fill="#073B4C",color=alpha("#EEE5E9",0.2))+
geom_sf(
whole_trans,
mapping=aes(geometry=geometry,fill=sm),
color=NA
)+
geom_sf(
grat_trans,
mapping=aes(geometry=geometry),
alpha=0.1,color="white")+
binned_scale(
aesthetics = "fill",
scale_name = "stepsn",
palette = function(x) pal,
breaks = c(50,150,250,350),
na.value = NA,
show.limits = FALSE,
guide = guide_colorsteps(
nrow=1,
direction = "horizontal",
barheight = unit(3, units = "mm") ,
barwidth = unit(30, units = "mm"),
title.position = 'top',
label.position = "bottom"
))+
coord_sf(
xlim = disp_win_coord[,'X'],
ylim = disp_win_coord[,'Y'],
datum = target_crs, expand = FALSE
)+
theme_void()+
theme(
legend.position = c(0.18,0.44),
legend.text = element_text(family="fira",color="white",size=20),
legend.title=element_blank(),
panel.background = element_rect(fill="#1D201F",color=NA)
)
res <- st_drop_geometry(data)%>%
mutate(
ct=1,
yr=lubridate::year(data$STARTDATE)
)%>%
group_by(yr)%>%
summarize(sm=sum(ct))
ggplot(res,aes(x=yr,y=sm))+
geom_glowline(
color="#F50056",shadowcolour="#FF70A2",
size=1.5)+
theme_void()+
theme(
#panel.background = element_rect(fill="#1D201F",color=NA)
)