Catégories
R

Where and when (not) to eat in France ?

Results of sanitary controls in France can be found on data.gouv.fr however, only the running year is available… Thanks to @cquest@amicale.net we can access the archives since 2017.

First a global view of the dataset :

About 800 controls per week, except during the lock-down in 2020, and a slightly lower control pressure in 2021 and 2022.

Poor results (To be improved or To be corrected urgently) are stable at around 6 %, except a recent spike in 2023?

It seems that poor results increase during the year, from June to November.

This surprising periodic phenomenon is also visible by day :

So for the « when », it is : not in summer or autumn.

What about the « where »? It seems you also could be careful in some départements

Not good in Guadeloupe, Guyane, Réunion and the southern lower Seine valley, west of Paris.

Can we see more in details ? Using a 30 km kernel smoothing :

It confirms some hot-spots west of Paris, in Alsace, in Indre, Cher, Alpes-Maritimes and between Gironde and Landes. You are safer in Paris and Bretagne

Has it changed ?

No real trend…

Download the supporting data and R scripts :

Catégories
R

Cherry blossom

It’s cherry blossom time… A nice dataset going back to the year 812 in Japan can be found here. It describes the phenological data for full flowering date of cherry tree (Prunus jamasakura) in Kyoto, showing springtime climate changes.

Let’s draw…

# params ------------------------------------------------------------------

url_kyoto <- "http://atmenv.envi.osakafu-u.ac.jp/aono/kyophenotemp4/"
file_kyoto <- "kyoto.rds"   # cache data
icon_sakura <- "sakura.png" # cache icon


# config ------------------------------------------------------------------

library(tidyverse)
library(fs)
library(janitor)
library(httr)
library(rvest)
library(glue)
library(ggimage)

Sys.setlocale("LC_TIME", "en_GB.UTF-8")


# data --------------------------------------------------------------------

# icon
if (!file_exists(icon_sakura)) {
  GET("https://www.flaticon.com/download/icon/7096433?icon_id=7096433&author=232&team=232&keyword=Sakura&pack=packs%2Fsakura-festival-59&style=522&format=png&color=&colored=2&size=512&selection=1&premium=0&type=standard&search=cherry+blossom",
      write_disk(icon_sakura))
}

# blossom dates, scraped from the web page (more up to date than the xls files)
if (!file_exists(file_kyoto)) {
  GET(url_kyoto) |> 
    content() |> 
    html_element("pre") |>
    html_text2() |>  
    str_replace_all("\xc2\xa0", " ") |> # bad encoding needs correction
    read_fwf(fwf_cols("ad"   = c(7, 10), 
                      "fifd" = c(17, 20), 
                      "fufd" = c(22, 25), 
                      "work" = c(27, 30),
                      "type" = c(32, 35), 
                      "ref"  = c(37, Inf)),
             skip = 26,
             na = c("", "NA", "-")) |> 
    remove_empty() |>  
    mutate(full_flowering_date = ymd(glue("{str_pad(ad, 4, 'left', '0')}{fufd}")),
           full_flowering_date_doy = yday(full_flowering_date)) |> 
    write_rds(file_kyoto)
}


# plot --------------------------------------------------------------------

read_rds(file_kyoto) |> 
  mutate(random_size = sample(c(0.015, 0.02, 0.025, 0.03), length(ad), replace = TRUE)) |> 
  ggplot(aes(ad, parse_date_time(full_flowering_date_doy, orders = "j"))) +
  geom_smooth(color = NA, fill = "chocolate4", alpha = 0.5) +
  geom_image(aes(size = I(random_size)), image = icon_sakura) +
  geom_smooth(color = "chocolate3", se = FALSE, alpha = 0.5) +
  scale_y_datetime(labels = scales::date_format("%b %d"),
                   breaks = "weeks", minor_breaks = "days") +
  labs(title = "Cherry blossom",
       subtitle = "Kyoto",
       x = "year",
       y = "date",
       caption = glue("http://r.iresmi.net/ {Sys.Date()}
                      data: {url_kyoto}
                      icon by Vitaly Gorbachev")) +
  theme_minimal() +
  theme(plot.background = element_rect(fill = "cornflowerblue"),
        panel.grid = element_line(color = "dodgerblue"),
        text = element_text(color = "pink", family = "Ubuntu"),
        plot.title = element_text(size = 20),
        plot.caption = element_text(size = 7),
        axis.text = element_text(color = "pink"))

ggsave("cherry_blossom.png", width = 25, height = 18, units = "cm", dpi = 300)
Catégories
R

Open and merge multiple shapefiles, updated

This old post sees a little traffic from search engines but is a mess after many editions due to the packages evolutions.

So, how can we (chose your term) append, merge, union or combine many shapefiles or other spatial vector data in 2023 with R, preferably using tidyverse functions ?

For good measure, we want to add the source file as an attribute.

First, make some data using the geopackage available here. We generate several shapefiles (one per région) in a temporary directory :

dep <- sf::read_sf("~/data/adminexpress/adminexpress_cog_simpl_000_2022.gpkg", 
                   layer = "departement")

dep |> 
  dplyr::group_by(insee_reg) |> 
  dplyr::group_walk(\(grp_data, grp_name) sf::write_sf(grp_data, 
                                                       glue::glue("~/temp/reg_{grp_name}.shp")))

This is the way

Current and concise.

fs::dir_ls("~/temp", regexp = ".*\\.shp$") |> 
  purrr::map(\(f) sf::read_sf(f) |> 
               dplyr::mutate(source = f, .before = 1)) |> 
  dplyr::bind_rows()

Approved

Recommended, as seen in the purrr help on map_dfr (that I liked better, see below) but verbose because we have to specify that we want a sf-tibble which is lost in translation.

fs::dir_ls("~/temp", regexp = ".*\\.shp$") |> 
  purrr::map(\(f) sf::read_sf(f) |> 
               dplyr::mutate(source = f, .before = 1)) |> 
  purrr::list_rbind() |>
  dplyr::as_tibble() |> 
  sf::st_sf()

Superseded

… Sadly. That’s short and understandable.

fs::dir_ls("~/temp", regexp = ".*\\.shp$") |> 
  purrr::map_dfr(\(f) sf::read_sf(f) |> 
                   dplyr::mutate(source = f, .before = 1)) 

Older

I liked it, too.

fs::dir_ls("~/temp", regexp = ".*\\.shp$") |> 
  dplyr::tibble(source = _) |>
  dplyr::mutate(shp = purrr::map(source, sf::read_sf)) |>
  tidyr::unnest(shp) |>
  sf::st_sf()

Oldest

If all the files share the same attributes structure.

fs::dir_ls("~/temp", regexp = ".*\\.shp$") |> 
  purrr::map(\(f) sf::read_sf(f) |> 
               dplyr::mutate(source = f, .before = 1)) |> 
  do.call(what = rbind)

or

fs::dir_ls("~/temp", regexp = ".*\\.shp$") |> 
  purrr::map(\(f) sf::read_sf(f) |> 
               dplyr::mutate(source = f, .before = 1)) |> 
  purrr::reduce(rbind)