-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScript_graph_total_benji.R
122 lines (96 loc) · 5.01 KB
/
Script_graph_total_benji.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
library(tidyverse)
library(dplyr)
## Importation du csv
dftotal <- read.csv("graphtotal.csv", header = TRUE,
colClasses = c(Catégories = "character",
Mandat.2.et.3 = "numeric",
Mandat.2 = "numeric",
Mandat.3 = "numeric"))
Promesses <- openxlsx::read.xlsx(
"_SharedFolder_livre_promesses-trudeau/Chapitre 1/BDTrudeau-Chap1.xlsx",
3) |> filter(`Inclusion.Polimètre./.Inclusion.Polimeter` == T)
dftotal <- data.frame(Promesses)
colnames(dftotal) <- c("Catégories", "Mandats 2 et 3", "Mandat 2", "Mandat 3")
## changer le data frame en un long format
dftotal <- dftotal %>% gather(key = "Status", value = "Value", -Catégories)
## Réordonner les colomnes en facteurs, et les réordonner comme souhaité
dftotal$Status <- factor(dftotal$Status, levels = c("Mandat 2", "Mandats 2 et 3", "Mandat 3"))
## Création de la palette de couleur
pourcentage_palette <- c("#CCCCCC", "#666666", "black")
## création du graphique
dftotalgraph <- ggplot(dftotal, aes(x = Catégories, y = Value ,fill = Status)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = paste0(Value, "%")), vjust = -0.5, position = position_dodge(0.9),
size = 2.5) +
scale_fill_manual(values = pourcentage_palette) +
labs(x = "Catégories d'enjeux",
y = "Pourcentage de l'ensemble des \n promesses du mandat par catégorie")+
clessnverse::theme_clean_light(base_size = 15) +
theme(
plot.title = element_text(size = 15, hjust = 0.5),
axis.title.x = element_text(size = 12, hjust = 0.5),
axis.title.y = element_text(size = 10, hjust = 0.7),
axis.text = element_text(size = 10),
axis.text.x = element_text(angle = 65, hjust=0.9))
## Impression du ggplot
print(dftotalgraph)
## Exportation
ggsave("_SharedFolder_livre_promesses-trudeau/Chapitre 1/graphs/pourcentage_mandat2_3.png", plot = dftotalgraph, width = 12, height = 6)
## English version
## Importation du csv
dftotaleng <- read.csv("graphtotal.csv", header = TRUE,
colClasses = c(Catégories = "character",
Mandat.2.et.3 = "numeric",
Mandat.2 = "numeric",
Mandat.3 = "numeric"))
colnames(dftotaleng) <- c("Categories", "Terms 2 & 3", "Term 2", "Term 3")
dftotaleng <- dftotaleng %>%
mutate(Categories = case_when(
Categories == "Affaires internationales et défense" ~ "International Affairs and Defense",
Categories == "Arts et culture" ~ "Arts and Culture",
Categories == "Économie et employabilité" ~ "Economy and Employability",
Categories == "Éducation et recherche" ~ "Education and Research",
Categories == "Environnement" ~ "Environment",
Categories == "Familles" ~ "Families",
Categories == "Gouvernement et démocratie" ~ "Government and Democracy",
Categories == "Identité et nationalisme" ~ "Identity and Nationalism",
Categories == "Loi et ordre" ~ "Law and Order",
Categories == "Minorités" ~ "Minorities",
Categories == "Régions et agriculture" ~ "Regions and Agriculture",
Categories == "Santé et services sociaux" ~ "Health and Social Service",
TRUE ~ Categories))
#
# À utiliser au besoin.
#
#row.names(dftotaleng$Categories) <- c(
# "International Affairs and Defense", "Arts and Culture",
# "Economy and Employability", "Education and Research", "Environment",
# "Families", "Government and Democracy", "Identity and Nationalism",
# "Law and Order", "Minorities", "Regions and Agriculture",
# "Health and Social Services")
## changer le data frame en un long format
dftotaleng <- dftotaleng %>% gather(key = "Status", value = "Value", -Categories)
## Réordonner les colomnes en facteurs, et les réordonner comme souhaité
dftotaleng$Status <- factor(dftotaleng$Status, levels = c("Term 2", "Terms 2 & 3", "Term 3"))
## Création de la palette de couleur
pourcentage_palette <- c("#CCCCCC", "#666666", "black")
## création du graphique
dftotalgrapheng <- ggplot(dftotaleng, aes(x = Categories, y = Value ,fill = Status)) +
geom_bar(stat = "identity", position = "dodge") +
geom_text(aes(label = paste0(Value, "%")), vjust = -0.5, position = position_dodge(0.9),
size = 3) +
scale_fill_manual(values = pourcentage_palette) +
labs(#title = "Percentage of pledges by issue category by mandate",
x = "Issue categories",
y = "Percentage of total mandate\npromises by category")+
clessnverse::theme_clean_light(base_size = 15) +
theme(
plot.title = element_text(size = 15, hjust = 0.5),
axis.title.x = element_text(size = 15, hjust = 0.5),
axis.title.y = element_text(size = 15, hjust = 0.7),
axis.text = element_text(size = 12),
axis.text.x = element_text(angle = 65, hjust=0.9))
## Impression du ggplot
print(dftotalgrapheng)
## Exportation
ggsave("_SharedFolder_livre_promesses-trudeau/Chapitre 1/graphs/pourcentage_mandat2_3ENG.png", plot = dftotalgrapheng, width = 12, height = 6)