################## Install packages #####################################
# install.packages("cartography")
# install.packages("readxl")
# install.packages("SpatialPosition")
# install.packages("mapview")






################## Import geo layer #####################################

# load libraries
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.1.2, proj.4 4.9.3
# import communes of Auvergne-Rhone-Alpes region
reg84 <- st_read(dsn = "data/communes_reg_84.geojson", stringsAsFactors = F)
## Reading layer `OGRGeoJSON' from data source `/home/tim/Documents/prz/tuto-mate/data/communes_reg_84.geojson' using driver `GeoJSON'
## Simple feature collection with 4141 features and 8 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 2.062908 ymin: 44.11565 xmax: 7.185633 ymax: 46.80403
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
# view information on the geo layer com
head(reg84)
## Simple feature collection with 6 features and 8 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 4.020999 ymin: 44.4428 xmax: 5.879937 ymax: 46.14185
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##   INSEE_COM                    NOM_COM SUPERFICIE POPULATION CODE_DEPT
## 1     38225 AUTRANS-MEAUDRE EN VERCORS       3371       2973        38
## 2     42070                   CORDELLE       2667        901        42
## 3     38342                   ROISSARD       1453        279        38
## 4     07110                    JOYEUSE       1300       1689        07
## 5     38489                     SIEVOZ        743        127        38
## 6     69242                    TAPONAS        758        945        69
##   NOM_DEPT CODE_REG              NOM_REG                       geometry
## 1    ISERE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((5.577223 45...
## 2    LOIRE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.059906 45...
## 3    ISERE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((5.638294 44...
## 4  ARDECHE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.23897 44....
## 5    ISERE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((5.849843 44...
## 6    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.754758 46...
# display the layer
plot(st_geometry(reg84))

# Extract the Rhone Departement 
dep69 <- reg84[reg84$CODE_DEPT==69,]
plot(st_geometry(dep69))

################## Import data #####################################

library(readxl)
# import data from an INSEE excel file 
csp <- read_excel("data/pop-act2554-csp-cd-6814.xls", 
                  sheet = "COM_2014", skip = 15)
head(csp)
## # A tibble: 6 x 18
##   RR    DR    CR    STABLE DR16  LIBELLE csx_rec1taxtypa… csx_rec1taxtypa…
##   <chr> <chr> <chr> <chr>  <chr> <chr>              <dbl>            <dbl>
## 1 84    01    001   1      01    Aberge…             5.00               0.
## 2 84    01    002   1      01    Aberge…             0.                 0.
## 3 84    01    003   0      01    Amarei…            NA                 NA 
## 4 84    01    004   1      01    Ambéri…             2.09               0.
## 5 84    01    005   1      01    Ambéri…             5.02               0.
## 6 84    01    006   1      01    Ambléon             0.                 0.
## # ... with 10 more variables: csx_rec2taxtypac_rec1rpop2014 <dbl>,
## #   csx_rec2taxtypac_rec2rpop2014 <dbl>,
## #   csx_rec3taxtypac_rec1rpop2014 <dbl>,
## #   csx_rec3taxtypac_rec2rpop2014 <dbl>,
## #   csx_rec4taxtypac_rec1rpop2014 <dbl>,
## #   csx_rec4taxtypac_rec2rpop2014 <dbl>,
## #   csx_rec5taxtypac_rec1rpop2014 <dbl>,
## #   csx_rec5taxtypac_rec2rpop2014 <dbl>,
## #   csx_rec6taxtypac_rec1rpop2014 <dbl>,
## #   csx_rec6taxtypac_rec2rpop2014 <dbl>
# select data for Rhone
csp69 <- data.frame(csp[csp$DR %in% 69,])
head(csp69)
##   RR DR  CR STABLE DR16           LIBELLE csx_rec1taxtypac_rec1rpop2014
## 1 84 69 001      1   69            Affoux                     12.069767
## 2 84 69 002      1   69        Aigueperse                     16.192771
## 3 84 69 003      1   69 Albigny-Sur-Saône                      4.982330
## 4 84 69 004      1   69              Alix                      7.963209
## 5 84 69 005      1   69         Ambérieux                      0.000000
## 6 84 69 006      1   69         Amplepuis                     55.434356
##   csx_rec1taxtypac_rec2rpop2014 csx_rec2taxtypac_rec1rpop2014
## 1                             0                     16.093023
## 2                             0                      8.096386
## 3                             0                     64.770286
## 4                             0                     11.944813
## 5                             0                     29.136531
## 6                             0                     98.927175
##   csx_rec2taxtypac_rec2rpop2014 csx_rec3taxtypac_rec1rpop2014
## 1                       0.00000                      12.06977
## 2                       0.00000                       0.00000
## 3                       4.98233                     209.25785
## 4                       0.00000                      27.87123
## 5                       0.00000                      20.81181
## 6                      11.89678                     106.84847
##   csx_rec3taxtypac_rec2rpop2014 csx_rec4taxtypac_rec1rpop2014
## 1                      0.000000                      20.11628
## 2                      0.000000                      12.14458
## 3                      9.964659                     209.25785
## 4                      0.000000                      71.66888
## 5                      0.000000                      83.24723
## 6                      3.975539                     352.42410
##   csx_rec4taxtypac_rec2rpop2014 csx_rec5taxtypac_rec1rpop2014
## 1                       0.00000                      48.27907
## 2                       0.00000                      20.24096
## 3                      14.94699                     209.25785
## 4                       0.00000                      63.70567
## 5                       0.00000                      45.78598
## 6                      35.69072                     348.48096
##   csx_rec5taxtypac_rec2rpop2014 csx_rec6taxtypac_rec1rpop2014
## 1                      4.023256                      32.18605
## 2                      4.048193                      28.33735
## 3                      9.964659                     109.61125
## 4                      7.963209                      51.76086
## 5                     16.649446                      37.46125
## 6                     59.365750                     474.98472
##   csx_rec6taxtypac_rec2rpop2014
## 1                      0.000000
## 2                      0.000000
## 3                      9.964659
## 4                      0.000000
## 5                      0.000000
## 6                     31.656059
# create INSEE_COM code
csp69$INSEE_COM <- paste0(csp69$DR, csp69$CR)

