library(here)
library(tidyverse)
library(janitor)
library(countrycode)
library(huxtable)
Charger les principaux packages
Récupérer les données
Astuce
Pour récupérer des données sur les pays européens, le package eurostat est votre ami. Parcourez le catalogue avec la fonction eurostat::get_eurostat_toc()
= here("data", "crim_just_job.rds") crim_just_job_path
::get_eurostat("crim_just_job", type = "label", cache_dir = here("data")) |>
eurostatwrite_rds(crim_just_job_path)
= read_rds(crim_just_job_path) crim_just_job
Les inspecter
slice_sample(crim_just_job, n = 20) |>
as_huxtable() |>
set_number_format(NA) |>
::theme_grey() huxtable
isco08 | sex | unit | geo | time | values |
Personnel in juvenile prison | Males | Number | Hungary | 2010-01-01 | 285 |
Personnel in adult prison | Total | Number | Portugal | 2016-01-01 | 4971 |
Personnel in adult prison | Females | Per hundred thousand inhabitants | Austria | 2013-01-01 | 21.6 |
Professional judges | Total | Per hundred thousand inhabitants | Switzerland | 2018-01-01 | 14.43 |
Personnel in adult prison | Males | Number | Germany | 2016-01-01 | 26201 |
Professional judges | Females | Per hundred thousand inhabitants | Kosovo (under United Nations Security Council Resolution 1244/99) | 2008-01-01 | 7.51 |
Personnel in adult prison | Males | Number | Montenegro | 2009-01-01 | 394 |
Personnel in adult prison | Total | Per hundred thousand inhabitants | Czechia | 2009-01-01 | 102.65 |
Personnel in adult prison | Females | Number | Denmark | 2013-01-01 | 2281 |
Personnel in adult prison | Total | Number | Albania | 2019-01-01 | 4223 |
Personnel in adult prison | Total | Number | Croatia | 2014-01-01 | 2694 |
Police officers | Females | Number | Estonia | 2016-01-01 | 1377 |
Personnel in adult prison | Total | Number | Albania | 2009-01-01 | 3843 |
Police officers | Males | Per hundred thousand inhabitants | Poland | 2017-01-01 | 451.24 |
Personnel in adult prison | Males | Number | Czechia | 2015-01-01 | 8224 |
Police officers | Females | Number | Cyprus | 2017-01-01 | 1273 |
Personnel in adult prison | Males | Per hundred thousand inhabitants | Montenegro | 2012-01-01 | 130.24 |
Personnel in adult prison | Males | Per hundred thousand inhabitants | Netherlands | 2020-01-01 | 90.12 |
Professional judges | Total | Per hundred thousand inhabitants | Serbia | 2020-01-01 | 38.24 |
Personnel in adult prison | Males | Number | Kosovo (under United Nations Security Council Resolution 1244/99) | 2014-01-01 | 629 |
Les recoder
Astuce
Dès que vous devez manipuler des noms et codes de pays, pensez au package countrycode.
= crim_just_job |>
police_europe pivot_wider(names_from = c(sex, isco08, unit), values_from = "values",
names_repair = make_clean_names) |>
complete(geo, time) |>
mutate(geo = if_else(geo %in% c("Scotland", "England and Wales", "Northern Ireland (UK)"),
"United Kingdom", geo)) |>
group_by(geo, time) |>
summarise(across(ends_with("number"), ~ sum(.x, na.rm = F)), .groups = "drop") |>
mutate(geo = str_replace(geo, "Türkiye", "Turkey"),
geo = if_else(str_detect(geo, "Germany"), "Germany", geo),
geo = if_else(str_detect(geo, "Kosovo"), "Republic of Kosovo", geo),
year = year(time),
wb = countrycode(geo, origin = "country.name", destination = "wb")) |>
select(wb, year,
ends_with("number") &
contains("police"))
Maintenant elles ressemblent à ceci :
slice_head(police_europe, n = 20) |>
as_huxtable() |>
set_number_format(NA) |>
::theme_bright() huxtable
wb | year | females_police_officers_number | males_police_officers_number | total_police_officers_number |
ALB | 2008 | 799 | 8789 | 9588 |
ALB | 2009 | 810 | 8419 | 9229 |
ALB | 2010 | 882 | 8788 | 9670 |
ALB | 2011 | 936 | 8787 | 9723 |
ALB | 2012 | 1018 | 8710 | 9728 |
ALB | 2013 | 1010 | 8467 | 9477 |
ALB | 2014 | 1018 | 8607 | 9625 |
ALB | 2015 | 945 | 8798 | 9743 |
ALB | 2016 | 1489 | 9066 | 10555 |
ALB | 2017 | 1513 | 9138 | 10651 |
ALB | 2018 | 1508 | 9064 | 10572 |
ALB | 2019 | 1512 | 8922 | 10434 |
ALB | 2020 | 1566 | 9283 | 10849 |
ALB | 2021 | 1562 | 9356 | 10918 |
AUT | 2008 | 2922 | 23701 | 26623 |
AUT | 2009 | 3167 | 23456 | 26623 |
AUT | 2010 | 3599 | 23939 | 27538 |
AUT | 2011 | 3507 | 24107 | 27614 |
AUT | 2012 | 3695 | 24072 | 27767 |
AUT | 2013 | 4039 | 23744 | 27783 |
Calculer le nombre de policiers par habitant
Ce calcul a déjà été effectué par les auteurs de la base téléchargée. Toutefois, un examen attentif des données montre quelques choix un peu curieux, et nous incite à le refaire nous-même, par précaution.
Astuce
Le package wbstats donne accès aux données de la Banque mondiale, parmi lesquelles le nombre d’habitant, le PIB par tête, etc.
= here("data", "wb.rds") wb_path
::wb_data(country = "countries_only",
wbstatsindicator = c("gdp_per_capita" = "NY.GDP.PCAP.CD",
"population" = "SP.POP.TOTL"),
mrv = 7,
gapfill = TRUE) |>
write_rds(wb_path)
= read_rds(wb_path) |>
wb mutate(country = str_replace(country, "Turkiye", "Turkey"),
wb = countrycode(country, origin = "country.name", destination = "wb")) %>%
rename(year = date) %>%
select(-iso2c, -iso3c, -country)
= wb |>
police_europe_pcm left_join(police_europe) |>
mutate(policiers_pcm = total_police_officers_number / population * 100000) |>
group_by(wb) |>
summarise(policiers_pcm = mean(policiers_pcm, na.rm = TRUE)) |>
filter(!is.nan(policiers_pcm)) |>
mutate(pays = countrycode(wb, origin = "wb", destination = "country.name.fr"),
pays = if_else(wb == "MNE", "Monténégro", pays),
pays = if_else(wb == "MKD", "Macédoine du Nord", pays),
policiers_pcm = round(policiers_pcm)) |>
arrange(desc(policiers_pcm))
Palmarès
Tableau
|>
police_europe_pcm select(pays, policiers_pcm) |>
rename(Pays = pays,
"Policiers pour 100 000 habitants" = policiers_pcm) |>
as_huxtable() |>
set_number_format(NA) |>
::theme_blue() huxtable
Pays | Policiers pour 100 000 habitants |
Monténégro | 680 |
Turquie | 530 |
Grèce | 506 |
Croatie | 504 |
Kosovo | 495 |
Macédoine du Nord | 467 |
Bosnie-Herzégovine | 464 |
Malte | 457 |
Portugal | 446 |
Italie | 422 |
Bulgarie | 415 |
Chypre | 401 |
Slovaquie | 397 |
Hongrie | 392 |
Tchéquie | 379 |
Albanie | 374 |
Espagne | 366 |
Irlande | 351 |
Autriche | 346 |
Slovénie | 345 |
Belgique | 339 |
France | 321 |
Luxembourg | 321 |
Liechtenstein | 319 |
Lettonie | 301 |
Allemagne | 299 |
Estonie | 299 |
Pays-Bas | 293 |
Lituanie | 287 |
Pologne | 261 |
Roumanie | 252 |
Royaume-Uni | 224 |
Suède | 221 |
Suisse | 217 |
Danemark | 191 |
Islande | 180 |
Finlande | 135 |
Graphique
|>
police_europe_pcm mutate(pays = fct_reorder(pays, policiers_pcm)) |>
ggplot(aes(x = policiers_pcm, y = pays)) +
geom_bar(stat = "identity") +
labs(x = "Policiers pour 100 000 habitants",
y = "",
title = "Les Européens mieux protégés (?) au Sud et à l'Est") +
theme_minimal()
Citation
BibTeX
@online{boulakia2023,
author = {Boulakia, Théo and Mariot, Nicolas},
title = {Où y a-t-il le plus de policiers en Europe\,?},
date = {2023-06-25},
url = {https://l-attestation.github.io/exercices/policiers-europe/},
langid = {fr}
}
Veuillez citer ce travail comme suit :
Boulakia, Théo, and Nicolas Mariot. 2023. “Où y a-t-il le plus de
policiers en Europe ?” June 25, 2023. https://l-attestation.github.io/exercices/policiers-europe/.