# Load the required libraries
library(ggplot2)
library(dplyr)
##
## 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
library(googlesheets4)
library(plotly) # For interactive plot
##
## 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
# Read the survey data from Google Sheets
url <- "https://docs.google.com/spreadsheets/d/1zxn9liBPwH_Sm64A78-jzyVzVTCzggw1mJLldSrwNVE/edit#gid=1655556458"
SURVEY <- read_sheet(url)
## ! Using an auto-discovered, cached token.
## To suppress this message, modify your code or options to clearly consent to
## the use of a cached token.
## See gargle's "Non-interactive auth" vignette for more details:
## <https://gargle.r-lib.org/articles/non-interactive-auth.html>
## ℹ The googlesheets4 package is using a cached token for
## 'jvaugh30@vols.utk.edu'.
## Auto-refreshing stale OAuth token.
## ✔ Reading from "Normal Distribution Survey (Responses)".
## ✔ Range 'Form Responses 1'.
# Define column names
NAMES <- c("Time", "Sleep", "Teeth",
"WeeklyFastFood", "PhoneHours",
"TimesAbroad", "DailyCupsWater",
"MinMusic23", "GPA", "OneMinuteMile",
"StatesTravlled", "WeeklySoda", "DailySoda")
# Rename columns
names(SURVEY) <- NAMES
# Calculate summary statistics for MinMusic23
min_music_summary <- SURVEY$MinMusic23 %>%
summary()
# Calculate mean and median
mean_min_music <- mean(SURVEY$MinMusic23, na.rm = TRUE)
median_min_music <- median(SURVEY$MinMusic23, na.rm = TRUE)
# Calculate standard deviation
sd_min_music <- sd(SURVEY$MinMusic23, na.rm = TRUE)
# Create a plot for MinMusic23 combining histogram and density plot
p <- ggplot(data = SURVEY, aes(x = MinMusic23)) +
geom_histogram(aes(y = ..density..), fill = "#8A360F", color = "black", alpha = 0.7, bins = 20) + # Burnt Sienna
geom_density(color = "blue", alpha = 0.7) + # Add density plot overlay
geom_vline(aes(xintercept = mean_min_music, color = "Mean"), size = 1.25) + # Dark Green
geom_vline(aes(xintercept = median_min_music, color = "Median"), size = 1.25) + # Dark Magenta
scale_color_manual(values = c("Mean" = "#006400", "Median" = "#8B008B")) + # Assign colors to Mean and Median
labs(title = "Distribution of Minutes of Music Listened to in 2023",
x = "Minutes of Music Listened to in 2023",
y = "Density") + # Change y-axis label to "Density"
theme_minimal() +
theme(legend.position = "top") # Move legend to the top
## Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
## ℹ Please use `linewidth` instead.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Convert to interactive plot
gp <- ggplotly(p)
## Warning: The dot-dot notation (`..density..`) was deprecated in ggplot2 3.4.0.
## ℹ Please use `after_stat(density)` instead.
## ℹ The deprecated feature was likely used in the ggplot2 package.
## Please report the issue at <https://github.com/tidyverse/ggplot2/issues>.
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
# Print the interactive plot
print(gp)