Ocultar código
library(glue)
library(ggtext)
library(showtext)
library(tidytext)
library(tidyverse)
Sitio en construcción
Víctor Gauto
1 de abril de 2025
Capacidad de ataque y defensa para cada tipo principal de Pokemon.
Colores.
Fuentes: Ubuntu y JetBrains Mono.
fuente <- glue(
"Datos: <span style='color:{c1};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana 13, ",
"<b style='font-family:jet;'>{{pokemon}}</b>.</span>"
)
autor <- glue("<span style='color:{c1};'>**Víctor Gauto**</span>")
icon_twitter <- glue("<span style='font-family:jet;'></span>")
icon_instagram <- glue("<span style='font-family:jet;'></span>")
icon_github <- glue("<span style='font-family:jet;'></span>")
icon_mastodon <- glue("<span style='font-family:jet;'>󰫑</span>")
icon_bsky <- glue("<span style='font-family:jet;'></span>")
usuario <- glue("<span style='color:{c1};'>**vhgauto**</span>")
sep <- glue("**|**")
mi_caption <- glue(
"{fuente}<br>{autor} {sep} {icon_github} {icon_twitter} {icon_instagram} ",
"{icon_mastodon} {icon_bsky} {usuario}"
)
Me interesa la distribución de los puntos de ataque y defensa por cada tipo principal de Pokemon. Agregar, cuando esté disponible, los íconos de cada Pokemon que tenga el máximo valor por cada tipo.
Logo y traducciones de los tipos principales de Pokemon.
link <- "https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/International_Pok%C3%A9mon_logo.svg/1024px-International_Pok%C3%A9mon_logo.svg.png"
logo <- glue("<img src='{link}' width=150 />")
tipos_v <- unique(pokemon$type_1)
tipos_v_trad <- c(
"Planta", "Fuego", "Agua", "Bicho", "Normal", "Veneno", "Eléctrico", "Tierra",
"Hada", "Lucha", "Psíquico", "Roca", "Fantasma", "Hielo", "Dragón",
"Siniestro", "Acero", "Volador"
)
tipos_v_trad <- set_names(tipos_v_trad, tipos_v)
Acomodo los datos y ordeno los tipos de Pokemon por capacidad de ataque y defensa.
d <- pokemon |>
pivot_longer(
cols = c(attack, defense),
names_to = "modo",
values_to = "puntos"
) |>
select(pokemon, puntos, type_1, url_icon, modo) |>
mutate(tipo = tipos_v_trad[type_1]) |>
mutate(
tipo = reorder_within(x = tipo, by = puntos, within = modo, fun = median)
) |>
mutate(modo = if_else(modo == "attack", "Ataque", "Defensa"))
Agrego íconos de los Pokemon que estén disponibles, para el valor máximo de cada tipo principal
Figura.
g <- ggplot(
d, aes(x = puntos, y = tipo, fill = puntos)
) +
geom_richtext(
data = d_icon, aes(label = label), fill = NA, hjust = 0,
label.color = NA
) +
geom_point(
position = position_jitter(seed = 2025, height = .1), shape = 21,
color = c6, size = 2, alpha = .7
) +
facet_wrap(vars(modo), nrow = 1, scales = "free") +
scale_x_continuous(limits = c(0, 231), breaks = seq(0, 200, 50)) +
scale_fill_gradientn(
colours = c(c1, c2, c3),
values = scales::rescale(
c(min(d$puntos, na.rm = TRUE), median(d$puntos, na.rm = TRUE),
max(d$puntos, na.rm = TRUE))
)
) +
scale_y_reordered() +
coord_cartesian(clip = "off", expand = FALSE, ylim = c(.5, 18.5)) +
labs(x = NULL, y = NULL, tag = logo, caption = mi_caption) +
ggridges::theme_ridges() +
theme(
text = element_text(family = "ubuntu", size = 15, color = c4),
aspect.ratio = 1.5,
plot.background = element_rect(fill = c5),
plot.margin = margin(r = 40, l = 20, t = 45, b = 10),
plot.caption = element_markdown(
size = 12, color = c3, lineheight = 1.1, margin = margin(t = 15)
),
plot.tag = element_markdown(),
plot.tag.position = c(.45, 1.025),
plot.tag.location = "plot",
panel.grid.major.y = element_blank(),
axis.text.x = element_text(family = "jet", color = c4),
axis.text.y = element_text(vjust = .5, color = c4),
axis.ticks = element_blank(),
legend.position = "none",
strip.text = element_text(
hjust = 0, face = "bold", size = 20, margin = margin(b = 5)
),
strip.background = element_blank()
)
Guardo.
---
format:
html:
code-fold: show
code-summary: "Ocultar código"
code-line-numbers: false
code-annotations: false
code-link: true
code-tools:
source: true
toggle: true
caption: "Código"
code-overflow: scroll
page-layout: full
editor_options:
chunk_output_type: console
categories: ["geom_richtext", "geom_point"]
execute:
eval: false
echo: true
warning: false
title: "Semana 13"
date: 2025-04-01
author: Víctor Gauto
---
Capacidad de ataque y defensa para cada tipo principal de **Pokemon**.