# suppress unused fields (the first 5 fields)
csp69 <- csp69[,-c(1:5)]

## ATTENTION ##
dep69[substr(dep69$NOM_COM, 1,4) =="LYON",  ] 
## Simple feature collection with 9 features and 8 fields
## geometry type:  MULTIPOLYGON
## dimension:      XY
## bbox:           xmin: 4.771712 ymin: 45.70731 xmax: 4.899021 ymax: 45.80818
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##      INSEE_COM                 NOM_COM SUPERFICIE POPULATION CODE_DEPT
## 1007     69384  LYON-4E-ARRONDISSEMENT        289      36336        69
## 1357     69386  LYON-6E-ARRONDISSEMENT        378      50364        69
## 1757     69382  LYON-2E-ARRONDISSEMENT        343      30804        69
## 2054     69388  LYON-8E-ARRONDISSEMENT        683      81531        69
## 2286     69389  LYON-9E-ARRONDISSEMENT        750      48926        69
## 2523     69387  LYON-7E-ARRONDISSEMENT        944      77855        69
## 2694     69385  LYON-5E-ARRONDISSEMENT        622      46630        69
## 2748     69381 LYON-1ER-ARRONDISSEMENT        153      29313        69
## 3363     69383  LYON-3E-ARRONDISSEMENT        632      98956        69
##      NOM_DEPT CODE_REG              NOM_REG                       geometry
## 1007    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.812872 45...
## 1357    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.839947 45...
## 1757    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.839947 45...
## 2054    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.849723 45...
## 2286    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.836936 45...
## 2523    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.849181 45...
## 2694    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.819694 45...
## 2748    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.819694 45...
## 3363    RHONE       84 AUVERGNE-RHONE-ALPES MULTIPOLYGON (((4.839947 45...
csp69[csp69$LIBELLE=="Lyon", ]
##     LIBELLE csx_rec1taxtypac_rec1rpop2014 csx_rec1taxtypac_rec2rpop2014
## 123    Lyon                      47.23109                      4.200817
##     csx_rec2taxtypac_rec1rpop2014 csx_rec2taxtypac_rec2rpop2014
## 123                      7860.952                      730.1241
##     csx_rec3taxtypac_rec1rpop2014 csx_rec3taxtypac_rec2rpop2014
## 123                      60758.81                      3232.642
##     csx_rec4taxtypac_rec1rpop2014 csx_rec4taxtypac_rec2rpop2014
## 123                      50614.36                      6667.904
##     csx_rec5taxtypac_rec1rpop2014 csx_rec5taxtypac_rec2rpop2014
## 123                      35653.98                      7277.054
##     csx_rec6taxtypac_rec1rpop2014 csx_rec6taxtypac_rec2rpop2014 INSEE_COM
## 123                      15633.92                      4697.168     69123
# aggregate Lyon arrondissements in dep69 into 1 commune
dep69[substr(dep69$NOM_COM, 1,4) =="LYON", "INSEE_COM" ] <- "69123"
dep69 <- aggregate(dep69, by=list(dep69$INSEE_COM), FUN = head, 1)
#st_geometry(dep69) <- st_cast(dep69$geometry, "MULTIPOLYGON")
dep69 <- dep69[,"INSEE_COM"]
plot(st_geometry(dep69))

