Last active
October 21, 2021 18:13
-
-
Save IvoVillanueva/5e218a31fdb1cdb73c462554147689cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# librerias --------------------------------------------------------------- | |
library(tidyverse) | |
library(rvest) | |
library(janitor) | |
library(ggchicklet) | |
library(prismatic) | |
library(ggtext) | |
library(fishualize) | |
library(scales) | |
library(ggimage) | |
library(gt) | |
library(gtExtras) | |
# tema ivo ---------------------------------------------------------------- | |
theme_ivo <- function() { | |
theme_minimal(base_size = 8.5, base_family = "Chivo") %+replace% | |
theme( | |
panel.grid.minor = element_blank(), | |
panel.grid.major = element_blank(), | |
plot.background = element_rect(fill = "#f4f4f4", color = "#f4f4f4"), | |
plot.caption = element_markdown(size = 9, hjust = 0.5, family = "Chivo") | |
) | |
} | |
# scrape data and wrangler ------------------------------------------------ | |
teams <- read.csv("acb.csv") | |
url <- "https://www.acb.com/estadisticas-equipos/valoracion/temporada_id/2021/edicion_id/963/tipo_id/0" %>% | |
read_html() | |
acb <- url %>% | |
html_element("table") %>% | |
html_table() %>% | |
row_to_names(row_number = 1) %>% | |
clean_names() %>% | |
select(tm = x, int, int_2, int_3) %>% | |
group_by(tm) %>% | |
mutate(across(c(int:int_3), as.numeric), | |
total = rowSums(across(int:int_3)), | |
"De 1" = int / total, | |
"De 2" = int_2 / total, | |
"De 3" = int_3 / total | |
) %>% | |
select(tm, "De 1":"De 3") %>% | |
pivot_longer(!tm, names_to = "tipo", values_to = "percent") | |
# plot -------------------------------------------------------------------- | |
p <- acb %>% | |
ungroup() %>% | |
mutate( | |
tm = as.factor(tm), | |
tipo = as.factor(tipo), | |
) %>% | |
ggplot(aes(tm, percent)) + | |
geom_chicklet(aes(fill = fct_inorder(tipo), color = after_scale(clr_darken(fill, 0.5))), alpha = .90) + | |
geom_text(aes(label = percent(percent, accuracy = 1L)), size = 4, position = position_stack(vjust = 0.5)) + | |
scale_y_continuous(position = "left", labels = c("0%", "25%", "50%", "75%", "100%"), limits = c(0, 1)) + | |
coord_flip() + | |
theme_ivo() + | |
scale_fill_fish(discrete = TRUE, option = "Trimma_lantana", end = .8) + | |
guides(fill = guide_legend( | |
keywidth = .5, | |
keyheight = .2, | |
default.unit = "inch", | |
title.position = "top", | |
label.position = "bottom", | |
nrow = 1, | |
hjust = 0.5 | |
)) + | |
theme( | |
legend.position = "top", | |
legend.text = element_text(size = 7, vjust = 4), | |
legend.title = element_text(size = 8, hjust = .5, vjust = -2), | |
legend.margin = margin(0, 0, 0, 0), | |
legend.box.margin = margin(0, 0, -5, 0), | |
axis.title.y = element_blank(), | |
axis.title.x = element_blank(), | |
axis.text.y = element_text(margin = margin(0, -27, 0, 0), size = 10, face = "bold"), | |
plot.title = element_markdown(face = "bold", size = 20.5, hjust = 0.5), | |
plot.subtitle = element_text(size = 15.5, hjust = 0.5) | |
) + | |
labs( | |
title = "Distribución De Tiro En Liga Endesa", | |
subtitle = paste0("Temporada (2021-22) | Hasta el ", format(Sys.Date(), "%d %B, %Y")), | |
caption = "<br>**Datos**: *acb.com* **Gráfico**: *Ivo Villanueva*", | |
fill = "" | |
) | |
ggsave("distribucion.png", p, | |
height = 10, width = 10, dpi = 300 | |
) | |
# gt table y data wrangler ---------------------------------------------------------------- | |
url <- "https://www.acb.com/estadisticas-equipos/valoracion/temporada_id/2021/edicion_id/963/tipo_id/0" %>% | |
read_html() | |
acb <- url %>% | |
html_element("table") %>% | |
html_table() %>% | |
row_to_names(row_number = 1) %>% | |
clean_names() %>% | |
select(tm = x, int, int_2, int_3) %>% | |
mutate(across(c(int:int_3), as.numeric), | |
total = rowSums(across(int:int_3)), | |
tm = case_when( | |
tm == "Bitci Baskonia" ~ "Baskonia", | |
TRUE ~ tm | |
)) %>% | |
arrange(desc(total)) %>% | |
left_join(teams) | |
combine_word <- function(tm, logo, abb) { | |
glue::glue( | |
"<div style='line-height:10px'><span style='font-weight:bold;font-variant:small-caps;horizontal-align:left;font-size:15px'>{tm}</span></div> | |
<div style='font-size:25px;line-height:15px;vertical-align:middle;horizontal-align:left'><span style ='font-weight:bold;color:grey;font-size:10px'><img src='{logo}' | |
style='width:20px; height:20px;vertical-align:middle;horizontal-align:left'> {abb}</span></div>" | |
) | |
} | |
acb %>% | |
mutate( | |
me_int = mean(int), | |
me_int2 = mean(int_2), | |
me_int3 = mean(int_3), | |
int1 = int, | |
int_21 = int_2, | |
int_31 = int_3, | |
combo = combine_word(tm, logo, abb), | |
combo = map(combo, gt::html) | |
) %>% | |
arrange(desc(total)) %>% | |
select(combo, int1, int, me_int, int_21, int_2, me_int2, int_31, int_3, me_int3) %>% | |
gt() %>% | |
gt_plt_bullet( | |
column = int, | |
target = me_int, | |
width = 30, | |
colors = c("#f28422", "#0e1631") | |
) %>% | |
gt_plt_bullet( | |
column = int_2, | |
target = me_int2, | |
width = 30, | |
colors = c("#f28422", "#0e1631") | |
) %>% | |
gt_plt_bullet( | |
column = int_3, | |
target = me_int3, | |
width = 30, | |
colors = c("#f28422", "#0e1631") | |
) %>% | |
cols_label( | |
combo = gt::html("<span style='font-weight:bold;font-size:12px'>EQUIPO</span>"), | |
int1 = gt::html("<span style='font-weight:bold;font-size:12px'>TIROS<br>DE 1</span>"), | |
int_21 = gt::html("<span style='font-weight:bold;font-size:12px'>TIROS<br>DE 2</span>"), | |
int_31 = gt::html("<span style='font-weight:bold;font-size:12px'>TIROS<br>DE 3</span>"), | |
me_int = gt::html("<span style='font-weight:bold;font-size:12px'>TIROS<br>DE 1 VS. MEDIA</span>"), | |
me_int2 = gt::html("<span style='font-weight:bold;font-size:12px'>TIROS<br>DE 2 VS. MEDIA</span>"), | |
me_int3 = gt::html("<span style='font-weight:bold;font-size:12px'>TIROS<br>DE 3 VS. MEDIA</span>") | |
) %>% | |
tab_header( | |
title = md("<img src='https://upload.wikimedia.org/wikipedia/commons/thumb/e/e7/Liga_Endesa_2019_logo.svg/800px-Liga_Endesa_2019_logo.svg.png' | |
style='height:40px;'>"), | |
subtitle = md(paste0("<b>Distribución de tiro y su relación con la media hasta el ", format(Sys.Date(), "%d %B, %Y</b>"))) | |
) %>% | |
tab_source_note( | |
source_note = md("<div><sup style='font-weight:bold;font-size:6px'>1</sup><i>Ordenado de mas tiros realizados a menos<i></div> | |
<div><b>Grafica por</b> : <i>\n Ivo Villanueva @elcheff<i> | |
<div><b>Datos por</b> : \n<i>acb.com<i>") | |
) %>% | |
gt_theme_538() %>% | |
cols_width( | |
c(int1, int_21, int_31) ~ px(25) | |
) %>% | |
cols_width( | |
c(me_int, me_int2, me_int3) ~ px(35) | |
) %>% | |
tab_options( | |
data_row.padding = px(3), | |
) %>% | |
cols_align( | |
align = "center", | |
columns = c(int1:me_int3) | |
) %>% | |
cols_align( | |
align = "left", | |
columns = combo | |
) %>% | |
gtsave("gtacbdistribucion.png") | |
# Ivo Villanueva ---------------------------------------------------------- |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment