/
day10_raster.R
137 lines (122 loc) · 3.55 KB
/
day10_raster.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
## https://www.blopig.com/blog/2019/08/combining-inset-plots-with-facets-using-ggplot2/
library(geodata)
library(here)
library(stars)
library(rnaturalearth)
library(dplyr)
library(ggplot2)
library(purrr)
extrafont::loadfonts(device = "win")
ec = ne_countries(scale = "medium",
country = "Ecuador",
returnclass = "sf")
bbox_cont = ec %>%
st_cast("POLYGON") %>%
mutate(area = st_area(geometry)) %>%
top_n(1) %>%
st_buffer(100000) %>%
st_bbox()
bbox_gal = c(
xmin = -91.7,
xmax = -89.2,
ymin = -1.4,
ymax = 0.3
) %>% st_bbox()
tavg = worldclim_country(
"Ecuador", "tavg",
path = here("data/worldclim/")
)
tavg_stars = st_as_stars(tavg)
tavg_ec = tavg_stars[ec]
tavg_ec_named = tavg_ec %>%
st_set_dimensions(which = 'band',
values = c("JAN", "FEB", "MAR", "APR", "MAY",
"JUN", "JUL", "AUG", "SEP", "OCT",
"NOV", "DEC"))
main = ggplot() +
geom_stars(data = tavg_ec_named, downsample = c(0,0,0)) +
scico::scale_fill_scico(
"Monthly average temperature (°C) \n 1970-2000",
palette = "berlin",
n.breaks = 7,
na.value = "transparent",
guide = guide_colorbar(
title.position = "top", title.hjust = 0.5,
barheight = grid::unit(8, 'mm'),
barwidth = grid::unit(100, 'mm'),
ticks = FALSE
)
) +
coord_equal(
xlim = c(bbox_cont["xmin"], bbox_cont["xmax"]),
ylim = c(bbox_cont["ymin"], bbox_cont["ymax"]),
) +
facet_wrap(
~band, strip.position = "bottom"
) +
labs(
caption = "#30DayMapChallenge | Day 10: Raster | Data: WorldClim | Created by @loreabad6"
) +
theme_void() +
theme(
legend.position = "top",
legend.direction = "horizontal",
text = element_text(size = 13, family = "Roboto", color = "#501802"),
plot.background = element_rect(
fill = "#faecde", color = NA
),
plot.margin = margin(t = 3, b = 3, unit = "mm"),
plot.caption = element_text(
hjust = 0.5, size = 9,
margin = margin(t = 10, unit = "mm")
)
)
## A function to plot the inset
get_inset = function(stars, month){
p = ggplot() +
geom_stars(
data = stars[,,,month],
downsample = c(0,0,0),
show.legend = FALSE
) +
scico::scale_fill_scico(
"Average temperature (°C) \n 1970-2000",
palette = "berlin",
n.breaks = 7,
na.value = "transparent"
) +
coord_equal(
xlim = c(bbox_gal["xmin"], bbox_gal["xmax"]),
ylim = c(bbox_gal["ymin"], bbox_gal["ymax"]),
) +
# facet_wrap(~band) +
theme_void() +
theme(
legend.position = "bottom",
legend.direction = "horizontal",
plot.background = element_rect(color = 'grey90', fill = "transparent")
)
p
}
## This function allows us to specify which facet to annotate
annotation_custom2 <- function (grob, xmin = -Inf, xmax = Inf, ymin = -Inf, ymax = Inf, data)
{
layer(data = data, stat = StatIdentity, position = PositionIdentity,
geom = ggplot2:::GeomCustomAnn,
inherit.aes = TRUE, params = list(grob = grob,
xmin = xmin, xmax = xmax,
ymin = ymin, ymax = ymax))
}
insets = 1:12 %>%
purrr::map( ~annotation_custom2(
grob = ggplotGrob(get_inset(tavg_ec_named, .)),
data = data.frame(month = .),
ymin = bbox_cont['ymin'],
ymax = -2.5,
xmin = -77.5,
xmax = bbox_cont['xmax']
)
)
final = main + insets
ggsave(final, filename = "maps/day10.png",
height = 22, width = 21, units = "cm")