# merge com layer and data
dep69 <- merge(dep69, csp69, by="INSEE_COM", all.x=TRUE)
head(dep69)
## Simple feature collection with 6 features and 14 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 4.24347 ymin: 45.82755 xmax: 4.837852 ymax: 46.30282
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##   INSEE_COM           LIBELLE csx_rec1taxtypac_rec1rpop2014
## 1     69001            Affoux                     12.069767
## 2     69002        Aigueperse                     16.192771
## 3     69003 Albigny-Sur-Saône                      4.982330
## 4     69004              Alix                      7.963209
## 5     69005         Ambérieux                      0.000000
## 6     69006         Amplepuis                     55.434356
##   csx_rec1taxtypac_rec2rpop2014 csx_rec2taxtypac_rec1rpop2014
## 1                             0                     16.093023
## 2                             0                      8.096386
## 3                             0                     64.770286
## 4                             0                     11.944813
## 5                             0                     29.136531
## 6                             0                     98.927175
##   csx_rec2taxtypac_rec2rpop2014 csx_rec3taxtypac_rec1rpop2014
## 1                       0.00000                      12.06977
## 2                       0.00000                       0.00000
## 3                       4.98233                     209.25785
## 4                       0.00000                      27.87123
## 5                       0.00000                      20.81181
## 6                      11.89678                     106.84847
##   csx_rec3taxtypac_rec2rpop2014 csx_rec4taxtypac_rec1rpop2014
## 1                      0.000000                      20.11628
## 2                      0.000000                      12.14458
## 3                      9.964659                     209.25785
## 4                      0.000000                      71.66888
## 5                      0.000000                      83.24723
## 6                      3.975539                     352.42410
##   csx_rec4taxtypac_rec2rpop2014 csx_rec5taxtypac_rec1rpop2014
## 1                       0.00000                      48.27907
## 2                       0.00000                      20.24096
## 3                      14.94699                     209.25785
## 4                       0.00000                      63.70567
## 5                       0.00000                      45.78598
## 6                      35.69072                     348.48096
##   csx_rec5taxtypac_rec2rpop2014 csx_rec6taxtypac_rec1rpop2014
## 1                      4.023256                      32.18605
## 2                      4.048193                      28.33735
## 3                      9.964659                     109.61125
## 4                      7.963209                      51.76086
## 5                     16.649446                      37.46125
## 6                     59.365750                     474.98472
##   csx_rec6taxtypac_rec2rpop2014                       geometry
## 1                      0.000000 POLYGON ((4.396142 45.85982...
## 2                      0.000000 POLYGON ((4.439457 46.29305...
## 3                      9.964659 POLYGON ((4.837265 45.85363...
## 4                      0.000000 POLYGON ((4.670139 45.90537...
## 5                      0.000000 POLYGON ((4.732623 45.92053...
## 6                     31.656059 POLYGON ((4.346022 45.92994...
# use friendly names
cs <- c("agr1", "agr0", "art1", "art0", 
        "cad1", "cad0", "int1","int0", 
        "emp1", "emp0","ouv1", "ouv0")
