Pour le #Tidytuesday de cette semaine, nous avons accès aux données des contrôles routiers qui sont fait aux États-Unis. Lors d’une journée type, les policiers peuvent effectuer plus de 50 000 contrôles routiers, mettez tout ça dans une base de données qui comportent quelques années d’historique et on se trouve avec plus de 30 Go de données!! Définitivement de quoi d’amuser! Les données brutes proviennent du site THE STANFORD OPEN POLICING PROJECT. Nous avons aussi accès à une base de données simplifié sur github.
Cette semaine, j’ai deux objectifs en tête:
1) Importer plusieurs .csv en une seule commande et les grouper en une table de données.
2) Visualiser le nombre de contrôle routier qui a été effectué en Caroline du Nord en fonction de l’âge de la personne contrôlée et comparer les différences entre les hommes et les femmes.
IMPORTER
# lire les csv dans l'espace de travail
files <- list.files(path = "~/Documents/ENTREPRISE/Projets R/Tidytuesday/TyT2019/W12/data", pattern = "*.csv", full.names = T)
# grouper tous les fichiers en une seule table de données
tbl <- sapply(files, read_csv, simplify=FALSE) %>%
bind_rows(.id = "id")
FALSE Parsed with column specification:
FALSE cols(
FALSE .default = col_character(),
FALSE raw_row_number = col_integer(),
FALSE date = col_date(format = ""),
FALSE time = col_time(format = ""),
FALSE subject_age = col_integer(),
FALSE arrest_made = col_logical(),
FALSE citation_issued = col_logical(),
FALSE warning_issued = col_logical(),
FALSE contraband_found = col_logical(),
FALSE contraband_drugs = col_logical(),
FALSE contraband_weapons = col_logical(),
FALSE frisk_performed = col_logical(),
FALSE search_conducted = col_logical(),
FALSE search_person = col_logical(),
FALSE search_vehicle = col_logical()
FALSE )
FALSE See spec(...) for full column specifications.
FALSE Parsed with column specification:
FALSE cols(
FALSE .default = col_character(),
FALSE raw_row_number = col_integer(),
FALSE date = col_date(format = ""),
FALSE time = col_time(format = ""),
FALSE subject_age = col_integer(),
FALSE arrest_made = col_logical(),
FALSE citation_issued = col_logical(),
FALSE warning_issued = col_logical(),
FALSE contraband_found = col_logical(),
FALSE contraband_drugs = col_logical(),
FALSE contraband_weapons = col_logical(),
FALSE frisk_performed = col_logical(),
FALSE search_conducted = col_logical(),
FALSE search_person = col_logical(),
FALSE search_vehicle = col_logical()
FALSE )
FALSE See spec(...) for full column specifications.
FALSE Parsed with column specification:
FALSE cols(
FALSE .default = col_character(),
FALSE raw_row_number = col_integer(),
FALSE date = col_date(format = ""),
FALSE time = col_time(format = ""),
FALSE subject_age = col_integer(),
FALSE arrest_made = col_logical(),
FALSE citation_issued = col_logical(),
FALSE warning_issued = col_logical(),
FALSE contraband_found = col_logical(),
FALSE contraband_drugs = col_logical(),
FALSE contraband_weapons = col_logical(),
FALSE frisk_performed = col_logical(),
FALSE search_conducted = col_logical(),
FALSE search_person = col_logical(),
FALSE search_vehicle = col_logical()
FALSE )
FALSE See spec(...) for full column specifications.
FALSE Parsed with column specification:
FALSE cols(
FALSE .default = col_character(),
FALSE raw_row_number = col_integer(),
FALSE date = col_date(format = ""),
FALSE time = col_time(format = ""),
FALSE subject_age = col_integer(),
FALSE arrest_made = col_logical(),
FALSE citation_issued = col_logical(),
FALSE warning_issued = col_logical(),
FALSE contraband_found = col_logical(),
FALSE contraband_drugs = col_logical(),
FALSE contraband_weapons = col_logical(),
FALSE frisk_performed = col_logical(),
FALSE search_conducted = col_logical(),
FALSE search_person = col_logical(),
FALSE search_vehicle = col_logical()
FALSE )
FALSE See spec(...) for full column specifications.
FALSE Parsed with column specification:
FALSE cols(
FALSE .default = col_character(),
FALSE raw_row_number = col_integer(),
FALSE date = col_date(format = ""),
FALSE time = col_time(format = ""),
FALSE subject_age = col_integer(),
FALSE arrest_made = col_logical(),
FALSE citation_issued = col_logical(),
FALSE warning_issued = col_logical(),
FALSE contraband_found = col_logical(),
FALSE contraband_drugs = col_logical(),
FALSE contraband_weapons = col_logical(),
FALSE frisk_performed = col_logical(),
FALSE search_conducted = col_logical(),
FALSE search_person = col_logical(),
FALSE search_vehicle = col_logical()
FALSE )
FALSE See spec(...) for full column specifications.
EXPLORER
glimpse(tbl)
## Observations: 2,722,013
## Variables: 27
## $ id <chr> "/Users/johaniefournier/Documents/ENTREPRIS...
## $ raw_row_number <int> 1219541, 1221117, 1221124, 1221133, 1221146...
## $ date <date> 2001-12-28, 2002-01-02, 2002-01-02, 2002-0...
## $ time <time> NA, 04:00:00, NA, 19:50:00, 20...
## $ location <chr> "Durham, Durham County", "Durham, Durham Co...
## $ county_name <chr> "Durham County", "Durham County", "Durham C...
## $ subject_age <int> 22, 28, 21, 28, 36, 68, 24, 30, 38, 56, 28,...
## $ subject_race <chr> "black", "hispanic", "black", "black", "bla...
## $ subject_sex <chr> "female", "male", "male", "female", "female...
## $ officer_id_hash <chr> "5b9908c1a5", "0c140e68b5", "0c140e68b5", "...
## $ department_name <chr> "Durham Police Department", "Durham Police ...
## $ type <chr> "vehicular", "vehicular", "vehicular", "veh...
## $ arrest_made <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F...
## $ citation_issued <lgl> FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FAL...
## $ warning_issued <lgl> TRUE, FALSE, TRUE, FALSE, TRUE, TRUE, FALSE...
## $ outcome <chr> "warning", "citation", "warning", "citation...
## $ contraband_found <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ contraband_drugs <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ contraband_weapons <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ frisk_performed <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ search_conducted <lgl> FALSE, FALSE, FALSE, FALSE, FALSE, FALSE, F...
## $ search_person <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ search_vehicle <lgl> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ search_basis <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ reason_for_frisk <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ reason_for_search <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ reason_for_stop <chr> "Investigation", "Stop Light/Sign Violation...
Nous disposons de plusieurs variables, mais pour ce que j’ai en tête j’ai seulement besoin de la date (au cas), de l’age et du sexe des sujets. Je ne pousserai donc pas plus loin mon analyse du reste des variables de ces données, et je vais commencer par extraire seulement ces variables des données.
PRÉPARER
data<-tbl%>%
select(date,subject_age,subject_sex)%>% # conserver seulement les colonnes pertinentes pour l'analyse
mutate(year=year(date))%>%
group_by(subject_age,subject_sex)%>%
summarise(nb=dplyr::n())%>%
mutate(nb=nb/1000)
data_sum<-data%>%
summarise(sum=sum(nb))%>%
summarise(sum=sum(sum))
VISUALISER
#Graphique
gg<-ggplot(data=data, aes(x=subject_age, y=nb, group=subject_sex, color=subject_sex))
gg<-gg + geom_line(size=1.5)
gg<-gg + scale_color_manual(values = c("#C003C6", "#A9A9A9")) #échelle des axes
gg<-gg + scale_y_continuous(breaks=seq(0,80,10), limits = c(0, 80))
gg<-gg + scale_x_continuous(breaks=seq(0,100, 10), limits = c(0, 100))
#modifier la légende
gg<-gg + theme(legend.position="none")
#modifier le thème
gg<-gg +theme(panel.border = element_blank(),
panel.background = element_rect(fill = "#292E1E", colour = "#292E1E"),
plot.background = element_rect(fill = "#292E1E", colour = "#292E1E"),
panel.grid.major.y= element_blank(),
panel.grid.major.x= element_blank(), panel.grid.minor = element_blank(),
axis.line = element_line(size=0.5, color="#A9A9A9"),
axis.ticks = element_line(size=0.5, color="#A9A9A9"))
#ajouter les titres
gg<-gg + labs(title="Women in North Carolina are less stopped than men by police on the road!",
subtitle="From 2000 to 2015, the Standford Open Policy Project collected informations of traffic stops by law enforcement in North Carolina.\nDuring those years, there has been over 2.7M of interventions by polices. Over all those interventions, woman are always less\nstopped than men no matter the age.",
y="number of traffic stops (x1000)",
x="Age") gg<-gg + theme(plot.title = element_text(hjust=0,size=20, color="#C003C6", face="bold"),
plot.subtitle = element_text(hjust=0,size=12, color="#A9A9A9"),
axis.title.y = element_text(hjust=1,size=12, color="#A9A9A9"),
axis.title.x = element_text(hjust=0,size=12, color="#A9A9A9"),
axis.text = element_text(hjust=0.5,size=12, color="#A9A9A9"))
#Ajouter les étiquettes de données
gg<-gg + annotate(geom="text", x=20,y=60, label="Men", color="#A9A9A9", size=4, hjust=0, fontface="bold") gg<-gg + annotate(geom="text", x=20,y=30, label="Women", color="#C003C6", size=4, hjust=0, fontface="bold")
J’ai choisi de comparer les nombres d’arrestations enter hommes et les femmes en fonction de l’age. Après avoir importé toutes les .csv de la Caroline du Nord et les avoir regroupés en une seule table de données, il y avait peu de manipulation à faire sur les données. Seulement grouper par catégorie et faire le compte du nombre d’arrestations. Utiliser des courbes m’apparaissaient le meilleur moyen de comparer les hommes et les femmes en fonction de leur âge. Le reste c’est une histoire de présentation graphique. Voici ce que ça donne:
Tu veux en savoir plus sur ma démarche? Va écouter l’épisode de podcast dans lequel j’explique mes réflexions pour arriver à ce résultat.
Pingback: Épisode 1: Line Chart ou comment utiliser des courbes pour montrer des différences | Johanie Fournier, agr.
Pingback: ADV1 - 'Line Chart' ou comment utiliser des courbes pour montrer des différences | Johanie Fournier, agr.