-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_carto.r
57 lines (48 loc) · 1.84 KB
/
quick_carto.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
rm(list=ls())
library(cartogram)
library(maptools)
library(tidyverse)
library(sf)
library(parallel)
library(Rcartogram)
library(getcartr)
library(rgeos)
library(rmapshaper)
setwd("~/projects/deep-sea fisheries/")
suppressWarnings(fao.data <- read_csv("200-400/data/fao-data-new.csv.gz"))
data("wrld_simpl")
world_map <- st_as_sf(wrld_simpl) %>%
st_transform(crs="+proj=eqc +ellps=WGS84 +datum=WGS84 +no_defs") %>%
filter(UN != 10 & UN != 260 & REGION != 10)# %>%
#st_as_sf(ms_simplify(as(.,'Spatial'),keep=0.3,keep_shapes=T))
world_map <- st_as_sf(ms_simplify(as(world_map,'Spatial'),keep=0.3,keep_shapes=T))
if (!exists("framesfile")) {
framesfile <- "cartogram_frames.rdata"
}
cat(paste("Saving to",framesfile))
#filter_year <- 1972
frames <- mclapply(seq(min(fao.data$year),max(fao.data$year),by=5),function(filter_year) {
cat(paste("this year:",filter_year,"\n"))
fish.year <- fao.data %>%
group_by(country.code,year) %>%
summarise(total.catch = sum(catch)) %>%
filter(year == filter_year) %>%
filter(country.code != '')
world_combined <-
left_join(world_map,fish.year, by=c("ISO3"="country.code")) %>%
mutate_if(is.numeric,funs(replace(.,is.na(.),0))) #%>%
#filter(UN != 10 & UN != 260 & REGION != 10) %>%
#st_transform(crs="+init=epsg:3857")
if (exists('filter_region')) {
world_combined <- world_combined %>% filter(REGION==filter_region)
}
if (exists('filter_subregion')) {
world_combined <- world_combined %>% filter(SUBREGION==filter_subregion)
}
fish.carto <- st_as_sf(quick.carto(as(world_combined,'Spatial'),world_combined$total.catch+1))
#ptm <- proc.time()
#fish.cartogram <- st_as_sf(cartogram::cartogram(as(world_combined,'Spatial'), "total.catch",itermax=iterations)) %>% mutate(year=year)
#proc.time() - ptm
(fish.carto)
},mc.preschedule=T, mc.cores=4)
save(frames,file=framesfile)