names(dep69)[3:14] <- cs
head(dep69)
## Simple feature collection with 6 features and 14 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 4.24347 ymin: 45.82755 xmax: 4.837852 ymax: 46.30282
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##   INSEE_COM           LIBELLE      agr1 agr0      art1     art0      cad1
## 1     69001            Affoux 12.069767    0 16.093023  0.00000  12.06977
## 2     69002        Aigueperse 16.192771    0  8.096386  0.00000   0.00000
## 3     69003 Albigny-Sur-Saône  4.982330    0 64.770286  4.98233 209.25785
## 4     69004              Alix  7.963209    0 11.944813  0.00000  27.87123
## 5     69005         Ambérieux  0.000000    0 29.136531  0.00000  20.81181
## 6     69006         Amplepuis 55.434356    0 98.927175 11.89678 106.84847
##       cad0      int1     int0      emp1      emp0      ouv1      ouv0
## 1 0.000000  20.11628  0.00000  48.27907  4.023256  32.18605  0.000000
## 2 0.000000  12.14458  0.00000  20.24096  4.048193  28.33735  0.000000
## 3 9.964659 209.25785 14.94699 209.25785  9.964659 109.61125  9.964659
## 4 0.000000  71.66888  0.00000  63.70567  7.963209  51.76086  0.000000
## 5 0.000000  83.24723  0.00000  45.78598 16.649446  37.46125  0.000000
## 6 3.975539 352.42410 35.69072 348.48096 59.365750 474.98472 31.656059
##                         geometry
## 1 POLYGON ((4.396142 45.85982...
## 2 POLYGON ((4.439457 46.29305...
## 3 POLYGON ((4.837265 45.85363...
## 4 POLYGON ((4.670139 45.90537...
## 5 POLYGON ((4.732623 45.92053...
## 6 POLYGON ((4.346022 45.92994...
for (i in 3:14){
  dep69[,i] <- round(dep69[,i, drop=T],0)
}


# agregate field (employed + unemployed)
dep69$agr <- dep69$agr0 + dep69$agr1
dep69$art <- dep69$art0 + dep69$art1
dep69$cad <- dep69$cad0 + dep69$cad1
dep69$int <- dep69$int0 + dep69$int1
dep69$emp <- dep69$emp0 + dep69$emp1
dep69$ouv <- dep69$ouv0 + dep69$ouv1
dep69$act <- dep69$agr + dep69$art + dep69$cad + 
  dep69$int + dep69$emp + dep69$ouv

head(dep69)
## Simple feature collection with 6 features and 21 fields
## geometry type:  POLYGON
## dimension:      XY
## bbox:           xmin: 4.24347 ymin: 45.82755 xmax: 4.837852 ymax: 46.30282
## epsg (SRID):    4326
## proj4string:    +proj=longlat +datum=WGS84 +no_defs
##   INSEE_COM           LIBELLE agr1 agr0 art1 art0 cad1 cad0 int1 int0 emp1
## 1     69001            Affoux   12    0   16    0   12    0   20    0   48
## 2     69002        Aigueperse   16    0    8    0    0    0   12    0   20
## 3     69003 Albigny-Sur-Saône    5    0   65    5  209   10  209   15  209
## 4     69004              Alix    8    0   12    0   28    0   72    0   64
## 5     69005         Ambérieux    0    0   29    0   21    0   83    0   46
## 6     69006         Amplepuis   55    0   99   12  107    4  352   36  348
##   emp0 ouv1 ouv0 agr art cad int emp ouv  act
## 1    4   32    0  12  16  12  20  52  32  144
## 2    4   28    0  16   8   0  12  24  28   88
## 3   10  110   10   5  70 219 224 219 120  857
## 4    8   52    0   8  12  28  72  72  52  244
## 5   17   37    0   0  29  21  83  63  37  233
## 6   59  475   32  55 111 111 388 407 507 1579
##                         geometry
## 1 POLYGON ((4.396142 45.85982...
## 2 POLYGON ((4.439457 46.29305...
## 3 POLYGON ((4.837265 45.85363...
## 4 POLYGON ((4.670139 45.90537...
## 5 POLYGON ((4.732623 45.92053...
## 6 POLYGON ((4.346022 45.92994...
################## Cartography #####################################
library(cartography)


# change projection (lambert93)
dep69 <- st_transform(dep69, 2154)
plot(st_geometry(dep69))

