library(ggplot2)
## Warning: 程辑包'ggplot2'是用R版本4.2.3 来建造的
library(ggridges)
## Warning: 程辑包'ggridges'是用R版本4.2.2 来建造的
# Create density plot data
set.seed(123)
density_data <- data.frame(
  name = factor(rep(c("Drug A", "Drug B", "Drug C", "Drug D"), each = 100),
                levels = c("Drug A", "Drug B", "Drug C", "Drug D")),
  ratio_ac50_adjust = c(
    rnorm(100, 1.2, 0.2),
    rnorm(100, 0.8, 0.15),
    rnorm(100, 1.5, 0.25),
    rnorm(100, 0.9, 0.18)
  )
)
head(density_data)
##     name ratio_ac50_adjust
## 1 Drug A          1.087905
## 2 Drug A          1.153965
## 3 Drug A          1.511742
## 4 Drug A          1.214102
## 5 Drug A          1.225858
## 6 Drug A          1.543013
# Create statistics data
stats_data <- data.frame(
  name = factor(c("Drug A", "Drug B", "Drug C", "Drug D"),
                levels = c("Drug A", "Drug B", "Drug C", "Drug D")),
  mean = c("1.20", "0.80", "1.50", "0.90"),
  sd = c("0.20", "0.15", "0.25", "0.18"),
  n = c("100", "100", "100", "100")
)

stats_data
##     name mean   sd   n
## 1 Drug A 1.20 0.20 100
## 2 Drug B 0.80 0.15 100
## 3 Drug C 1.50 0.25 100
## 4 Drug D 0.90 0.18 100
# Create single plot
ggplot(density_data, aes(x = ratio_ac50_adjust, y = name, fill = stat(x))) +
  # Draw grid lines
  geom_segment(data = stats_data,
               aes(x = 0, xend = 2, y = name, yend = name),
               linetype = "dashed", color = "gray", linewidth = 0.5) +
  
  # Draw y-axis line
  geom_segment(aes(x = 0, xend = 0, 
                   y = 0.6, yend = 5.4),
               linewidth = 0.5, color = "black") +
  
  # Draw x-axis line (up to 2)
  geom_segment(aes(x = 0, xend = 2.2, 
                   y = 0.6, yend = 0.6),
               linewidth = 0.5, color = "black") +
  
  # Draw density ridges
  geom_density_ridges_gradient(scale = 2, rel_min_height = 0.01) +
  scale_fill_gradient(low = "#4BAAFF", high = "red") +
  
  # Add statistics text
  geom_text(data = stats_data, 
            aes(x = 2.5, y = name, label = mean),
            hjust = 0.5, size = 5) +
  geom_text(data = stats_data, 
            aes(x = 3.0, y = name, label = sd),
            hjust = 0.5, size = 5) +
  geom_text(data = stats_data, 
            aes(x = 3.5, y = name, label = n),
            hjust = 0.5, size = 5) +
  
  # Add x-axis ticks
  geom_segment(data = data.frame(x = seq(0, 2, 0.5)),
               aes(x = x, xend = x, 
                   y = 0.6, yend = 0.5),
               linewidth = 0.5, color = "black") +
  
  # Add y-axis ticks
  geom_segment(data = stats_data,
               aes(x = 0, xend = -0.05,
                   y = name, yend = name),
               linewidth = 0.5, color = "black") +
  
  # Set axis ranges and labels
  scale_x_continuous(limits = c(-0.1, 4),
                     breaks = c(seq(0, 2, 0.5), 2.5, 3.0, 3.5),
                     labels = c(seq(0, 2, 0.5), "Mean", "SD", "N"),
                     expand = c(0, 0)) +
  scale_y_discrete(expand = expansion(mult = c(0.1, 0.1))) +
  labs(x = expression(IC[50]*","*"comb"~"/"~IC[50]*","*"single"),
       y = "") +
  
  # Theme settings
  theme_minimal() +
  theme(
    legend.position = "none",            # Remove legend
    panel.grid = element_blank(),        # Remove panel grid
    axis.text = element_text(size = 14, color = "black"),  # Set axis text
    axis.title.x = element_text(size = 14, color = "black", margin = margin(t = 10)),  # Set x-axis title
    axis.line = element_blank(),         # Remove default axis lines
    axis.ticks = element_blank(),        # Remove default ticks
    panel.border = element_blank(),      # Remove panel border
    panel.background = element_blank()   # Remove panel background
  )
## Warning: `stat(x)` was deprecated in ggplot2 3.4.0.
## i Please use `after_stat(x)` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
## Picking joint bandwidth of 0.0666

dir_path <- "C:\\Users\\liyix\\OneDrive\\Desktop\\"
ggsave(filename = paste0(Sys.Date(), "_", "density_table.tif"), plot = last_plot(), 
       device = "tiff", path = dir_path,
       scale = 1, width = 21, height = 21, units = "cm",
       dpi = 300, limitsize = TRUE, compression = "lzw")
## Picking joint bandwidth of 0.0666