## Paquetes
```{r}
library(glue)
library(ggtext)
library(showtext)
library(tidytext)
library(tidyverse)
```
## Estilos
Colores.
```{r}
c1 <- "#9C1052"
c2 <- "grey95"
c3 <- "#3E4E1E"
c4 <- "#3A5DA8"
c5 <- "grey95"
c6 <- "black"
```
Fuentes: Ubuntu y JetBrains Mono.
```{r}
font_add(
family = "ubuntu",
regular = "././fuente/Ubuntu-Regular.ttf",
bold = "././fuente/Ubuntu-Bold.ttf",
italic = "././fuente/Ubuntu-Italic.ttf"
)
font_add(
family = "jet",
regular = "././fuente/JetBrainsMonoNLNerdFontMono-Regular.ttf"
)
showtext_auto()
showtext_opts(dpi = 300)
```
## Epígrafe
```{r}
fuente <- glue(
"Datos: <span style='color:{c1};'><span style='font-family:jet;'>",
"{{<b>tidytuesdayR</b>}}</span> semana 13, ",
"<b style='font-family:jet;'>{{pokemon}}</b>.</span>"
)
autor <- glue("<span style='color:{c1};'>**Víctor Gauto**</span>")
icon_twitter <- glue("<span style='font-family:jet;'></span>")
icon_instagram <- glue("<span style='font-family:jet;'></span>")
icon_github <- glue("<span style='font-family:jet;'></span>")
icon_mastodon <- glue("<span style='font-family:jet;'>󰫑</span>")
icon_bsky <- glue("<span style='font-family:jet;'></span>")
usuario <- glue("<span style='color:{c1};'>**vhgauto**</span>")
sep <- glue("**|**")
mi_caption <- glue(
"{fuente}<br>{autor} {sep} {icon_github} {icon_twitter} {icon_instagram} ",
"{icon_mastodon} {icon_bsky} {usuario}"
)
```
## Datos
```{r}
tuesdata <- tidytuesdayR::tt_load(2025, 13)
pokemon <- tuesdata$pokemon_df
```
## Procesamiento
Me interesa la distribución de los puntos de ataque y defensa por cada tipo principal de Pokemon. Agregar, cuando esté disponible, los íconos de cada Pokemon que tenga el máximo valor por cada tipo.
## Figura
Logo y traducciones de los tipos principales de Pokemon.
```{r}
link <- "https://upload.wikimedia.org/wikipedia/commons/thumb/9/98/International_Pok%C3%A9mon_logo.svg/1024px-International_Pok%C3%A9mon_logo.svg.png"
logo <- glue("<img src='{link}' width=150 />")
tipos_v <- unique(pokemon$type_1)
tipos_v_trad <- c(
"Planta", "Fuego", "Agua", "Bicho", "Normal", "Veneno", "Eléctrico", "Tierra",
"Hada", "Lucha", "Psíquico", "Roca", "Fantasma", "Hielo", "Dragón",
"Siniestro", "Acero", "Volador"
)
tipos_v_trad <- set_names(tipos_v_trad, tipos_v)
```
Acomodo los datos y ordeno los tipos de Pokemon por capacidad de ataque y defensa.
```{r}
d <- pokemon |>
pivot_longer(
cols = c(attack, defense),
names_to = "modo",
values_to = "puntos"
) |>
select(pokemon, puntos, type_1, url_icon, modo) |>
mutate(tipo = tipos_v_trad[type_1]) |>
mutate(
tipo = reorder_within(x = tipo, by = puntos, within = modo, fun = median)
) |>
mutate(modo = if_else(modo == "attack", "Ataque", "Defensa"))
```
Agrego íconos de los Pokemon que estén disponibles, para el valor máximo de cada tipo principal
```{r}
d_icon <- d |>
slice_max(order_by = puntos, n = 1, by = c(tipo, modo), with_ties = FALSE) |>
mutate(
label = if_else(
is.na(url_icon),
"",
glue(
"<img src='https:{url_icon}' width=35 />"
)
)
)
```
Figura.
```{r}
g <- ggplot(
d, aes(x = puntos, y = tipo, fill = puntos)
) +
geom_richtext(
data = d_icon, aes(label = label), fill = NA, hjust = 0,
label.color = NA
) +
geom_point(
position = position_jitter(seed = 2025, height = .1), shape = 21,
color = c6, size = 2, alpha = .7
) +
facet_wrap(vars(modo), nrow = 1, scales = "free") +
scale_x_continuous(limits = c(0, 231), breaks = seq(0, 200, 50)) +
scale_fill_gradientn(
colours = c(c1, c2, c3),
values = scales::rescale(
c(min(d$puntos, na.rm = TRUE), median(d$puntos, na.rm = TRUE),
max(d$puntos, na.rm = TRUE))
)
) +
scale_y_reordered() +
coord_cartesian(clip = "off", expand = FALSE, ylim = c(.5, 18.5)) +
labs(x = NULL, y = NULL, tag = logo, caption = mi_caption) +
ggridges::theme_ridges() +
theme(
text = element_text(family = "ubuntu", size = 15, color = c4),
aspect.ratio = 1.5,
plot.background = element_rect(fill = c5),
plot.margin = margin(r = 40, l = 20, t = 45, b = 10),
plot.caption = element_markdown(
size = 12, color = c3, lineheight = 1.1, margin = margin(t = 15)
),
plot.tag = element_markdown(),
plot.tag.position = c(.45, 1.025),
plot.tag.location = "plot",
panel.grid.major.y = element_blank(),
axis.text.x = element_text(family = "jet", color = c4),
axis.text.y = element_text(vjust = .5, color = c4),
axis.ticks = element_blank(),
legend.position = "none",
strip.text = element_text(
hjust = 0, face = "bold", size = 20, margin = margin(b = 5)
),
strip.background = element_blank()
)
```
Guardo.
```{r}
ggsave(
plot = g,
filename = "tidytuesday/2025/semana_13.png",
width = 30,
height = 22,
units = "cm"
)
```