Skip to content

Commit

Permalink
construction figure interactive AAC data per region
Browse files Browse the repository at this point in the history
  • Loading branch information
Sckende committed Nov 29, 2024
1 parent febdf1b commit 7e8f23f
Showing 1 changed file with 90 additions and 1 deletion.
91 changes: 90 additions & 1 deletion AAC_per_region.r
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
library(stringr)
library(ggplot2)
library(dplyr)

sup_qc <- 1542056 # km2 from wikipedia
qc <- st_read("/home/local/USHERBROOKE/juhc3201/BDQC-GEOBON/data/QUEBEC_regions/sf_CERQ_SHP/QUEBEC_CR_NIV_01.gpkg")
dt <- read.table("/home/local/USHERBROOKE/juhc3201/BDQC-GEOBON/data/g15_indicators/results/AAC/AAC_final_per_region_land_use.txt")
head(dt)
info_ls <- strsplit(dt$info, "_")
Expand Down Expand Up @@ -54,6 +57,15 @@ dt3$class2[is.na(dt3$class2)] <- "Naturel"
df_colors <- data.frame(class = unique(dt3$class), color = c("brown2", "chocolate4", "deepskyblue3", "darkolivegreen", "aquamarine4"))
df_colors2 <- data.frame(class2 = unique(dt3$class2), color = c("brown2", "aquamarine4"))

qc_small <- as.data.frame(qc[, c("FID01", "NOM_PROV_N")])
qc_small <- qc_small[, c(1, 2)]
dt3 <- left_join(dt2, qc_small, by = join_by("reg" == "NOM_PROV_N"))
dt3 <- dt3[!is.na(dt3$count), ]

area_tot <- dt3 |>
group_by(year) |>
summarise(pix_tot = sum(count), area_tot_km2 = sum(count) * 30 * 30 / 1000000)

# ----- #
# Viz per region per class1

Expand All @@ -62,10 +74,13 @@ length(dt_reg) # 20 regions

reg_cl1 <- lapply(dt_reg, function(x) {
reg <- unique(x$reg)

j <- x |>
group_by(class, year) |>
summarise(sum = sum(count))
j$reg <- reg
j$area_km2 <- (j$sum * 30 * 30) / 1000000
j$prop <- (j$area_km2 / sup_qc) * 100
j <- left_join(j, df_colors, c("class"))
j
})
Expand All @@ -76,7 +91,8 @@ reg_cl2 <- lapply(dt_reg, function(x) {
group_by(class2, year) |>
summarise(sum = sum(count))
j$reg <- reg
j$year <- as.numeric(j$year)
j$area_km2 <- (j$sum * 30 * 30) / 1000000
j$prop <- (j$area_km2 / sup_qc) * 100
j <- left_join(j, df_colors2, c("class2"))
j
})
Expand Down Expand Up @@ -134,3 +150,76 @@ lapply(reg_cl2, function(x) {
)
}
})

#### Graphic par region ####
# ----------------------- #

d <- reg_cl1[[5]]
ggplot(d, aes(x = as.numeric(year), y = prop, color = class)) +
geom_line()


#### Viz interactive ####
# -------------------- #

library(lubridate)
library(ggplot2)
library(plotly)
library(sf)
library(terra)
st_read("/home/local/USHERBROOKE/juhc3201/BDQC-GEOBON/data/QUEBEC_regions/sf_CERQ_SHP/QUEBEC_CR_NIV_01.gpkg")
plot(st_geometry(qc))
qc_ll <- st_transform(qc, st_crs("EPSG:4326"))

# p <- ggplot(qc) +
# geom_sf()
# p
# ggplotly(p) %>%
# highlight(
# "plotly_hover",
# selected = attrs_selected(line = list(color = "black"))
# ) %>%
# widgetframe::frameWidget()

library(leaflet)
library(leafpop)
img <- "/home/claire/Pictures/Screenshots/test.png"
img2 <- "https://upload.wikimedia.org/wikipedia/commons/thumb/6/62/Mount_Eden.jpg/640px-Mount_Eden.jpg"
img3 <- "/home/claire/Desktop/fox.jpg"
img4 <- "https://miro.medium.com/v2/resize:fit:1400/1*jj818i6pGhnuWjwGOi8v8g.jpeg"
img5 <- "https://object-arbutus.cloud.computecanada.ca/bq-io/acer/ebv/rs_ebird.tif"

qc_ll$graph <- rep(c("https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Vulpes_vulpes_ssp_fulvus.jpg/800px-Vulpes_vulpes_ssp_fulvus.jpg", "https://shop.wwf.ca/cdn/shop/files/Dmitry-Deshevykh-WWF-Russia_red_fox.jpg?v=1694548960&width=1024", "https://naturecanada.ca/wp-content/uploads/2023/04/Red_Foxes_1980x1080.jpg", "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSyyCOQt9SxqPW_DEGrx2jfxolcRv_xSmeOy6GXrOhlTvJAr9f6R3sawaX-bwNLlaiNfpk&usqp=CAU"), 5)
p <- ggplot(data = meuse, aes(x = copper, y = cadmium, fill = as.factor(round(elev)))) +
geom_point()
p

leaflet(qc_ll) %>%
addTiles() %>%
fitBounds(
lng1 = -79.76332, # st_bbox(qc)[1],
lat1 = 44.99136, # st_bbox(qc)[2],
lng2 = -56.93521, # st_bbox(qc)[3],
lat2 = 62.58192 # st_bbox(qc)[4]
) %>%
addPolygons(
fillColor = "grey",
weight = 1,
opacity = 1,
color = "white",
dashArray = "3",
fillOpacity = 0.7,
layerId = qc_ll$NOM_PROV_N,
popup = popupImage(qc_ll$graph),
# popup = popupImage("/home/local/USHERBROOKE/juhc3201/Pictures/arctic_fox.jpg"),
# popup = popupGraph(p),
highlightOptions = highlightOptions(
weight = 5,
color = "#666",
dashArray = "",
fillOpacity = 0.7,
bringToFront = TRUE
)
)

# Remplacer foxes par lien local des graphes qui seront enregistres dans une colonne du dataframe final

0 comments on commit 7e8f23f

Please sign in to comment.