# compacted for better display on slides
df <- 6
alpha <- 0.05
critical_value <- qchisq(1 - alpha, df) # chisq funs come with
calculated_value <- 6.867 # base R!
x <- seq(0, 20, length.out = 1000) # length.out makes
y <- dchisq(x, df) # curve much smoother
data <- data.frame(x, y) # make x, y into dataframe
fig2 <- ggplot(data, aes(x = x, y = y)) +
geom_line(color = "blue", linewidth = 0.5) + # Chi-square curve
geom_ribbon(data = subset(data, x >= critical_value), # fill area
aes(ymax = y, ymin = 0),
fill = "green", alpha = 0.2) +
geom_ribbon(data = subset(data, x <= calculated_value), # fill area
aes(ymax = y, ymin = 0),
fill = "red", alpha = 0.2) +
geom_vline(xintercept = critical_value, # critical value line
color = "green", linetype = "dashed", linewidth = 0.5) +
geom_vline(xintercept = calculated_value, # our chi-square value line
color = "red", linetype = "dashed", linewidth = 0.5) +
labs(title = "Chi-Square Distribution (df=6) with CV corresponding to a=0.05",
x = "Chi-Square Value",
y = "Density")