#rm(list=ls(all=TRUE))
library(dplyr)
library(glue)
library(scales)
library(ggplot2)
library(plotly)
library(reshape2)
library(purrr)
library(haven)
library(DT)
library(stringr)
library(boot)
library(tinytex)
library(gtsummary)
library(crosstalk)
library(kableExtra)
library(filenamer)
library(xlsx)
library(forcats)
 CDCgv<-read.csv("C:\\Users\\echen\\Documents\\UX023_CL301\\R\\CDCgv.csv")
 ht_mean_w.f<-read.csv("C:\\Users\\echen\\Documents\\UX023_CL301\\R\\ht_mean_w.csv")

 CDCgv.f<-CDCgv[which(CDCgv$SEX=="F"),]

LNplot1 <- ht_mean_w.f %>%
  highlight_key(~hightlight) %>%
  ggplot() +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, aes(x = ageY, y = P3), color = "grey") +
  geom_text(data = CDCgv.f, aes(x = 14, y = max(CDCgv.f$P3)-3, label = "3%"), hjust = "left", vjust = 0) +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P5), color = "grey") +
  geom_text(data = CDCgv.f, aes(x = 15, y = max(CDCgv.f$P5)-1, label = "5%"), hjust = "left", vjust = 0) +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P10), color = "grey") +
  geom_text(data = CDCgv.f, aes(x = 16, y = max(CDCgv.f$P10), label = "10%"), hjust = "left", vjust = 0) +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P25), color = "grey") +
  geom_text(data = CDCgv.f, aes(x = 17, y = max(CDCgv.f$P25), label = "25%"), hjust = "left", vjust = 0) +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P50), color = "grey") +
  geom_text(data = CDCgv.f, aes(x = 18, y = max(CDCgv.f$P50), label = "50%"), hjust = "left", vjust = 0) +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P75), color = "grey") +
  geom_text(data = CDCgv.f, aes(x = 19, y = max(CDCgv.f$P75), label = "75%"), hjust = "left", vjust = 0) +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P90), color = "grey") +
  geom_text(data = CDCgv.f, aes(x = 20, y = max(CDCgv.f$P90), label = "90%"), hjust = "left", vjust = 0) +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P95), color = "grey") +
  geom_text(data = CDCgv.f, aes(x = 21, y = max(CDCgv.f$P95), label = "95%"), hjust = "left", vjust = 0) +
  geom_line(data=ht_mean_w.f,
    aes(x = age, y = meanval, linetype = age_cat),
    alpha = 1,
    size = 0.3,
  ) +
  scale_linetype_manual(values = c("solid", "dashed")) +
  geom_point(size = 3, alpha = 1, aes(x = age, y = meanval, text = labeltext, color = age_cat, shape = age_cat)) +
  scale_shape_manual(values = c(19,1)) +
  scale_color_manual(values = c("red","blue"))


ggplotly(LNplot1) %>%
  layout(legend = list(orientation = "h", x = 0.5, y = -0.2, xanchor = "center"))
LNplot1 <- ht_mean_w.f %>%
  highlight_key(~hightlight) %>%
  ggplot() +
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, aes(x = ageY, y = P3), color = "grey") +
 
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P5), color = "grey") +

  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P10), color = "grey") +
 
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P25), color = "grey") +

  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P50), color = "grey") +

  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P75), color = "grey") +
 
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P90), color = "grey") +
  
  geom_line(data = CDCgv.f, size = 0.3, alpha = 0.8, shape = 1, aes(x = ageY, y = P95), color = "grey") +
 
  geom_line(data=ht_mean_w.f,
    aes(x = age, y = meanval, linetype = age_cat),
    alpha = 1,
    size = 0.3,
  ) +
  scale_linetype_manual(values = c("solid", "dashed")) +
  geom_point(size = 3, alpha = 1, aes(x = age, y = meanval, text = labeltext, color = age_cat, shape = age_cat)) +
  scale_shape_manual(values = c(19,1)) +
  scale_color_manual(values = c("red","blue"))

LNplotly<-ggplotly(LNplot1)
# Get the Y value from P3 as minY and Y value from P95 as maxY
minY <- max(CDCgv.f$P3)
maxY <- max(CDCgv.f$P95)

# Calculate the range and expand it to fit 8 labels, assuming each label is approximately 4px high
# The number 4 here is arbitrary and can be adjusted based on the actual height of your labels
label_height_px <- 4
total_label_height <- label_height_px * 8
y_range <- maxY - minY
expansion_factor <- total_label_height / y_range
expanded_minY <- minY - (y_range * expansion_factor * 0.30) # 15% extra space below
expanded_maxY <- maxY + (y_range * expansion_factor *0) # 0% extra space above

# Determine the step size between each label based on the expanded range
step_size <- (expanded_maxY - expanded_minY) / 7 # 8 labels means 7 steps between them

# Get the last x value (assuming it is the same for all percentiles)
last_x <- max(CDCgv.f$ageY)

# Add annotations for each percentile line
percentiles <- c('3%', '5%', '10%', '25%', '50%', '75%', '90%', '95%')
for (i in seq_along(percentiles)) {
  p <- percentiles[i]
  
  # Calculate the y position for the current label
  label_y <- expanded_minY + (i - 1) * step_size
  
  LNplotly <- LNplotly %>%
    add_annotations(
      x = last_x,  # Adding a small constant to x to avoid overlapping the line
      y = label_y,
      text = p,  # The percentile label
      showarrow = FALSE,
      xanchor = 'left',
      yanchor = 'bottom'
    )
}
# Update layout to adjust legend
 LNplotly %>%
  layout(legend = list(orientation = "h", x = 0.5, y = -0.2, xanchor = "center"))