Víctor Gauto
  • Tidytuesday
  • Publicaciones
  • Mapas de Argentina
  • Visualizaciones
  1. 2025
  2. Semana 13

Sitio en construcción

  • Inicio
  • 2025
    • Semana 02
    • Semana 03
    • Semana 04
    • Semana 05
    • Semana 06
    • Semana 07
    • Semana 08
    • Semana 09
    • Semana 10
    • Semana 11
    • Semana 12
    • Semana 13
    • Semana 14
    • Semana 15
    • Semana 16
    • Semana 17
    • Semana 18
    • Semana 19
    • Semana 20
    • Semana 21
  • 2024
    • Semana 02
    • Semana 03
    • Semana 04
    • Semana 05
    • Semana 06
    • Semana 07
    • Semana 08
    • Semana 09
    • Semana 10
    • Semana 11
    • Semana 12
    • Semana 13
    • Semana 14
    • Semana 15
    • Semana 16
    • Semana 17
    • Semana 18
    • Semana 19
    • Semana 20
    • Semana 21
    • Semana 22
    • Semana 23
    • Semana 24
    • Semana 25
    • Semana 26
    • Semana 27
    • Semana 28
    • Semana 29
    • Semana 30
    • Semana 31
    • Semana 32
    • Semana 33
    • Semana 34
    • Semana 35
    • Semana 36
    • Semana 37
    • Semana 38
    • Semana 39
    • Semana 40
    • Semana 41
    • Semana 42
    • Semana 43
    • Semana 44
    • Semana 45
    • Semana 46
    • Semana 47
    • Semana 48
    • Semana 49
    • Semana 50
    • Semana 51
    • Semana 52
    • Semana 53
  • 2023
    • Semana 07
    • Semana 12
    • Semana 13
    • Semana 14
    • Semana 15
    • Semana 16
    • Semana 17
    • Semana 18
    • Semana 19
    • Semana 20
    • Semana 21
    • Semana 22
    • Semana 23
    • Semana 24
    • Semana 25
    • Semana 26
    • Semana 27
    • Semana 28
    • Semana 29
    • Semana 30
    • Semana 31
    • Semana 32
    • Semana 33
    • Semana 34
    • Semana 35
    • Semana 36
    • Semana 37
    • Semana 38
    • Semana 39
    • Semana 40
    • Semana 41
    • Semana 42
    • Semana 43
    • Semana 44
    • Semana 45
    • Semana 46
    • Semana 47
    • Semana 48
    • Semana 49
    • Semana 50
    • Semana 51
    • Semana 52

Contenido

  • Paquetes
  • Estilos
  • Epígrafe
  • Datos
  • Procesamiento
  • Figura
  • Editar esta página
  • Informar de un problema
  1. 2025
  2. Semana 13

Semana 13

  • Mostrar todo el código
  • Ocultar todo el código

  • Ver el código fuente
geom_richtext
geom_point
Autor

Víctor Gauto

Fecha de publicación

1 de abril de 2025

Capacidad de ataque y defensa para cada tipo principal de Pokemon.

Semana 13, 2025

Paquetes

Ocultar código
library(glue)
library(ggtext)
library(showtext)
library(tidytext)
library(tidyverse)

Estilos

Colores.

Ocultar código
c1 <- "#9C1052"
c2 <- "grey95"
c3 <- "#3E4E1E"
c4 <- "#3A5DA8"
c5 <- "grey95"
c6 <- "black"

Fuentes: Ubuntu y JetBrains Mono.

Ocultar código
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

Ocultar código
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;'>&#xf099;</span>")
icon_instagram <- glue("<span style='font-family:jet;'>&#xf16d;</span>")
icon_github <- glue("<span style='font-family:jet;'>&#xf09b;</span>")
icon_mastodon <- glue("<span style='font-family:jet;'>&#xf0ad1;</span>")
icon_bsky <- glue("<span style='font-family:jet;'>&#xe28e;</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

Ocultar código
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.

Ocultar código
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.

Ocultar código
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

Ocultar código
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.

Ocultar código
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.

Ocultar código
ggsave(
  plot = g,
  filename = "tidytuesday/2025/semana_13.png",
  width = 30,
  height = 22,
  units = "cm"
)
Subir
Semana 12
Semana 14
Ejecutar el código
---
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**.

![Semana 13, 2025](semana_13.png)

## 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;'>&#xf099;</span>")
icon_instagram <- glue("<span style='font-family:jet;'>&#xf16d;</span>")
icon_github <- glue("<span style='font-family:jet;'>&#xf09b;</span>")
icon_mastodon <- glue("<span style='font-family:jet;'>&#xf0ad1;</span>")
icon_bsky <- glue("<span style='font-family:jet;'>&#xe28e;</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"
)
```

Creado con y

Víctor Gauto

  • Editar esta página
  • Informar de un problema