# Load the packages in ----------------------------------------------------
if(!require(tidyverse)){install.packages("tidyverse"); library(tidyverse)}
if(!require(patchwork)){install.packages("patchwork"); library(patchwork)}
if(!require(ggplot2)){install.packages("ggplot2"); library(ggplot2)}
if(!require(ggwordcloud)){install.packages("ggwordcloud"); library(ggwordcloud)}
if(!require(ggpmisc)){install.packages("ggpmisc"); library(ggpmisc)}
if(!require(ggimage)){install.packages("ggimage"); library(ggimage)}
if(!require(ggtext)){install.packages("ggtext"); library(ggimage)}
if(!require(rlist)){install.packages("rlist"); library(rlist)}
# I stick all my styling into a CUsotm PAckage to tidy up my code and keep it consistent over the time
if(!require(CustomGGPlot2Theme)){devtools::install("CustomGGPlot2Theme"); library(CustomGGPlot2Theme)}
# get the wd - force of habit
wd <- getwd()
#pulling images off internet was taking a lot of time and casing a time out error
options(timeout = 1000)
font <- "noto_mono"
pokemon_df <- readr::read_csv('https://raw.githubusercontent.com/rfordatascience/tidytuesday/main/data/2025/2025-04-01/pokemon_df.csv')
# I only care about first gen - Older Millenial
first_gen <- pokemon_df %>%
filter(generation_id == 1) %>%
#for later
rename("special attack" = special_attack)
# going to loop through attack, defence, special attack, speed
columns <- c("attack", "defense", "special attack", "speed")
n = 3
#empty list of plots
list_of_plots <- list()
for(n in 1: length(columns)) {
data <- first_gen %>%
group_by(type_1) %>%
slice_max(order_by = .data[[columns[n]]], n = 1, with_ties = FALSE) %>%
ungroup() %>%
arrange(.data[[columns[n]]]) # Correct dynamic column referencing
#Make labels with a picture of the Pokemon and then the name of the pokemon underneath
# Generate labels after sorting
labels <- paste0(
"<img src='", data$url_image, "' width='45' height='35' /><br>",
"<span style='font-size:16px;'>", # Add font size styling here
sapply(strwrap(str_to_title(data$pokemon), width = 20, simplify = FALSE),
function(x) paste(x, collapse = "<br>")),
"</span>"
)
chart <- data %>%
ggplot(aes(x = reorder(pokemon, .data[[columns[n]]]),
y = .data[[columns[n]]],
fill = str_to_title(type_1))) +
geom_bar(stat = "identity") +
geom_text(aes(label = .data[[columns[n]]]),
hjust = -0.5,
position = position_dodge(width = 0.25),
size = 6,
family = font) +
coord_flip() +
labs(x = "Pokémon",
y = paste0(str_to_title(columns[n]), " Score"),
subtitle = str_to_title(columns[n]),
fill = "Pokémon Type") +
Custom_Style() +
theme(axis.text.y = element_markdown(),
legend.text = element_text(size = 15),
axis.text = element_text(size = 12)) +
scale_x_discrete(labels = labels)
list_of_plots <- list.append(list_of_plots, chart)
}
#single plot to look good for social media
single_chart <- list_of_plots[[1]]
single_chart <- single_chart +
labs(
title = 'Visualisation of various Pokémon stats by their Attack',
subtitle = "TidyTuesday: Week 13, 2025",
theme = Custom_Style()
) +
theme(
plot.subtitle = element_text(size = 16)
)
patchwork <- wrap_plots(list_of_plots) +
plot_layout(guides = "collect") +
plot_annotation(
title = str_wrap('Visualisation of various Pokémon stats by their type', 80),
subtitle = "TidyTuesday: Week 13, 2025",
theme = Custom_Style()
) &
theme(
plot.title = element_text(size = 24),
plot.subtitle = element_text(size = 16)
)