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/real_final/one_sided/H0_Blinded_Unblinded_one_sided_filter.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() +
    # Add a solid reference line at significance level 0.05
    geom_hline(yintercept = 0.025, linetype = "solid", color = "black", size = 0.05) +
    labs(
      title = paste("Type I Error vs Actual Event Rates (H0: Treatment = Placebo)\n",
                    "(Assumed Control vs Treatment Event Rates: 70% vs ", name, "%)", sep = ""),
      x = "Actual Event Rate (Treatment = Placebo)",
      y = "Type I Error",
      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.05), breaks = seq(0, 0.05, by = 0.005)) +
    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
      aspect.ratio = 0.5  # Set the aspect ratio to make the plot wider and shorter
    )
  
  # Display the plot
  p
}

# Call the function for different rates
simu(rate = 0.2, name = 20)
## 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_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)