library(lme4)
## Le chargement a nécessité le package : Matrix
library(multilevelTools)
library(lmerTest)
## 
## Attachement du package : 'lmerTest'
## L'objet suivant est masqué depuis 'package:lme4':
## 
##     lmer
## L'objet suivant est masqué depuis 'package:stats':
## 
##     step
library(sf) #si cela fonctionne pas télécharger libgdal-dev dans synaptic
## Linking to GEOS 3.8.0, GDAL 3.0.4, PROJ 6.3.1; sf_use_s2() is TRUE
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.2     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ purrr     1.0.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ tidyr::expand() masks Matrix::expand()
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ✖ tidyr::pack()   masks Matrix::pack()
## ✖ tidyr::unpack() masks Matrix::unpack()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(readxl)
library(ggrepel)
vignobles <- st_read("/home/lf/Nextcloud/Sauvegarde PC/Doctorat/Formations/QuantiLille_2023/Donnees_spat/geo/Vignobles_prix.shp") %>%
  st_transform(crs="EPSG:2154")
## Reading layer `Vignobles_prix' from data source 
##   `/home/lf/Nextcloud/Sauvegarde PC/Doctorat/Formations/QuantiLille_2023/Donnees_spat/geo/Vignobles_prix.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 2391 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 826631.6 ymin: 6645988 xmax: 851465.7 ymax: 6690810
## Projected CRS: RGF93 / Lambert-93
head(vignobles)
## Simple feature collection with 6 features and 8 fields
## Geometry type: MULTIPOLYGON
## Dimension:     XY
## Bounding box:  xmin: 829391.8 ymin: 6653883 xmax: 831448.4 ymax: 6655864
## Projected CRS: RGF93 / Lambert-93
##                            Concat         LIBCOM              NOM     NIVEAU
## 1          1AUXEY-DURESSESCORLOIN AUXEY-DURESSES          CORLOIN Coteaux b.
## 2 1AUXEY-DURESSESDERRIERE LE BOIS AUXEY-DURESSES DERRIERE LE BOIS Coteaux b.
## 3      1AUXEY-DURESSESEN CHAIGNUT AUXEY-DURESSES      EN CHAIGNUT Coteaux b.
## 4       1AUXEY-DURESSESEN FRIVOLE AUXEY-DURESSES       EN FRIVOLE Coteaux b.
## 5        1AUXEY-DURESSESEN MORVIN AUXEY-DURESSES        EN MORVIN Coteaux b.
## 6      1AUXEY-DURESSESEN POCHENOT AUXEY-DURESSES      EN POCHENOT Coteaux b.
##   SURFACE SCORE_b        Source PrixMoy                       geometry
## 1    0.22   67.66 interpolation      27 MULTIPOLYGON (((831448.4 66...
## 2    0.55   44.41 interpolation      36 MULTIPOLYGON (((831201.1 66...
## 3    0.96   56.02 interpolation      45 MULTIPOLYGON (((829782.7 66...
## 4    0.61   50.18 interpolation      48 MULTIPOLYGON (((829491.5 66...
## 5    0.18   55.29 interpolation      38 MULTIPOLYGON (((830567.3 66...
## 6    0.28   59.73 interpolation      43 MULTIPOLYGON (((830017.7 66...
summary(vignobles)
##     Concat             LIBCOM              NOM               NIVEAU         
##  Length:2391        Length:2391        Length:2391        Length:2391       
##  Class :character   Class :character   Class :character   Class :character  
##  Mode  :character   Mode  :character   Mode  :character   Mode  :character  
##                                                                             
##                                                                             
##                                                                             
##                                                                             
##     SURFACE          SCORE_b         Source             PrixMoy       
##  Min.   :0.0000   Min.   :18.40   Length:2391        Min.   :    5.0  
##  1st Qu.:0.1300   1st Qu.:67.90   Class :character   1st Qu.:   23.0  
##  Median :0.3100   Median :73.20   Mode  :character   Median :   36.0  
##  Mean   :0.4773   Mean   :71.89                      Mean   :  154.4  
##  3rd Qu.:0.6100   3rd Qu.:76.60                      3rd Qu.:   87.0  
##  Max.   :7.2000   Max.   :94.22                      Max.   :25960.0  
##                                                      NA's   :4        
##           geometry   
##  MULTIPOLYGON :2391  
##  epsg:2154    :   0  
##  +proj=lcc ...:   0  
##                      
##                      
##                      
## 
communes <- st_read("/home/lf/Nextcloud/Sauvegarde PC/Doctorat/Formations/QuantiLille_2023/Donnees_spat/geo/Communes.shp") %>%
  st_transform(crs="EPSG:2154")
