-
Notifications
You must be signed in to change notification settings - Fork 7
/
tres.R
117 lines (102 loc) · 3.26 KB
/
tres.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
# #30díasdegráficos día 3
# Visualización datos FIFA19
# https://www.kaggle.com/karangadiya/fifa19
# Autora: Stephanie Orellana (@sporella)
# Cargar librerías --------------------------------------------------------
library(tidyverse)
library(janitor)
library(ggthemes)
library(ggforce)
# Cargar y procesar datos -------------------------------------------------
data <- read_csv("data/fifa19.csv") %>%
clean_names()
equipos <- c("Real Madrid", "FC Bayern München", "FC Barcelona", "Atlético Madrid",
"Liverpool", "Paris Saint-Germain")
data_equipos <- data %>%
mutate(wage2 = as.numeric(str_extract(wage, pattern = "[:digit:]+"))) %>%
filter(club %in% equipos, position != "GK")
# Modificar tema de ggplot ------------------------------------------------
# basado en ggthemes::theme_fivethirtyeight()
theme_modi <- function (base_size = 12,
base_family = "sans")
{
colors <- deframe(ggthemes::ggthemes_data[["fivethirtyeight"]])
(
theme_foundation(base_size = base_size, base_family = base_family) +
theme(
line = element_line(colour = "black"),
rect = element_rect(
fill = colors["Light Gray"],
linetype = 0,
colour = NA
),
text = element_text(colour = colors["Dark Gray"]),
axis.ticks = element_blank(),
axis.line = element_blank(),
legend.background = element_rect(),
legend.position = "bottom",
legend.direction = "horizontal",
legend.box = "vertical",
panel.grid = element_line(colour = NULL),
panel.grid.major = element_line(colour = colors["Medium Gray"]),
panel.grid.minor = element_blank(),
plot.title = element_text(
hjust = 0,
size = rel(1.5),
face = "bold"
),
plot.margin = unit(c(1,
1, 1, 1), "lines"),
strip.background = element_rect()
)
)
}
# Gráfico en ggplot2 y anotaciones con ggforce ----------------------------
p <- ggplot(data_equipos,
aes(
y = age,
x = overall,
size = wage2,
colour = club,
fill = club
)) +
geom_point(alpha = 0.7,
shape = 21,
colour = "grey33") +
scale_size(
breaks = floor(seq(1, 600, length.out = 5)),
limits = c(1, 600),
range = c(2, 15),
labels = function(x) {
paste0("€", x, "K")
}
) +
scale_fill_manual(values = c(
"#7f62b8",
"#b84c7d",
"#b8553c",
"#00a6b5",
"#bd9d3c",
"#46c19a"
)) +
theme_modi() +
labs(
x = "Rendimiento general",
y = "Edad",
fill = "Club",
size = "Salario",
colour = "Club",
title = "Estadísticas juego FIFA19",
caption = "@sporella"
) +
guides(fill = guide_legend(override.aes = list(size = 8)),
size = guide_legend(order = 1))
p <- p+
geom_mark_circle(aes(filter = age<20 & overall>80, label = name),
colour="black",fill="transparent",size=1,
show.legend = F)+
geom_mark_circle(aes(filter = age>30 & overall>93, label = name),
colour="black",fill="transparent",size=1,
show.legend = F)
p
ggsave("plots/tres/fifa.png",p, width = 6.59, height = 5.46)