-
Notifications
You must be signed in to change notification settings - Fork 2
/
week02.R
203 lines (179 loc) · 6.53 KB
/
week02.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
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
# 0. Library and fonts management
library(dplyr)
library(ggplot2)
library(showtext)
library(glue)
library(scales)
library(sysfonts)
library(ggforce)
library(ggtext)
## Adding Google Fonts
sysfonts::font_add_google(name = "Teko", family = "teko") ### Sans Serif
sans <- "teko"
## Allows the use of the downloaded Google Font
## To see the results updated, it's needed to call windows() or save the image
showtext::showtext_opts(dpi = 320)
showtext::showtext_auto()
# 1. Data download, load and handling
## Data on inequality comes from IBGE
## https://www.ibge.gov.br/estatisticas/sociais/populacao/25844-desigualdades-sociais-por-cor-ou-raca.html?=&t=resultados
rawdata <- read.csv2("2022/week02/data.csv")
## Keeps only income for black people and orders the data by it
df <- rawdata %>%
dplyr::filter(race == "black") %>%
dplyr::arrange(desc(income))
## Associates each year with a color
df <- df %>%
dplyr::mutate(color = c("#dc143c","#ffd700","#d2b48c",
"#4682b4","#654321","#d2b48c",
"#000000"))
## Defines size for the circles
df <- df %>%
dplyr::mutate(size = scales::rescale(income, to = c(200,600)))
## Defines some layout constants
lnhgt <- 0.9
bgcolor <- "#d2b48c"
thick <- 0.05
wide <- 0.15
smooth <- 3.3
radius <- c(9.55,8.5,7.4,6.17,5.37,4.9,3.05)
theta <- c(1.5,5.8,3.9,0.5,4.9,2.4)
## Defines coordinates for the titles
titles <- tibble(
x = 0,
y = c(14.5,11.3,-13.5),
size = c(35,12,15),
label = c(
"REAL MONTHLY INCOME PER CAPITA\nOF BLACK HOUSEHOLDS IN BRAZIL.",
"EACH CIRCLE REPRESENTS THE INCOME FOR A GIVEN YEAR. ALL VALUES ARE DEFLATED\nTO 2018 BRAZILIAN REAIS. TRANSPARENT CIRCLES INDICATE YEARS OF INCOME LOSS.",
"INSPIRED BY: W.E.B. DU BOIS | DATA FROM: IBGE | GRAPHIC BY: ÍCARO BERNARDES (@IcaroBSC)"
)
)
## Defines coordinates for the message
message <- tibble(
x = 0,
y = -11,
size = 20,
label = "IN THE SAME PERIOD, WHITE HOUSEHOLDS HAD IN THEIR LOWEST AN INCOME OF R$ 1846.31.<br>
ALMOST THE <span style='color:white;'>DOUBLE</span> OF BLACK HOUSEHOLDS AT THEIR MAXIMUM (R$ 934.23)."
)
## Defines coordinates for the income labels
incomes <- tibble(
x = c(8, -2.9, -4.6, 3, -4.3, 2.4, 0),
y = c(0, 6.7, -4.25, 4.55, 1.15, -2.97, 0),
label = glue::glue("R$ {df$income}"),
angle = c(0, 290, 45, 60, 346, 312, 0),
color = c("white","black","black",
"white","white","black",
"white")
)
## Defines coordinates for the years labels
years <- tibble(
x = 0,
y = -radius,
label = df$year,
color = c("white","black","black",
"white","white","black",
"white")
)
## Creates coordinates for the wedges
plt <- df %>%
dplyr::select(year, color) %>%
dplyr::rename("fill" = "color")
wedges0 <- tibble(
r = radius[1:6]+0.2,
start = theta,
end = start+wide,
year = df$year[1:6]
) %>%
dplyr::left_join(plt) %>%
dplyr::mutate(x0 = 0, y0 = 0, r0 = 1.8)
wedges1 <- tibble(
r = radius[2:6]+0.2,
start = theta[1:5]-thick,
end = start+wide+2*thick,
year = df$year[2:6]
) %>%
dplyr::left_join(plt) %>%
dplyr::mutate(x0 = 0, y0 = 0, r0 = 1.8)
wedges2 <- tibble(
r = radius[3:6]+0.2,
start = theta[1:4]-2*thick,
end = start+wide+4*thick,
year = df$year[3:6]
) %>%
dplyr::left_join(plt) %>%
dplyr::mutate(x0 = 0, y0 = 0, r0 = 1.8)
wedges3 <- tibble(
r = radius[4:6]+0.2,
start = theta[1:3]-3*thick,
end = start+wide+6*thick,
year = df$year[4:6]
) %>%
dplyr::left_join(plt) %>%
dplyr::mutate(x0 = 0, y0 = 0, r0 = 1.8)
wedges4 <- tibble(
r = radius[5:6]+0.2,
start = theta[1:2]-4*thick,
end = start+wide+8*thick,
year = df$year[5:6]
) %>%
dplyr::left_join(plt) %>%
dplyr::mutate(x0 = 0, y0 = 0, r0 = 1.8)
wedges5 <- tibble(
r = radius[6]+0.2,
start = theta[1]-5*thick,
end = start+wide+10*thick,
year = df$year[6]
) %>%
dplyr::left_join(plt) %>%
dplyr::mutate(x0 = 0, y0 = 0, r0 = 1.8)
# 2. Generates the plot
## Creates the main plot
p <- df %>%
ggplot() +
### Places the "target sections"
geom_point(aes(x = 0, y = 0, size = I(size), color = I(color))) +
### Places the wedges
ggforce::geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = r0, r = r, fill = I(fill),
start = start, end = end), color = NA ,
radius = unit(smooth, 'mm'), data = wedges5) +
ggforce::geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = r0, r = r, fill = I(fill),
start = start, end = end), color = NA ,
radius = unit(smooth, 'mm'), data = wedges4) +
ggforce::geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = r0, r = r, fill = I(fill),
start = start, end = end), color = NA ,
radius = unit(smooth, 'mm'), data = wedges3) +
ggforce::geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = r0, r = r, fill = I(fill),
start = start, end = end), color = NA ,
radius = unit(smooth, 'mm'), data = wedges2) +
ggforce::geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = r0, r = r, fill = I(fill),
start = start, end = end), color = NA ,
radius = unit(smooth, 'mm'), data = wedges1) +
ggforce::geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = r0, r = r, fill = I(fill),
start = start, end = end), color = NA ,
radius = unit(smooth, 'mm'), data = wedges0) +
### Places the titles
geom_text(aes(x = x, y = y, label = label, size = I(size)),
family = sans, lineheight = lnhgt, vjust = 1, data = titles) +
### Places the message and the rectangle highlight
annotate("tile", x = -5.5, y = -12.1, width = 2.1, height = 0.8, fill = "#dc143c") +
ggtext::geom_richtext(aes(x = x, y = y, label = label, size = I(size)),
family = sans, lineheight = lnhgt, vjust = 1,
label.color = NA, fill = NA, data = message) +
### Places the income labels
geom_text(aes(x = x, y = y, label = label, color = I(color), angle = angle),
size = 12, family = sans, data = incomes) +
### Places the years labels
geom_text(aes(x = x, y = y, label = label, color = I(color)),
size = 12, family = sans, data = years) +
### Defines a coordinates system and limits the axes
coord_cartesian(xlim = c(-11,11), ylim = c(-14,14)) +
### Eliminates unnecessary elements and customizes the plot
theme_void() +
theme(
plot.background = element_rect(fill = bgcolor, color = NA)
)
## Saves the plot
ggsave("2022/week02/income.png", plot = p, dpi = "retina",
width = 22, height = 28)