# Map of active population
plot(st_geometry(dep69), col="lightblue", border="ivory")
propSymbolsLayer(dep69, var = "act", col="red")
title("Population active")

# Custom map of active population
par(mar=c(0.2,0.2,1.4,0.2), bg="grey95")
plot(st_geometry(dep69), col="lightblue", border="ivory")
propSymbolsLayer(dep69, var = "act", col="darkblue", inches = 0.6, 
                 border = "white", lwd=0.7, symbols = "square",
                 legend.style = "e", legend.pos="topright",
                 legend.title.txt = "Nombre d'actifs\n(2014)", 
                 legend.values.rnd = -1)
barscale(size = 10)
north(pos = "topleft", col = "darkblue")
layoutLayer(title = "Population active dans le département du Rhône", 
            sources = "Insee, 2018", author = "cartography 2.0.2", 
            col = "darkblue", coltitle = "white", 
            frame = TRUE, scale = NULL, north = FALSE)

# get ideal output ratio for the dep69 layer
exp_dim <- getFigDim(x = dep69, width = 600, 
                     mar = c(0.2,0.2,1.4,0.2), res = 100)

png(filename = "output/pop_act.png",
    width = exp_dim[1], height = exp_dim[2], res = 100)
# Custom map of active population
par(mar=c(0.2,0.2,1.4,0.2), bg="grey95")
plot(st_geometry(dep69), col="lightblue", border="ivory")
propSymbolsLayer(dep69, var = "act", col="darkblue", inches = 0.6, 
                 border = "white", lwd=0.7, symbols = "square",
                 legend.style = "e", legend.pos="topright",
                 legend.title.txt = "Nombre d'actifs\n(2014)", 
                 legend.values.rnd = -1)
barscale(size = 10)
north(pos = "topleft", col = "darkblue")
layoutLayer(title = "Population active dans le département du Rhône", 
            sources = "Insee, 2018", author = "cartography 2.0.2", 
            col = "darkblue", coltitle = "white", 
            frame = TRUE, scale = NULL, north = FALSE)
dev.off()
## png 
##   2
## Compare two maps
# use fixmax param
par(mar=c(0,0,1.2,0), bg="grey95", mfrow=c(1,2))
plot(st_geometry(dep69), col="lightblue", border="ivory")
propSymbolsLayer(dep69, var = "cad", col="darkblue", 
                 inches = 0.4, fixmax=63991,
                 border = "white", lwd=0.5,
                 legend.pos="n")
north(pos = "topleft", col = "darkblue")
layoutLayer(title = "Cadres", 
            sources = "Insee, 2018", 
            author = "cartography 2.0.2", 
            col = "darkblue", coltitle = "white", 
            postitle = "center",
            frame = F, scale = NULL, north = FALSE)
plot(st_geometry(dep69), col="lightblue", border="ivory")
propSymbolsLayer(dep69, var = "ouv", col="darkred", 
                 inches = 0.4, fixmax=63991,
                 border = "white", lwd=0.7, 
                 legend.pos="n")
barscale(size = 10)
layoutLayer(title = "Ouvriers", 
            sources = "", author = "", 
            col = "darkred", coltitle = "white",  
            postitle = "center",
            frame = F, scale = NULL, north = FALSE)
# use custom legend
legendCirclesSymbols(pos = "topright", 
                     title.txt = "Nombre d'actifs\n(2014)", 
                     var = c(10,10000,25000,64000),
                     inches = 0.4, col = "lightgrey")

## explore a map interactively
library(mapview)
mapview(dep69) 
# add a few toponymes
labelLayer(x = dep69[dep69$INSEE_COM %in% c("69091",
                                            "69243", 
                                            "69256",
                                            "69264"),], 
           txt = "LIBELLE", 
           pos=3, 
           halo=TRUE)

# Carte de Typologie
par(mar=c(0,0,1.2,0), bg="grey95", mfrow=c(1,1))
dep69$dom <- "Indéterminé"
dep69$r <- dep69$ouv / dep69$cad
dep69$r[is.na(dep69$r)] <- 0
dep69[dep69$r > 1.1,"dom"] <- "Plus d'ouvriers que de cadres"
dep69[dep69$r < 0.91,"dom"] <- "Plus de cadres que d'ouvriers"

