# 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_b_u_t50_type1_error.xlsx"

# Read the data from the Excel file
blinded_data <- read_excel(file_path, sheet = "Blinded")
unblinded_data <- read_excel(file_path, sheet = "Unblinded")

# Add a column to indicate the source of the data
blinded_data$`Interim Type` <- "Blinded"
unblinded_data$`Interim Type` <- "Unblinded"

# Combine the data
combined_data <- bind_rows(blinded_data, unblinded_data)

# Filter the data
filtered_data <- combined_data[combined_data$`Treatment Rate Assumption` == 0.5, ]

# 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 = `Interim Type`)) +
  geom_point(size = 1) + 
  geom_line() +
  labs(
    title = "Significance vs Actual Event Rates (H0: Treatment = Placebo)\n(Assumption: 70% vs 50%; Interim at 50%)",
    x = "Actual Event Rate (Treatment = Placebo)",
    y = "Probability of Claiming Success",
    color = "Interim Type"
  ) +
  scale_x_continuous(limits = c(0, 1.0), breaks = seq(0, 1.0, by = 0.1)) +
  scale_y_continuous(limits = c(0, 0.15), breaks = seq(0, 0.15, by = 0.01)) +
  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
  )
## 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.
# Save the plot
ggsave("C:/Users/szhuo/Downloads/PhD/final_CP/final_plots/CP_70V50_Sig_t50_TypeI_error_2D.png", plot = p, width = 8, height = 6)

p