Je réalise quelques cartes de répartition d'espèces. J'ai un tableur avec mes données géolocalisées, ainsi que mes fichiers shapes.
A partir de cette table, j'ai extrait 3 "couches" en fonction d'une variable calculée d'après l'ancienneté de la donnée. Elle comporte les valeurs suivantes: "ANC", "REC" & "ACT".
Pour réaliser mes cartes, j'ai choisi d'utiliser la fonction geom_hex.
J'ai testé une première fois l'argument "bins". Tout semble fonctionner avec mon fichier de 30000 lignes.
Puis j'ai voulu paramétrer mes hexagones de façon à définir leur aire avec l'argument "binwidth". De là, plusieurs questions / problèmes.
- binwidth est définit comme étant un "Numeric vector giving bin width in both vertical and horizontal directions". Quelles sont exactement ces deux dimensions? Pour moi, dans binwidth = c(x, y), x = 2 apothèmes de l'hexagone régulier, et y = 2a, pour a la longueur du côté de l'hexagone. Mais je ne suis pas certain de mon coup.
Sinon, existe-t-il un autre moyen de définir l'aire des hexagones réguliers produits?
en utilisant l'argument "bins", les hexagones des trois couches ("ANC", "REC" & "ACT") se superposent parfaitement. Mais avec l'argument "binwidth", ce n'est pas le cas. Deux des trois couches semblent bien se superposer, mais la 3e est décalée sur les deux axes. Comment puis-je faire pour que les hexagones se superposent parfaitement entre les 3 couches?
enfin, avec l'exemple simplifié à l'extrême ci-dessous, plus rien de fonctionne: les hexagones sont de forme et de tailles complètement différentes.
voici mon code:
Code : Tout sélectionner
library(ggplot2)
library(hexbin)
library("rgdal")
data <- read.delim("data.txt")
FRA <- readOGR("gadm36_FRA_0.shp",layer="gadm36_FRA_0")
FRA_df <- fortify(FRA)
###définition des couches de points en fonctions de l’ancienneté de la donnée
df_layer_ANC <- data[data$SYNTH_PERIODE =="ANC",]
df_layer_REC <- data[data$SYNTH_PERIODE =="REC",]
df_layer_ACT <- data[data$SYNTH_PERIODE =="ACT",]
#bins
for(Var in unique(data$TAXO_STAND))
{
final.plot <- ggplot(subset(data, TAXO_STAND == Var), aes(x = X, y = Y)) +
coord_sf(xlim = c(75000, 1260000), ylim = c(6000000, 7150000), expand = FALSE) +
geom_polygon(data = FRA_df, aes(x = long, y = lat, group = group), fill='antiquewhite') +
geom_hex(data= subset(data[data$SYNTH_PERIODE =="ANC",], TAXO_STAND == Var), aes(x = X, y = Y), fill="yellow", color = 'darkgrey', bins=100) +
geom_hex(data= subset(data[data$SYNTH_PERIODE =="REC",], TAXO_STAND == Var), aes(x = X, y = Y), fill="orange", color = 'darkgrey', bins=100) +
geom_hex(data= subset(data[data$SYNTH_PERIODE =="ACT",], TAXO_STAND == Var), aes(x = X, y = Y), fill="#FF0000", color = 'darkgrey', bins=100) +
theme(axis.title.x = element_blank(),axis.text.x = element_blank(),axis.title.y = element_blank(),axis.text.y = element_blank(), panel.grid.minor.x=element_blank(),panel.grid.major.x=element_blank(),panel.grid.minor.y=element_blank(),panel.grid.major.y=element_blank())
ggsave(final.plot, file = paste0(Var, "_bins.png"), scale = 2)
print(final.plot)
}
#binwidth
for(Var in unique(data$TAXO_STAND))
{
final.plot <- ggplot(subset(data, TAXO_STAND == Var), aes(x = X, y = Y)) +
coord_sf(xlim = c(75000, 1260000), ylim = c(6000000, 7150000), expand = FALSE) +
geom_polygon(data = FRA_df, aes(x = long, y = lat, group = group), fill='antiquewhite') +
geom_hex(data= subset(data[data$SYNTH_PERIODE =="ANC",], TAXO_STAND == Var), aes(x = X, y = Y), fill="yellow", color = 'darkgrey', binwidth=c(24028.11,27745.28)) +
geom_hex(data= subset(data[data$SYNTH_PERIODE =="REC",], TAXO_STAND == Var), aes(x = X, y = Y), fill="orange", color = 'darkgrey', binwidth=c(24028.11,27745.28)) +
geom_hex(data= subset(data[data$SYNTH_PERIODE =="ACT",], TAXO_STAND == Var), aes(x = X, y = Y), fill="#FF0000", color = 'darkgrey', binwidth=c(24028.11,27745.28)) +
theme(axis.title.x = element_blank(),axis.text.x = element_blank(),axis.title.y = element_blank(),axis.text.y = element_blank(), panel.grid.minor.x=element_blank(),panel.grid.major.x=element_blank(),panel.grid.minor.y=element_blank(),panel.grid.major.y=element_blank())
ggsave(final.plot, file = paste0(Var, "_binwidth.png"), scale = 2)
print(final.plot)
}
Mes données (ultra simplifiées) sont structurées ainsi:
Code : Tout sélectionner
TAXO_STAND OBSE_DATE2 SYNTH_PERIODE X Y
SP1 29/05/2012 ACT 1203700 6172200
SP1 16/06/2012 ACT 296800 6754700
SP1 07/06/1997 REC 502500 6502400
SP1 04/06/2016 ACT 1165500 6133700
SP1 11/06/1935 ANC 661300 6894000
SP2 30/06/1999 ACT 318900 6741200
SP2 07/04/1990 REC 585500 6360300
SP2 NA REC 892530 6245660
SP2 26/05/1928 ANC 821900 6319800
SP2 NA ANC 847300 6526300
En voici la sortie de dput()
Code : Tout sélectionner
structure(list(TAXO_STAND = structure(c(1L, 1L, 1L, 1L, 1L, 2L,
2L, 2L, 2L, 2L), .Label = c("SP1", "SP2"), class = "factor"),
OBSE_DATE2 = structure(c(7L, 5L, 3L, 1L, 4L, 8L, 2L, NA,
6L, NA), .Label = c("04/06/2016", "07/04/1990", "07/06/1997",
"11/06/1935", "16/06/2012", "26/05/1928", "29/05/2012", "30/06/1999"
), class = "factor"), SYNTH_PERIODE = structure(c(1L, 1L,
3L, 1L, 2L, 1L, 3L, 3L, 2L, 2L), .Label = c("ACT", "ANC",
"REC"), class = "factor"), X = c(1203700L, 296800L, 502500L,
1165500L, 661300L, 318900L, 585500L, 892530L, 821900L, 847300L
), Y = c(6172200L, 6754700L, 6502400L, 6133700L, 6894000L,
6741200L, 6360300L, 6245660L, 6319800L, 6526300L)), class = "data.frame", row.names = c(NA,
-10L))
D'avance, merci pour votre aide.
Très bonne journée,
Mat'