-
Notifications
You must be signed in to change notification settings - Fork 15
/
06_donut.R
62 lines (53 loc) · 2.04 KB
/
06_donut.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
# ----- Día 6: Donut -----
# Referencia: https://www.r-graph-gallery.com/128-ring-or-donut-plot.html
library(ggplot2)
library(ggrepel)
library(babynames)
library(dplyr)
library(RColorBrewer)
library(grid)
library(gridExtra)
# Importar fuentes
library(extrafont)
#font_import()
loadfonts(device = "win")
# Procesamiento
babynames %>%
filter(name == "Daniel") %>%
mutate(decada = substr(year, 1, 3)) %>%
group_by(decada) %>%
summarise(count = sum(n)) %>%
filter(decada != 201) -> data
# Compute percentages
data$fraction <- data$count / sum(data$count)
# Compute the cumulative percentages (top of each rectangle)
data$ymax <- cumsum(data$fraction)
# Compute the bottom of each rectangle
data$ymin <- c(0, head(data$ymax, n = -1))
# Compute label position
data$labelPosition <- (data$ymax + data$ymin) / 2
# Compute a good label
data$label <- paste0(data$decada, "0s\n", format(data$count, big.mark = "."), " bebés")
# Número de datos y colores
nb.cols <- nrow(data)
mycolors <- colorRampPalette(brewer.pal(12, "Paired"))(nb.cols)
# Donut plot
set.seed(1) # No es obligatorio, es para que las etiquetas salgan siempre en el mismo sitio
ggplot(data, aes(ymax = ymax, ymin = ymin, xmax = 4, xmin = 3, fill = decada)) +
geom_rect() +
geom_label_repel(x = 3.5, aes(y = labelPosition, label = label), size = 4, family = "Arial Rounded MT Bold",
max.overlaps = 5, label.size = NA, fill = NA, box.padding = 0, label.padding = 0,
direction = "y") +
scale_fill_manual(values = mycolors) +
coord_polar(theta = "y") +
ggtitle("Número de bebés de Estados Unidos que se llaman 'Daniel' por década (1880-2009)") +
xlim(c(2, 4)) +
theme_void() +
labs(caption = "danielredondo.com\nFuente: {babynames}, USA Social Security Administration.") +
theme(
legend.position = "none",
plot.caption = element_text(margin = margin(b = 20), size = 13, family = "Constantia"),
plot.title = element_text(hjust = 0.5, size = 14, face = "bold", family = "Constantia")
)
# Exportar gráfico
ggsave("6.png", height = 8, width = 8)