# Load required libraries
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(hexbin)
## Warning: package 'hexbin' was built under R version 4.4.1
library(tidyr)

# Define dimensions
x_dim <- 4
y_dim <- 5

# Create grid data
set.seed(123)  # for reproducibility
# Create grid data with values ranging from negative to positive
set.seed(123)  # for reproducibility
grid_data <- expand.grid(x = 0:(x_dim-1), y = 0:(y_dim-1)) %>%
  mutate(value = seq(-2, 2, length.out = n()))
grid_data
##    x y      value
## 1  0 0 -2.0000000
## 2  1 0 -1.7894737
## 3  2 0 -1.5789474
## 4  3 0 -1.3684211
## 5  0 1 -1.1578947
## 6  1 1 -0.9473684
## 7  2 1 -0.7368421
## 8  3 1 -0.5263158
## 9  0 2 -0.3157895
## 10 1 2 -0.1052632
## 11 2 2  0.1052632
## 12 3 2  0.3157895
## 13 0 3  0.5263158
## 14 1 3  0.7368421
## 15 2 3  0.9473684
## 16 3 3  1.1578947
## 17 0 4  1.3684211
## 18 1 4  1.5789474
## 19 2 4  1.7894737
## 20 3 4  2.0000000
# Create hexagon coordinates
hex_coords <- hexbin::hexcoords(0.5)

# Function to generate hexagon vertices
generate_hexagon <- function(x, y) {
  data.frame(
    hex_x = x + hex_coords$x,
    hex_y = y + hex_coords$y
  )
}

# Generate hexagon data with consistent spacing
hex_data <- grid_data %>%
  mutate(
    center_x = x + (y %% 2) * 0.5,
    center_y = y * sqrt(3)/2
  ) %>%
  rowwise() %>%
  mutate(
    hexagon = list(generate_hexagon(center_x, center_y))
  ) %>%
  unnest(hexagon) %>%
  group_by(x, y, value) %>%
  mutate(vertex = row_number()) %>%
  ungroup()

# Define pastel color palette
pastel_colors <- c("#FFB3BA", "#BAFFC9", "#BAE1FF", "#FFFFBA", "#FFDFBA")

# Create the plot
ggplot(hex_data, aes(hex_x, hex_y, group = interaction(x, y))) +
  geom_polygon(aes(fill = value), color = 'white') +
  scale_fill_viridis_c(option = "C") + coord_equal()  # Ensure equal aspect ratio

p <- ggplot(hex_data, aes(hex_x, hex_y, group = interaction(x, y))) +
  geom_polygon(aes(fill = value), color = 'white') +
  scale_fill_gradientn(colors = pastel_colors) +
  theme_void() +  # Remove all background elements
  theme(
    plot.background = element_rect(fill = "white", color = NA),
    legend.position = "none"  # Remove the legend
  ) +
  coord_equal()  # Ensure equal aspect ratio

# Display the plot
print(p)

# Define custom three-color palette
custom_colors <- c("#FF9999", "gray90", "#99CCFF")

# Create the plot
p <- ggplot(hex_data, aes(hex_x, hex_y, group = interaction(x, y))) +
  geom_polygon(aes(fill = value), color = 'white') +
  scale_fill_gradientn(colors = custom_colors, limits = c(-2, 2)) +
  theme_void() +  # Remove all background elements
  theme(
    plot.background = element_rect(fill = "white", color = NA),
    legend.position = "right"  # Remove the legend
  ) +
  coord_equal()  # Ensure equal aspect ratio

# Display the plot
print(p)

# Optional: Save the plot
# ggsave("hexagonal_heatmap_custom_gradient.png", p, width = 8, height = 6, dpi = 300, bg = "white")

# Verify the number of hexagons and value range
num_hexagons <- hex_data %>% 
  select(x, y) %>% 
  distinct() %>% 
  nrow()

value_range <- range(hex_data$value)

print(paste("Number of hexagons:", num_hexagons))
## [1] "Number of hexagons: 20"
print(paste("Value range:", paste(value_range, collapse = " to ")))
## [1] "Value range: -2 to 2"
# Save the plot as TIF
ggsave(paste0("colorful_gradient_hexmap_", x_dim, "x", y_dim, "_hexagons.tif"), 
       plot = p, width = 12, height = 15, dpi = 300, compression = "lzw")