-
Notifications
You must be signed in to change notification settings - Fork 0
/
timeline-graph-womenMPs.R
155 lines (130 loc) · 5.38 KB
/
timeline-graph-womenMPs.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Install all the needed packages
install.packages(c("dplyr",
"ggplot2",
"viridis",
"timelineS"))
# Load the data set of the first woman in parliament (source: IPU's Parline).
first <- read.csv("/Users/user1/Downloads/ipu-first-woman.csv",
fileEncoding = "UTF-8")
head(first)
# Convert 'First.woman.in.parliament' to Date format
first$First.woman.in.parliament <- as.character(first$First.woman.in.parliament)
first$First.woman.in.parliament <- as.Date(first$First.woman.in.parliament, "%Y")
class(first$First.woman.in.parliament)
# Count occurrences of first woman MPs for each year in each country
library(dplyr)
timeline <- first %>%
group_by(country, First.woman.in.parliament) %>%
summarize(count = n()) %>%
ungroup()
# Plot the timeline of the first women in parliament for continental European countries
# This creates a scatter plot of the countries' abbreviations and the year that a women MP entered parliament for the first time
library(ggplot2)
ggplot(timeline, aes(x = First.woman.in.parliament, y = country, color = country)) +
geom_point(aes(size = count), size = 4.5) +
scale_size_continuous(range = c(2, 8)) +
labs(title = "Timeline of First Woman MP (in European Countries)",
x = "Year",
y = "Country",
color = "Country") +
theme_gray() +
theme(legend.position = "none",
plot.title = element_text(face = "bold"),
axis.title.x = element_text(face = "bold"),
axis.title.y = element_text(face = "bold"))
# Visualizing the timeline in a different way::::::
# Create a summary data frame with the earliest year for each country
timeline_summary <- first %>%
group_by(country_name) %>%
summarize(First_year = min(First.woman.in.parliament))
head(timeline_summary)
# Generate color palette
library(viridis)
palette <- viridis(48)
# Create the new plot with the custom palette
ggplot(timeline_summary, aes(x = First_year, y = country_name)) +
geom_segment(aes(xend = First_year, yend = country_name), color = "darkblue", size = 2) +
geom_point(aes(color = country_name), size = 3) + # Map color to Country variable
scale_color_manual(values = palette) + # Use custom color palette
geom_text(aes(label = country_name), hjust = -0.2, vjust = 0.5, size = 3, color = "black") +
labs(title = "Timeline of First Woman MPs in European Countries",
x = "Year",
y = "",
caption = "IPU") +
theme_minimal() +
theme(plot.title = element_text(face = "bold"),
axis.text.y = element_blank(),
axis.ticks.y = element_blank(),
panel.grid.major.y = element_blank(),
panel.grid.minor.y = element_blank(),
legend.position = "none")
#github's Lee repository
first <- read.csv("/Users/user1/Downloads/ipu-first-woman.csv",
fileEncoding = "UTF-8")
head(first)
library(timelineS)
subset_first <- first[, c("country", "First.woman.in.parliament", "country_name")]
head(subset_first)
# Convert 'First.woman.in.parliament' to Date format
subset_first$First.woman.in.parliament <-
as.Date(subset_first$First.woman.in.parliament, "%Y.%m.%d")
# Convert 'Country' to factor
subset_first$country <- as.factor(subset_first$country)
class(subset_first)
# Create the timeline using the designated package
timelineS(subset_first, main = "When did Women Enter Parliament for the First Time?", buffer.days = 600,
label.direction = "up", label.cex = 0.4, label.angle = 35,
scale.tickwidth = 1.5, label.position = 3, label.color = "skyblue",
line.color = "darkblue", point.cex = 0.5, point.color = "black",
label.length = seq(0.1, 1.7, length.out = nrow(subset_first)))
legend_text <- c("AL-Albania",
"AD-Andorra",
"AM-Armenia",
"AT-Austria",
"AZ-Azerbaijan",
"BY-Belarus",
"BE-Belgium",
"BA-Bosnia and Herzegovina",
"BG-Bulgaria",
"HR-Croatia",
"CY-Cyprus",
"CZ-Czechia",
"DK-Denmark",
"EE-Estonia",
"FI-Finland",
"FR-France",
"GE-Georgia",
"DE-Germany",
"GR-Greece",
"HU-Hungary",
"IS-Iceland",
"IE-Ireland",
"IT-Italy",
"LV-Latvia",
"LI-Liechtenstein",
"LT-Lithuania",
"LU-Luxembourg",
"MT-Malta",
"MC-Monaco",
"ME-Montenegro",
"NL-Netherlands",
"MK-North Macedonia",
"NO-Norway",
"PL-Poland",
"PT-Portugal",
"MD-Republic of Moldova",
"RO-Romania",
"RU-Russian Federation",
"SM-San Marino",
"RS-Serbia",
"SK-Slovakia",
"SI-Slovenia",
"ES-Spain",
"SE-Sweden",
"CH-Switzerland",
"TR-Türkiye",
"UA-Ukraine",
"GB-United Kingdom")
# Add the caption of the country abbreviations to the graph
caption_text <- paste("Legend: ", paste(legend_text, collapse = ", "), sep = "")
mtext(caption_text, side = 1, line = -2, adj = 0, cex = 0.25)