/
bundestag_pie.Rmd
290 lines (264 loc) · 6.85 KB
/
bundestag_pie.Rmd
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
---
title: "Bundestag pie chart"
vignette: >
%\VignetteIndexEntry{Bundestag pie chart}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r message = FALSE, echo = FALSE}
knitr::opts_chunk$set(
fig.width = 6,
fig.height = 5
)
```
For this example, we will use the following packages.
```{r message = FALSE}
library(tidyverse)
library(ggforce) # for geom_arc_bar()
library(cowplot) # for theme_map()
```
The dataset is provided as `practicalgg::bundestag`. Let's look at it in table form and as a basic pie-chart.
```{r message = FALSE}
library(tidyverse)
bundestag <- practicalgg::bundestag %>%
select(party, seats, colors)
bundestag
ggplot(bundestag, aes(x = 1, y = seats, fill = party)) +
geom_col() +
coord_polar(theta = "y")
```
We could try to style this pie chart to make it look the way we want, but I usually find it easier to draw pie charts in cartesian coordinates, using `geom_arc_bar()` from ggforce. This requires a little more data preparation up front but gives much more predictable results on the back end.
```{r}
bund_pie <- bundestag %>%
arrange(seats) %>%
mutate(
end_angle = 2*pi*cumsum(seats)/sum(seats), # ending angle for each pie slice
start_angle = lag(end_angle, default = 0), # starting angle for each pie slice
mid_angle = 0.5*(start_angle + end_angle), # middle of each pie slice, for the text label
# horizontal and vertical justifications depend on whether we're to the left/right
# or top/bottom of the pie
hjust = ifelse(mid_angle > pi, 1, 0),
vjust = ifelse(mid_angle < pi/2 | mid_angle > 3*pi/2, 0, 1)
)
bund_pie
# radius of the pie and radius for outside and inside labels
rpie <- 1
rlabel_out <- 1.05 * rpie
rlabel_in <- 0.6 * rpie
ggplot(bund_pie) +
geom_arc_bar(
aes(
x0 = 0, y0 = 0, r0 = 0, r = rpie,
start = start_angle, end = end_angle, fill = party
)
) +
coord_fixed()
```
Next we add labels representing the numbers of seats for each party.
```{r}
ggplot(bund_pie) +
geom_arc_bar(
aes(
x0 = 0, y0 = 0, r0 = 0, r = rpie,
start = start_angle, end = end_angle, fill = party
)
) +
geom_text(
aes(
x = rlabel_in * sin(mid_angle),
y = rlabel_in * cos(mid_angle),
label = seats
),
size = 14/.pt # use 14 pt font size
) +
coord_fixed()
```
And we provide labels for the parties outside of the pie.
```{r}
ggplot(bund_pie) +
geom_arc_bar(
aes(
x0 = 0, y0 = 0, r0 = 0, r = rpie,
start = start_angle, end = end_angle, fill = party
)
) +
geom_text(
aes(
x = rlabel_in * sin(mid_angle),
y = rlabel_in * cos(mid_angle),
label = seats
),
size = 14/.pt
) +
geom_text(
aes(
x = rlabel_out * sin(mid_angle),
y = rlabel_out * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
),
size = 14/.pt
) +
coord_fixed()
```
We see that we need to make some space for the outside labels. We can do that by manually setting the scale limits and expansion.
```{r}
ggplot(bund_pie) +
geom_arc_bar(
aes(
x0 = 0, y0 = 0, r0 = 0, r = rpie,
start = start_angle, end = end_angle, fill = party
)
) +
geom_text(
aes(
x = rlabel_in * sin(mid_angle),
y = rlabel_in * cos(mid_angle),
label = seats
),
size = 14/.pt
) +
geom_text(
aes(
x = rlabel_out * sin(mid_angle),
y = rlabel_out * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
),
size = 14/.pt
) +
scale_x_continuous(
name = NULL,
limits = c(-1.5, 1.4),
expand = c(0, 0)
) +
scale_y_continuous(
name = NULL,
limits = c(-1.05, 1.15),
expand = c(0, 0)
) +
coord_fixed()
```
This plot shows how using a cartesian coordinate system is helpful. We can see exactly where elements lie and how we need to expand the limits to fully show all the labels. The CDU/CSU label remains partially obscured at this point, but this will be fixed later as we remove the legend and axis labels, resulting in slightly more space for the pie chart itself as well as the labels.
Next we change the pie colors. The dataset provides appropriate party colors, and we use those directly with `scale_fill_identity()`. Note that this scale eliminates the legend. We don't need a legend anyways, because we have direct labeled the pie slices.
```{r}
ggplot(bund_pie) +
geom_arc_bar(
aes(
x0 = 0, y0 = 0, r0 = 0, r = rpie,
start = start_angle, end = end_angle, fill = colors
)
) +
geom_text(
aes(
x = rlabel_in * sin(mid_angle),
y = rlabel_in * cos(mid_angle),
label = seats
),
size = 14/.pt
) +
geom_text(
aes(
x = rlabel_out * sin(mid_angle),
y = rlabel_out * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
),
size = 14/.pt
) +
scale_x_continuous(
name = NULL,
limits = c(-1.5, 1.4),
expand = c(0, 0)
) +
scale_y_continuous(
name = NULL,
limits = c(-1.05, 1.15),
expand = c(0, 0)
) +
scale_fill_identity() +
coord_fixed()
```
The black color for the text labels doesn't work well on top of the dark fill colors, and the black outline also looks overbearing, so we'll change those colors to white.
```{r}
ggplot(bund_pie) +
geom_arc_bar(
aes(
x0 = 0, y0 = 0, r0 = 0, r = rpie,
start = start_angle, end = end_angle, fill = colors
),
color = "white"
) +
geom_text(
aes(
x = rlabel_in * sin(mid_angle),
y = rlabel_in * cos(mid_angle),
label = seats
),
size = 14/.pt,
color = c("black", "white", "white")
) +
geom_text(
aes(
x = rlabel_out * sin(mid_angle),
y = rlabel_out * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
),
size = 14/.pt
) +
scale_x_continuous(
name = NULL,
limits = c(-1.5, 1.4),
expand = c(0, 0)
) +
scale_y_continuous(
name = NULL,
limits = c(-1.05, 1.15),
expand = c(0, 0)
) +
scale_fill_identity() +
coord_fixed()
```
Finally, we need to apply a theme that removes the background grid and axes
```{r message = FALSE}
ggplot(bund_pie) +
geom_arc_bar(
aes(
x0 = 0, y0 = 0, r0 = 0, r = rpie,
start = start_angle, end = end_angle, fill = colors
),
color = "white"
) +
geom_text(
aes(
x = rlabel_in * sin(mid_angle),
y = rlabel_in * cos(mid_angle),
label = seats
),
size = 14/.pt,
color = c("black", "white", "white")
) +
geom_text(
aes(
x = rlabel_out * sin(mid_angle),
y = rlabel_out * cos(mid_angle),
label = party,
hjust = hjust, vjust = vjust
),
size = 14/.pt
) +
scale_x_continuous(
name = NULL,
limits = c(-1.5, 1.4),
expand = c(0, 0)
) +
scale_y_continuous(
name = NULL,
limits = c(-1.05, 1.15),
expand = c(0, 0)
) +
scale_fill_identity() +
coord_fixed() +
theme_map()
```