# Load required libraries
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ readr     2.1.5
## ✔ forcats   1.0.0     ✔ stringr   1.5.1
## ✔ ggplot2   3.5.1     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ tidyr     1.3.1
## ✔ purrr     1.0.4     
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
# Load and clean data
data <- read_csv("Genshin_Impact_All_Character_Stat.csv")
## Rows: 574 Columns: 10
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (5): Character, Element, Weapon, Main role, Ascension
## dbl (5): Lv, Rarity, Base HP, Base ATK, Base DEF
## 
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
# Standardize column names
names(data) <- names(data) %>%
  str_replace_all("\\s+", "_") %>%
  str_replace_all("\\.+", "_") %>%
  tolower()

# Rename columns if necessary
data <- data %>%
  rename(
    character = character,  # Adjust if actual column name differs
    level = lv,
    hp = base_hp,
    atk = base_atk,
    def = base_def
  )

# Choose one character for the plot
selected_character <- "Hutao"  # Replace with the character you want

# Filter the data for the selected character
character_data <- data %>%
  filter(character == selected_character)

# Reshape for plotting (long format)
long_data <- character_data %>%
  pivot_longer(cols = c(hp, atk, def), names_to = "stat", values_to = "value")

# Plot with corrected color mapping
ggplot(long_data, aes(x = level, y = value, color = stat, group = stat)) +
  geom_line(linewidth = 1.5, linetype = "solid") +  
  geom_point(size = 4, alpha = 0.7) +  
  geom_text(aes(label = value), vjust = -0.6, size = 3.5, color = "black", fontface = "bold") + 
  labs(
    title = paste(selected_character, "Stat Progression"),
    x = "Level",
    y = "Stat Value",
    subtitle = "Track how each stat (HP, ATK, DEF) changes with leveling up"
  ) +
  scale_color_manual(values = c("hp" = "#1f77b4", "atk" = "#ff7f0e", "def" = "#2ca02c")) +  
  theme_minimal(base_size = 14) +  
  theme(
    legend.title = element_blank(),
    legend.position = "top",
    plot.title = element_text(size = 18, face = "bold", hjust = 0.5),
    plot.subtitle = element_text(size = 12, face = "italic", hjust = 0.5),
    axis.title = element_text(size = 14, face = "bold"),
    axis.text = element_text(size = 12),
    strip.text = element_text(size = 14, face = "bold"),
    panel.grid.major = element_line(color = "gray90", linewidth = 0.5),
    panel.grid.minor = element_blank(),
    plot.caption = element_text(size = 10, face = "italic", hjust = 1)
  ) +
  facet_wrap(~stat, scales = "free_y")  # Facet by stat with free y-scales

ggsave("character_stat_progression.jpg", width = 15, height = 8, dpi = 300)