# Load necessary libraries
library(readxl)
## Warning: package 'readxl' was built under R version 4.2.3
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 4.2.3
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.2.3
## 
## 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
# Define file path
file_path <- "C:/Users/szhuo/Downloads/PhD/final_CP/final_data/CP_t50_type1_error_hr_1000.xlsx"

combined_data <- read_excel(file_path, sheet = "Overall")

simu <- function(rate, name){
  
# Filter the data
filtered_data <- combined_data[combined_data$`Treatment Rate Assumption` == rate, ]
filtered_data <- filtered_data[filtered_data$`Type` != "Logrank", ]

# Create a new column for Treatment Difference
filtered_data$`Treatment Difference` <- filtered_data$`Actual Control Rate` - filtered_data$`Actual Treatment Rate`

# Ensure 'Significant at Final' is numeric
filtered_data$`Significant at Final` <- as.numeric(filtered_data$`Significant at Final`)


# Create the ggplot with a white background and dashed grid lines
p <- ggplot(filtered_data, aes(x = `Actual Treatment Rate`, y = `Significant at Final`, color = `Type`)) +
  geom_point(size = 1) + 
  geom_line() +
  labs(
      title = paste("Significance vs Actual Event Rates (H0: Treatment = Placebo)\n",
                    "(Assumption: 70% vs ", name, "%; Interim at 50%)", sep = ""),
    x = "Actual Event Rate (Treatment = Placebo)",
    y = "Probability of Claiming Success",
    color = "Type"
  ) +
  
  scale_x_continuous(limits = c(0, 1.0), breaks = seq(0, 1.0, by = 0.1)) +
  scale_y_continuous(limits = c(0, 0.1), breaks = seq(0, 0.1, by = 0.005), minor_breaks = seq(0, 0.1, by = 0.001)) +
  theme_minimal() +
  theme(
    panel.background = element_rect(fill = "white", color = NA),  # White panel background
    plot.background = element_rect(fill = "white", color = NA),   # White plot background
    panel.grid.major = element_line(color = "grey80", linetype = "dashed"),  # Dashed major grid lines
    panel.grid.minor = element_line(color = "grey90", linetype = "dashed"),  # Dashed minor grid lines
    panel.border = element_rect(colour = "black", fill = NA, size = 1),
    plot.title = element_text(hjust = 0.5, color = "black"),      # Black title text
    axis.text = element_text(color = "black"),                    # Black axis text
    axis.title = element_text(color = "black"),                   # Black axis titles
    legend.text = element_text(color = "black"),                  # Black legend text
    legend.title = element_text(color = "black"),                 # Black legend title
    plot.margin = margin(t = 10, r = 10, b = 10, l = 20)          # Increase left margin
  )


# file_name <- sprintf("C:/Users/szhuo/Downloads/PhD/final_CP/final_plots/CP_t50_type1_error_hr_10000_rec_1.png", name)

p

}

simu(rate=0.2, name=20)
## 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.

simu(rate=0.3, name=30)

simu(rate=0.4, name=40)

simu(rate=0.5, name=50)

simu(rate=0.6, name=60)