# Load necessary libraries
pacman::p_load(pacman, ggplot2, dplyr, gridExtra, lomb)

# Load the data
data <- read.csv("TabbysStarFlux.csv")

# Rename the 'Time.BJD.2454833.' column to 'time'
names(data)[names(data) == 'Time.BJD.2454833.'] <- 'time'

# Ensure the 'time' column is numeric
data$time <- as.numeric(data$time)

# Create a scatter plot with a line for the flux over time
p1 <- ggplot(data, aes(x = time, y = Flux)) +
  geom_line(color = '#3399ff') + 
  geom_point() + 
  geom_errorbar(aes(ymin = Flux - `Uncertainty.1.std..dev.`, ymax = Flux + `Uncertainty.1.std..dev.`), width = 0.1) +
  labs(title = "Tabby's Star Flux Over Time", 
       x = "Time (BJD - 2454833)", 
       y = "Flux") +
  theme()

# LOESS (locally estimated scatterplot smoothing)
p2 <- ggplot(data, aes(x = time, y = Flux)) +
  geom_line(color = '#3399ff') +
  geom_smooth(method = 'loess', color = '#cc0033', se = FALSE) +
  labs(title = "Smoothed Flux of Tabby's Star Over Time", 
       x = "Time (BJD - 2454833)", 
       y = "Flux") +
  theme()

# Histogram of Flux Values
p3 <- ggplot(data, aes(x = Flux)) +
  geom_histogram(binwidth = 0.001, fill = '#3399ff', color = '#000000') +
  labs(title = "Histogram of Tabby's Star Flux", 
       x = "Flux", 
       y = "Frequency") +
  theme()

# Error vs. Flux Plot
p4 <- ggplot(data, aes(x = Flux, y = `Uncertainty.1.std..dev.`)) +
  geom_point() +
  geom_smooth(method = 'lm', color = '#cc0033') +
  labs(title = "Uncertainty vs. Flux", 
       x = "Flux", 
       y = "Uncertainty (1 std. dev)") +
  theme()

# Combine the first four plots
grid.arrange(p1, p2, p3, p4, nrow = 2, ncol = 2)
## `geom_smooth()` using formula = 'y ~ x'
## `geom_smooth()` using formula = 'y ~ x'

# Time Between Dips (Inter-Dip Time Intervals)
# Set a threshold for dips 
dip_threshold <- 0.99

# Identify rows where the flux is below the threshold
dip_data <- data[data$Flux < dip_threshold, ]

# Calculate time intervals between dips
dip_data$time_diff <- c(NA, diff(dip_data$time))

# Handle NA values: filter out rows with NA in 'time_diff'
dip_data <- dip_data[!is.na(dip_data$time_diff), ]

# Plot the time intervals between dips
p5 <- ggplot(dip_data, aes(x = time, y = time_diff)) +
  geom_point(color = '#000000') +
  geom_line(color = '#3399ff') +
  labs(title = "Time Intervals Between Dips in Tabby's Star Flux", 
       x = "Time (BJD - 2454833)", 
       y = "Time Interval Between Dips") +
  theme()

# Lomb-Scargle Periodogram for Frequency
# Perform the Lomb-Scargle periodogram (Frequency)
lsp_result <- lsp(data$Flux, times = data$time, type = "frequency", plot = FALSE)

# Create Lomb-Scargle Periodogram plot for frequency
p6 <- ggplot(data.frame(lsp_result$scanned, lsp_result$power), aes(x = lsp_result$scanned, y = lsp_result$power)) +
  geom_line(color = '#3399ff') +
  labs(title = "Lomb-Scargle Periodogram (Frequency)", x = "Frequency", y = "Power") +
  theme()

# Lomb-Scargle Periodogram for Period
# Perform the Lomb-Scargle periodogram (Period)
lsp1_result <- lsp(data$Flux, times = data$time, type = "period", plot = FALSE)

# Create Lomb-Scargle Periodogram plot for period
p7 <- ggplot(data.frame(lsp1_result$scanned, lsp1_result$power), aes(x = lsp1_result$scanned, y = lsp1_result$power)) +
  geom_line(color = '#3399ff') +
  labs(title = "Lomb-Scargle Periodogram (Period)", x = "Period", y = "Power") +
  theme()

# Create text outputs for the summary of each Lomb-Scargle result
summary_freq <- capture.output(summary(lsp_result))
summary_period <- capture.output(summary(lsp1_result))

# Convert the summary outputs to grobs (graphical objects) for display and center the text
grob_freq <- grid::textGrob(paste(summary_freq, collapse = "\n"), 
                            just = "center", gp = grid::gpar(fontsize = 11))
grob_period <- grid::textGrob(paste(summary_period, collapse = "\n"), 
                              just = "center", gp = grid::gpar(fontsize = 11))

# Arrange the Lomb-Scargle plots and their summaries in a 4x4 grid 
grid.arrange(
  gridExtra::arrangeGrob(p6, grob_freq, nrow = 2, heights = c(2, 1)),
  gridExtra::arrangeGrob(p7, grob_period, nrow = 2, heights = c(2, 1)),
  nrow = 1, ncol = 2
)

# Load the data
data <- read.csv("KIC8462852_light_curve.csv")

# Convert the "Year" column to numeric by removing the " ± 2.5" text
data$Year <- as.numeric(sub(" ± 2.5", "", data$Year))

# Plotting KIC8462852 B magnitude over time with reversed y-axis
p1 <- ggplot(data, aes(x = Year, y = KIC8462852.B..mag.)) +
  geom_line(color = "#330099") +
  geom_point(color = "#330099") +
  labs(title = "Magnitude of KIC8462852 Over Time",
       x = "Year",
       y = "KIC8462852 B (mag)") +
  scale_y_reverse() +  # Reverse the y-axis
  theme()

# Adding comparison plots for the other stars with reversed y-axis
p2 <- ggplot(data, aes(x = Year)) +
  geom_line(aes(y = KIC8462852.B..mag., color = "KIC8462852")) +
  geom_line(aes(y = TYC.3162.1001.1.B..mag., color = "TYC 3162-1001-1")) +
  geom_line(aes(y = TYC.3162.879.1.B..mag., color = "TYC 3162-879-1")) +
  labs(title = "Magnitude Comparison of Stars Over Time",
       x = "Year",
       y = "Magnitude (B)") +
  scale_y_reverse() +  # Reverse the y-axis
  scale_color_manual(values = c("KIC8462852" = "#330099", 
                                "TYC 3162-1001-1" = "#cc0033", 
                                "TYC 3162-879-1" = "#339900")) +
  theme()

# Combine the plots
grid.arrange(p1, p2, nrow = 1, ncol = 2)