# Load required libraries
library(ggplot2)
library(scales)

# Simulate example data for kinase_sum
# Most values are concentrated in the 1–100 range, with a few in 101–267
set.seed(123)
data_4 <- data.frame(
  kinase_sum = c(
    sample(1:50, 300, replace = TRUE),
    sample(51:100, 150, replace = TRUE),
    sample(101:267, 50, replace = TRUE)
  )
)
# Define key transformation parameters
split_point <- 50     # Boundary point to split the x-axis
x_min <- 1            # Minimum x-axis value
x_max <- 267          # Maximum x-axis value

# Forward transform: map 1–50 to [0, 0.5], and 51–267 to [0.5, 1]
split_transform <- function(x) {
  ifelse(x <= split_point,
         (x - x_min) / (split_point - x_min) * 0.5,
         0.5 + (x - (split_point + 1)) / (x_max - (split_point + 1)) * 0.5)
}

# Inverse transform: to restore original axis labels
split_inverse <- function(x) {
  ifelse(x <= 0.5,
         x_min + x / 0.5 * (split_point - x_min),
         (split_point + 1) + (x - 0.5) / 0.5 * (x_max - (split_point + 1)))
}

# Create a custom transformation for the x-axis
split_scale <- trans_new("split_custom", split_transform, split_inverse)
ggplot(data_4) +
  # Kernel density estimation on original kinase_sum data
  stat_density(aes(x = kinase_sum), color = "#B3CDE3", size = 1,
               alpha = 0.5, bw = 2.5, geom = "line", 
               position = "identity") +
  
  # Adjust y-axis spacing
  scale_y_continuous(expand = c(0.0003, 0.0003)) +
  
  # Apply custom x-axis transformation and set breaks
  scale_x_continuous(
    trans = split_scale,
    breaks = c(1, 10, 20, 30, 40, 50, 100, 150, 200, 267),
    limits = c(1, 267),
    expand = c(0.005, 0.005)
  ) +
  
  # Labels and theme
  labs(
    title = "(1–50 vs 51–267)",
    x = "Number", 
    y = "Density"
  ) +
  theme_minimal(base_size = 14) + 
  # geom_vline(aes(xintercept=max(AUC)),
  #            color="blue", linetype="dashed", size=1) +
  theme(
    plot.margin = margin(t = 10, r = 20, b = 10, l = 10),
    panel.spacing = unit(0.1, "cm"),
    legend.position = "top",
    legend.key = element_rect(colour = NA, fill = NA),
    legend.text = element_text(size = 14),
    legend.title = element_text(size = 14),
    axis.ticks = element_line(colour = "black", size = 0.5, linetype = "solid"),
    axis.line = element_line(colour = "black", size = 0.5, linetype = "solid"),
    axis.text = element_text(face = "plain", color = "black", family = "sans", size = 14),
    panel.background = element_rect(fill = "white", colour = "white", size = 0.5, linetype = "solid"),
    panel.grid.major = element_line(size = 1, linetype = 'dashed', colour = "white"),
    axis.title = element_text(color = "black", size = 14, face = "plain", family = "sans")
  )
## 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.
## Warning: The `size` argument of `element_line()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Warning: The `size` argument of `element_rect()` is deprecated as of ggplot2 3.4.0.
## ℹ Please use the `linewidth` argument instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.

##########################output
ggsave(filename = paste0(Sys.Date(),"-Number.tif"), plot = last_plot(), 
       device = "tiff", path = getwd(),
       scale = 1, width = 15, height = 12, units = "cm",
       dpi = 300, limitsize = TRUE, compression = "lzw")