typoLayer(dep69, var="dom", 
          legend.values.order = c("Plus d'ouvriers que de cadres", 
                                  "Plus de cadres que d'ouvriers",
                                  "Indéterminé" ),
          col = c('darkred', 'darkblue', 'grey'), 
          border = "white", lwd=0.5, 
          legend.title.txt = "Type dominant") 
layoutLayer(title = "Ouvriers vs. Cadres", 
            sources = "", author = "", 
            col = "darkred", coltitle = "white",  
            postitle = "center",
            frame = F, scale = NULL, north = FALSE)      

par(mar=c(0,0,1.2,0), bg="grey95", mfrow=c(1,1))
plot(st_geometry(dep69), col="lightblue", border="ivory")
propSymbolsTypoLayer(dep69, var = "act",var2="dom", 
                     legend.var2.values.order = c("Plus d'ouvriers que de cadres", 
                                                  "Plus de cadres que d'ouvriers",
                                                  "Indéterminé" ),
                     col = c('darkred', 'darkblue', 'grey'), 
                     inches = 0.45,
                     legend.var.title.txt = "Nb. actifs",
                     legend.var.pos = "right",
                     border = "white", lwd=0.8, 
                     legend.var2.title.txt = "Type dominant") 
layoutLayer(title = "Ouvriers vs. Cadres", 
            sources = "", author = "", 
            col = "darkred", coltitle = "white",  
            postitle = "center",
            frame = F, scale = NULL, north = FALSE)

# Cartes choroplèthes
dep69$pcad <- 100 * dep69$cad / dep69$act
summary(dep69$pcad)
##    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
##   0.000   9.639  15.504  17.205  23.939  42.151
par(mfrow=c(1,1), mar=c(4,4,4,4))
boxplot(dep69$pcad)

hist(dep69$pcad, 20)
rug(dep69$pcad)

bks <- getBreaks(v = dep69$pcad, method = "q6")
cols <- carto.pal("green.pal", 3,"wine.pal",3)
par(mar = c(0.2,0.2,1.4,0.2), bg="grey95", mfrow=c(1,1))
choroLayer(dep69, var = "pcad", breaks = bks, 
           col = cols, border = "grey80", 
           lwd = 0.4, legend.pos = "topright", 
           legend.title.txt = "Part des cadres\n(en %)")
layoutLayer(title = "Les cadres", 
            sources = "", author = "", theme = "green.pal", 
            col = "darkred", coltitle = "white", 
            postitle = "center",
            frame = TRUE, scale = 10)
north(pos = "topleft", south = TRUE)

# Grid map
grid <- getGridLayer(x = dep69, cellsize = 3000*3000, 
                     type = "regular", var = c('cad', 'act'))
grid$pcad <- 100*grid$cad/grid$act
par(mar=c(0.2,0.2,1.4,0.2), bg="grey95", mfrow=c(1,1))
choroLayer(grid, var = "pcad", breaks=bks, 
           col = cols, border = "grey80", 
           lwd = 0.4, legend.pos = "topright", 
           legend.title.txt = "Part des cadres\n(en %)")
layoutLayer(title = "Les cadres", 
            sources = "", author = "", theme = "green.pal", 
            col = "darkred", coltitle = "white", 
            postitle = "center",
            frame = TRUE, scale = 10)
north(pos = "topleft", south = TRUE)

# Smooth Map
grid$cad100 <- grid$cad * 100
smoothLayer(x = grid, var = "cad100", var2 = "act", 
            typefct = "exponential", 
            span = 2500, beta = 2, breaks = bks, col = cols,
            legend.pos = "topright", mask =dep69,
            legend.title.txt = "Part des cadres\n(en %)", 
            border = "grey90", lwd = 0.2)
north(pos = "topleft", south = TRUE)
layoutLayer(title = "Les cadres", 
            sources = "", author = "", theme = "green.pal", 
            col = "darkred", coltitle = "white",  
            postitle = "center",
            frame = TRUE, scale = 10)
text(x = 867000, y = 6490000, 
     labels = "Lissage par potentiels\n fonction exponnentielle\n span = 2.5km, beta = 2", 
     font = 3, cex = 0.8)