R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Load required libraries
library(plotly)
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(gsheet)

# Read the survey data from Google Sheets
SURVEY <- gsheet2tbl("https://docs.google.com/spreadsheets/d/1zxn9liBPwH_Sm64A78-jzyVzVTCzggw1mJLldSrwNVE/edit#gid=1655556458")

# Define column names
NAMES <- c("Time", "Sleep", "Teeth", 
           "WeeklyFastFood", "PhoneHours", 
           "TimesAbroad", "DailyCupsWater", 
           "MinMusic23", "GPA", "OneMinuteMile",
           "StatesTraveled", "WeeklySoda", "DailySoda")

# Rename columns
names(SURVEY) <- NAMES

# Calculate quantiles of PhoneHours
quantiles <- quantile(SURVEY$PhoneHours, probs = seq(0, 1, by = 0.01))

# Generate quantiles from a normal distribution with mean and standard deviation from the data
norm_quantiles <- qnorm(seq(0, 1, by = 0.01), mean = mean(SURVEY$PhoneHours), sd = sd(SURVEY$PhoneHours))

# Create data frame for plotting
plot_data <- data.frame(quantiles = quantiles, norm_quantiles = norm_quantiles, prob = seq(0, 1, by = 0.01))

# Create quantile plot
plot <- ggplot(plot_data, aes(x = norm_quantiles, y = quantiles, text = paste("Theoretical Quantile:", round(norm_quantiles, 2), "<br>Sample Quantile:", round(quantiles, 2)))) +
  geom_point() +
  geom_abline(slope = 1, intercept = 0, color = "blue", linetype = "dashed") +  # Add line of normality
  labs(x = "Theoretical Quantiles", y = "Sample Quantiles", title = "Quantile Plot of PhoneHours") +
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5),  # Center the title
    panel.grid.major = element_line(color = "lightgray", size = 0.5),  # Major gridlines
    panel.grid.minor = element_line(color = "lightgray", size = 0.2, linetype = "dotted")  # Minor gridlines
  )
## Warning: The `size` argument of `element_line()` 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.
# Make the plot interactive
ggplotly(plot, tooltip = "text")