-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGGANIMATE_MAPAS.R
133 lines (86 loc) · 4.01 KB
/
GGANIMATE_MAPAS.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
library(OpenStreetMap)
library(here)
source('libraries.R')
#CONFIG
DOWNLOAD_OS_MAPS<- FALSE
#READ DATA
x<- here::here('Data/Espana/20190620/Espana_20190620.RDS') %>%
readRDS() %>% lapply(function(y) y$Variable) %>% bind_rows(.id = "Date")
w<- x$lon %>% range() %>% .[2]
e<- x$lon %>% range() %>% .[1]
n<- x$lat %>% range() %>% .[2]
s<- x$lat %>% range() %>% .[1]
ul <- round(c(n,w),digits = 3) #Upper Left
lr <- round(c(s,e), digits = 3) #Lower Right
# Descargar y guardar mapas -----------------------------------------------
#Se puede seleccionar el tipo de mapa a descargar
# si no pones nada descarga todos los mapas disponibles
#Se puede cambiar la resolución, pero esta por defecto en
# 40 numtiles
if(DOWNLOAD_OS_MAPS){
download_maps(ul,lr, res=40)
}
# Plotear mapas -----------------------------------------------------------
map.latlon<- list.dirs(here::here('Mapas/'), recursive = F) %>% list.files(full.names = T) %>%
.[str_detect(.,".RDS")] %>% .[2] %>%
readRDS()
x1<- x %>% group_split(lon, lat) %>% lapply(function(y) {
y$RAINC<- c(0,diff(y$RAINC) )
return(y)}) %>% bind_rows()
x1$RAINC<- ifelse(x1$RAINC==0, NA, x1$RAINC)
x1<- x1[complete.cases(x1),]
x1$Date<- ifelse(nchar(x1$Date)<11, paste(x1$Date, "00:00:00"), x1$Date) %>% ymd_hms()
library(gganimate)
pmap2<-autoplot(map.latlon)+
geom_point(data= x1,
aes(x=lon, y=lat, color= RAINC),
alpha= 0.2,size= 3, shape=15) +
scale_color_gradientn(colours = c("blue", "darkgreen","red"),
na.value = NA,
breaks= seq(min(x1$RAINC,
max(x1$RAINC,
8)))) +
theme_bw() +
coord_fixed(1.3)+
theme(axis.line=element_blank(),axis.text.x=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),legend.position="none",
panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),plot.background=element_blank())+
labs(title = 'Date: {frame_time}')+
transition_time(Date)
library(gifski)
animate(pmap2,renderer = gifski_renderer(), fps = 2, nframes = nrow(x1 %>%
group_split(lon,lat) %>%
.[[1]] %>%
nrow(.)))
ul <- round(c(43,-8),digits = 3) #Upper Left
lr <- round(c(40,-2), digits = 3) #Lower Right
# Descargar y guardar mapas -----------------------------------------------
#Se puede seleccionar el tipo de mapa a descargar
#download_maps(ul,lr, res=40)
x2<- x1 %>% group_split(Date) %>% .[[19]] %>% filter(lon > ul[2],
lon < lr[2],
lat > lr[1],
lat < ul[1],)
map.latlon<- list.dirs(here::here('Mapas/'), recursive = F) %>% list.files(full.names = T) %>%
.[str_detect(.,".RDS")] %>% .[1] %>%
readRDS()
autoplot(map.latlon)+
geom_point(data= x2,
aes(x=lon, y=lat, color= RAINC),
alpha= 0.4,size= 9, shape=16) +
scale_color_gradientn(colours = c("blue", "darkgreen","red"),
na.value = NA,
breaks= seq(min(x1$RAINC,
max(x1$RAINC,
8)))) +
theme_bw() +
coord_fixed(1.3)+
theme(axis.line=element_blank(),axis.text.x=element_blank(),
axis.text.y=element_blank(),axis.ticks=element_blank(),
axis.title.x=element_blank(),
axis.title.y=element_blank(),legend.position="none",
panel.background=element_blank(),panel.border=element_blank(),panel.grid.major=element_blank(),
panel.grid.minor=element_blank(),plot.background=element_blank())