## Reading layer `Communes' from data source 
##   `/home/lf/Nextcloud/Sauvegarde PC/Doctorat/Formations/QuantiLille_2023/Donnees_spat/geo/Communes.shp' 
##   using driver `ESRI Shapefile'
## Simple feature collection with 31 features and 20 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 826428 ymin: 6645899 xmax: 855115 ymax: 6691133
## Projected CRS: RGF93 / Lambert-93
head(communes)
## Simple feature collection with 6 features and 20 fields
## Geometry type: POLYGON
## Dimension:     XY
## Bounding box:  xmin: 831753 ymin: 6649806 xmax: 855115 ymax: 6688354
## Projected CRS: RGF93 / Lambert-93
##    gid id_geofla code_comm insee_com            nom_comm           statut
## 1  563       563       054     21054              BEAUNE  Sous-préfecture
## 2  573       573       267     21267    FLAGEY-ECHEZEAUX   Commune simple
## 3 2196      2196       295     21295   GEVREY-CHAMBERTIN Chef-lieu canton
## 4 2219      2219       464     21464 NUITS-SAINT-GEORGES Chef-lieu canton
## 5 2228      2228       512     21512  PULIGNY-MONTRACHET   Commune simple
## 6 6127      6127       200     21200             COUCHEY   Commune simple
##   x_chf_lieu y_chf_lieu x_centroid y_centroid z_moyen superficie population
## 1       8396      66598       8395      66599     252       3110       21.9
## 2       8500      66758       8501      66751     230        799        0.5
## 3       8488      66825       8500      66818     300       2503        3.3
## 4       8478      66726       8468      66735     320       2038        5.6
## 5       8334      66509       8338      66510     241        730        0.5
## 6       8498      66863       8482      66864     396       1258        1.2
##   code_cant code_arr code_dept  nom_dept code_reg nom_region          Cote
## 1        98        1        21 COTE-D'OR       26  BOURGOGNE Côte de Beane
## 2        24        1        21 COTE-D'OR       26  BOURGOGNE  Côte de Nuit
## 3        15        2        21 COTE-D'OR       26  BOURGOGNE  Côte de Nuit
## 4        24        1        21 COTE-D'OR       26  BOURGOGNE  Côte de Nuit
## 5        23        1        21 COTE-D'OR       26  BOURGOGNE Côte de Beane
## 6        15        2        21 COTE-D'OR       26  BOURGOGNE     Côte D'or
##                         geometry
## 1 POLYGON ((843586 6656790, 8...
## 2 POLYGON ((852627 6673525, 8...
## 3 POLYGON ((851046 6679694, 8...
## 4 POLYGON ((847977 6670152, 8...
## 5 POLYGON ((835887 6649806, 8...
## 6 POLYGON ((852913 6684159, 8...

Ici, on va faire la carte des vignobles.

ggplot(data = vignobles) + 
  geom_sf(aes(fill = log(PrixMoy)),#aes = esthétique du graph
          lwd=0.01, colour = "white")+
          scale_fill_viridis_c(option = "viridis")+
            labs(title = "Niveaux de prix des vignobles")

ggplot(data = vignobles)+
  geom_sf(aes(fill=SCORE_b),
          lwd=0.01, colour = "white")+
  scale_fill_viridis_c(option = "viridis")+
  labs(title = "niveaux de qualite")

ggplot(data = communes)+
  geom_sf()

vignobles_par_commune <- as_tibble(vignobles) %>%
  group_by(LIBCOM)%>%
  summarise(aire_vignobles = sum(SURFACE),
            MoyPond_b = sum(SCORE_b * SURFACE) / sum(SURFACE),
            N_vignobles = n(),
            Prix_moyen = mean(PrixMoy, na.rm=T))

vignobles_par_commune_par_AOC <- as_tibble(vignobles) %>%
  count(LIBCOM, NIVEAU) %>%
  pivot_wider(names_from = NIVEAU, values_from = n) %>%
  mutate_if(is.numeric,replace_na,0)