# Create example data
data_4 <- data.frame(
  Sample.ID = rep(1:10, each = 2),  # Replicate sample IDs 10 times (each 2 times)
  key = rep(c("A", 
              "B"), 10),  # Repeated keys for two categories
  value = c(
    4.1, 4.9, 5.1, 5.5, 5.2, 6.0, 5.8, 6.3, 7.1, 7.8,
    8.0, 8.5, 6.4, 5.6, 7.0, 6.2, 8.2, 5.3, 6.8, 9.0,
    5.7, 8.3, 4.2, 4.6, 5.9, 6.5, 6.1, 7.5, 6.9, 7.9,
    4.7, 5.0, 6.3, 6.7, 8.1, 7.4, 5.4, 8.4, 6.2, 7.2
  )
)

# View the first few rows of the data
head(data_4)
##   Sample.ID key value
## 1         1   A   4.1
## 2         1   B   4.9
## 3         2   A   5.1
## 4         2   B   5.5
## 5         3   A   5.2
## 6         3   B   6.0
# Data transformation: Apply a nonlinear transformation to the 'value' column
data_4$trans_value <- with(data_4, ifelse(
  value <= 5,  # If the value is less than or equal to 5
  (value - 4) / (5 - 4) * 0.9,  # Map 4-5 to 0-0.9
  0.9 + (value - 5) / (9 - 5) * 0.1  # Map 5-9 to 0.9-1
))

# View the transformed data
head(data_4)
##   Sample.ID key value trans_value
## 1         1   A   4.1      0.0900
## 2         1   B   4.9      0.8100
## 3         2   A   5.1      0.9025
## 4         2   B   5.5      0.9125
## 5         3   A   5.2      0.9050
## 6         3   B   6.0      0.9250
library(ggplot2)

# Create the plot using ggplot
ggplot() + 
  geom_tile(
    data = data_4,
    aes(x = key, y = Sample.ID, height = 1, width = 1, fill = trans_value),  # Define the data and aesthetics
    size = 0.2  # Line size around the tiles
  ) +  
  ylab("") + xlab("") +  # Remove axis labels
  scale_y_discrete(expand = c(0, 0), position = "right") +  # Y-axis setup
  scale_x_discrete(
    expand = c(0, 0),  # Remove extra space around the X-axis labels
    labels = c(
      "TDB_GSK3_alpha_virtural_primaryscreen_ratio.LogAC50" = "GSK3A",  # Renaming the first key
      "TDB_GSK3_beta_virtual_primaryscreen_ratio.LogAC50" = "GSK3B"  # Renaming the second key
    )
  ) +
  scale_fill_gradient2(
    low = "#4575B4",  # Low value color (blue)
    mid = "#E0F3F8",  # Middle value color (light blue)
    high = "#FC8D59",  # High value color (orange-red)
    midpoint = 0.5,  # Set the midpoint for color scaling (the transformed value of 4.5)
    limits = c(0, 1),  # Set the color scale limits to 0-1
    breaks = c(0, 0.45, 0.9, 1),  # Set the breaks for the legend
    labels = c("4", "4.5", "5", "9"),  # Corresponding labels for the breaks
    name = expression(paste(IC[50], " [μM]")),  # Legend title
    na.value = "gray90"  # Set missing values to gray
  ) +
  theme(
    legend.position = "top",  # Position the legend at the top
    axis.text.x = element_text(angle = 0, vjust = 0.5, hjust = 0.5, color = "black"),  # Customize x-axis text
    axis.text.y = element_blank(),  # Remove y-axis text
    axis.ticks = element_blank(),  # Remove axis ticks
    legend.key.height = unit(0.8, "line"),  # Set the height of the legend key
    legend.key.width = unit(1.5, "line"),  # Set the width of the legend key
    plot.background = element_rect(fill = "green")  # Set the background color of the plot
  )
## 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.