# Install and load necessary packages
if(!require(ggplot2)) install.packages("ggplot2")
## Loading required package: ggplot2
if(!require(showtext)) install.packages("showtext")
## Loading required package: showtext
## Loading required package: sysfonts
## Loading required package: showtextdb
if(!require(sysfonts)) install.packages("sysfonts")
library(ggplot2)
library(showtext)
library(sysfonts)

# Add elegant fonts
font_add_google("Roboto", "roboto")
font_add_google("Open Sans", "opensans")
showtext_auto()

# Configuration parameters --------------------------------------------------
total_percentage <- 23.5     # Total completion percentage
partial_first <- 3.5         # Partial completion ratio
partial_second <- 6.5        # Partial incompletion ratio

# Optional: Additional pastel colors for customization
pastel_palette <- c(
  "#F8C3CD", # Pink
  "#A0DEFF", # Blue (our current)
  "#E6C2A0", # Tan
  "#D8BFD8", # Lavender
  "#CCE2CB", # Mint
  "#FFE1A8"  # Yellow
) # Uncomment below to use a different pastel color

# Pastel color settings - soft, gentle colors
color_completed <- "#A0DEFF"  # Pastel blue
color_incomplete <- "#F9F7F3" # Soft cream background
color_border <- "#E8E8E8"     # Light gray border
color_text <- "#6D7278"       # Muted text color

border_size <- 0.8            # Thinner border for a more modern look

# Grid settings
n_rows <- 10
n_cols <- 10
total_squares <- n_rows * n_cols
completed_squares <- floor(total_percentage)
partial_square_position <- completed_squares + 1
split_ratio <- partial_first / (partial_first + partial_second)
# Create grid dataframe with only two status categories
grid_df <- expand.grid(x = 1:n_cols, y = 1:n_rows)
grid_df$id <- 1:nrow(grid_df)
# Simplify to just two status categories for the legend
grid_df$status <- ifelse(grid_df$id <= completed_squares, "Completed", "Incomplete")

# Add percentage label dataframe
percentage_label <- data.frame(
  x = 5.5,
  y = 5.5,
  label = paste0(total_percentage, "%")
)

# Store the partial square information separately
partial_square <- data.frame(
  x = (partial_square_position - 1) %% n_cols + 1,
  y = ceiling(partial_square_position / n_cols)
)

# Plot: Base grid
p <- ggplot(grid_df, aes(x = x, y = y)) +
  # Add softer shadow effect
  geom_rect(aes(xmin = x - 0.42, xmax = x + 0.42, 
                ymin = y - 0.42, ymax = y + 0.42),
            fill = "#F0F0F0", color = NA) +
  # Main squares
  geom_rect(aes(xmin = x - 0.45, xmax = x + 0.45, 
                ymin = y - 0.45, ymax = y + 0.45, 
                fill = status),
            color = color_border, size = border_size) +
  # Improved color mapping - now only two categories
  scale_fill_manual(values = c("Completed" = color_completed, 
                               "Incomplete" = color_incomplete)) +
  scale_y_reverse() +
  coord_equal() +
  # Add large centered percentage label with pastel coloring
  geom_text(data = percentage_label, aes(x = x, y = y, label = label),
            size = 20, family = "roboto", fontface = "bold", color = "#92A8D1", alpha = 0.85) +
  # Improved theme
  theme_void() +
  theme(
    legend.position = "bottom",
    legend.title = element_text(family = "opensans", size = 12, face = "bold"),
    legend.text = element_text(family = "opensans", size = 10),
    legend.key.size = unit(0.8, "cm"),
    plot.title = element_text(family = "roboto", size = 16, face = "bold", hjust = 0.5, margin = margin(b = 20)),
    plot.subtitle = element_text(family = "opensans", size = 12, hjust = 0.5, margin = margin(b = 20)),
    plot.caption = element_text(family = "opensans", size = 9, hjust = 0.5, margin = margin(t = 20)),
    plot.background = element_rect(fill = "white", color = NA),
    plot.margin = margin(t = 20, r = 20, b = 20, l = 20)
  ) +
  labs(
    title = "Project Completion Waffle Chart",
    subtitle = paste0("Current Progress: ", total_percentage, "%"),
    fill = "Status",
    caption = paste0("Note: Square #", partial_square_position, " is partially filled (", 
                     partial_first, ":", partial_second, " ratio) to represent exactly ", 
                     total_percentage, "% completion")
  ) +
  # Add a very subtle pastel background
  theme(plot.background = element_rect(fill = "#FCFCFC", color = NA))
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Add partially filled square (blue part) - now handled separately
if(partial_square_position <= total_squares) {
  p <- p + geom_rect(data = partial_square,
                     aes(xmin = x - 0.45,
                         xmax = x - 0.45 + 0.9 * split_ratio,
                         ymin = y - 0.45,
                         ymax = y + 0.45),
                     fill = color_completed, color = NA)
}

# Display the plot
print(p)

# Save high-quality chart
ggsave("beautiful_waffle_chart.png", p, width = 8, height = 8, dpi = 300)