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
| library(viridis) # nice color palette
library(ggplot2) # plotting
library(ggmap) # ggplot functionality for maps
library(dplyr) # use for fixing up data
library(readxl) # reading in data/csv
library(RColorBrewer) # for color palettes
library(purrr) # for mapping over a function
library(magick) # this is call to animate/read pngsBackground
Rpop <- read_excel("E:/Stage/final/Rpop.xlsx",
na = "NA")
## Set the Scale
breaks<-(c(17000,20000,30000,40000,50000,60000,70000,80000,90000))
# Get the basemap
ca <- get_map( c(lon= 9.87391,lat= 37.27442),
zoom=9,crop=T,
scale="auto",color="bw",source="google",
maptype="terrain")
gg <- ggmap(ca, extent='panel',padding=0)
gg
map_pop <- function(Yr){
gg + geom_point(data=Rpop[Rpop$année==Yr,],
aes(x=Rpop$longitude, y=Rpop$latitude, fill=Rpop$populatio),
show.legend=F, pch=21, size=4.8, color="gray30")+
theme_bw() + ylab("Latitude") + xlab("Longitude") +
theme(axis.text.x = element_text(angle = 60, vjust=0.15, size=8),
legend.position=c(1,1),legend.justification=c(1,1),
legend.direction="vertical",legend.text=element_text(size=8),
legend.title=element_text(size=8, face="bold"),
legend.box="horizontal", panel.background = element_blank(),
legend.box.just = c("top"),
legend.background = element_rect(fill=alpha('white', 0.6),
colour = "gray30")) +
scale_fill_viridis(name="POP", limits=range(breaks),
breaks=breaks, option = "D", direction = -1)+
facet_wrap(~Rpop$année, ncol = 1)
print(paste0("saving plot ", Yr))
ggsave(filename = paste0("E:/Stage/travail/lol",Yr,".png"),
width = 8,height=8,dpi = 150)
}
seq(from = 2004, to=2015, by=1) %>%
map_df(map_pop)
# Step 2: List those Plots, Read them in, and then make animation
list.files(path = "E:/Stage/travail/img", pattern = "*.png", full.names = T) %>%
map(image_read) %>% # reads each path file
image_join() %>% # joins image
image_animate(fps=2) %>% # animates, can opt for number of loops
image_write("ndwi_aug_hgm.gif") # write to current dir |
Partager