library(ggplot2) library(dplyr) library(gridExtra) library(patchwork) library(scales)
theme_set( theme_minimal(base_size = 12) + theme( plot.title = element_text(size = 16, face = “bold”, hjust = 0.5, margin = margin(b = 10)), plot.subtitle = element_text(size = 12, hjust = 0.5, margin = margin(b = 10)), axis.title = element_text(size = 13, face = “bold”), axis.text = element_text(size = 11), legend.position = “bottom”, legend.title = element_text(size = 12, face = “bold”), panel.grid.minor = element_blank(), panel.border = element_rect(fill = NA, color = “gray80”, size = 0.5), plot.background = element_rect(fill = “white”, color = NA) ) )
url <- “https://raw.githubusercontent.com/tmatis12/datafiles/main/normtemp.csv” heart_data <- read.csv(url)
males <- heart_data[heart_data\(gender == 1, ] females <- heart_data[heart_data\)gender == 2, ]
male_heart_rate <- males$heart_rate cat(“========== MALE HEART RATE STATISTICS ==========”) cat(“Sample Size:”, length(male_heart_rate), “”) cat(“Mean:”, round(mean(male_heart_rate), 2), “”) cat(“Median:”, round(median(male_heart_rate), 2), “”) cat(“SD:”, round(sd(male_heart_rate), 2), “”) cat(“Min:”, min(male_heart_rate), “”) cat(“Max:”, max(male_heart_rate), “”) cat(“Q1:”, round(quantile(male_heart_rate, 0.25), 2), “”) cat(“Q3:”, round(quantile(male_heart_rate, 0.75), 2), “”) cat(“IQR:”, round(IQR(male_heart_rate), 2), “”)
female_heart_rate <- females$heart_rate cat(“========== FEMALE HEART RATE STATISTICS ==========”) cat(“Sample Size:”, length(female_heart_rate), “”) cat(“Mean:”, round(mean(female_heart_rate), 2), “”) cat(“Median:”, round(median(female_heart_rate), 2), “”) cat(“SD:”, round(sd(female_heart_rate), 2), “”) cat(“Min:”, min(female_heart_rate), “”) cat(“Max:”, max(female_heart_rate), “”) cat(“Q1:”, round(quantile(female_heart_rate, 0.25), 2), “”) cat(“Q3:”, round(quantile(female_heart_rate, 0.75), 2), “”) cat(“IQR:”, round(IQR(female_heart_rate), 2), “”)
male_hist <- ggplot(males, aes(x = heart_rate)) + geom_histogram(aes(y = after_stat(density)), bins = 12, fill = “steelblue”, color = “navy”, alpha = 0.7) + geom_density(color = “firebrick”, size = 1.2) + stat_function(fun = dnorm, args = list(mean = mean(male_heart_rate), sd = sd(male_heart_rate)), color = “darkorange”, size = 1, linetype = “dashed”) + labs(title = “Male Resting Heart Rate Distribution”, subtitle = paste0(“n =”, length(male_heart_rate), “, Mean =”, round(mean(male_heart_rate), 1), ” bpm, SD = “, round(sd(male_heart_rate), 1),” bpm”), x = “Heart Rate (beats per minute)”, y = “Density”) + theme_minimal()
print(male_hist)
female_hist <- ggplot(females, aes(x = heart_rate)) + geom_histogram(aes(y = after_stat(density)), bins = 12, fill = “hotpink”, color = “darkred”, alpha = 0.7) + geom_density(color = “steelblue”, size = 1.2) + stat_function(fun = dnorm, args = list(mean = mean(female_heart_rate), sd = sd(female_heart_rate)), color = “darkorange”, size = 1, linetype = “dashed”) + labs(title = “Female Resting Heart Rate Distribution”, subtitle = paste0(“n =”, length(female_heart_rate), “, Mean =”, round(mean(female_heart_rate), 1), ” bpm, SD = “, round(sd(female_heart_rate), 1),” bpm”), x = “Heart Rate (beats per minute)”, y = “Density”) + theme_minimal()
print(female_hist)
male_qq <- ggplot(males, aes(sample = heart_rate)) + stat_qq(color = “steelblue”, size = 2.5, alpha = 0.7) + stat_qq_line(color = “firebrick”, size = 1.2) + labs(title = “Normal Q-Q Plot: Male Heart Rate”, subtitle = “Checking normality assumption”, x = “Theoretical Quantiles”, y = “Sample Quantiles”) + theme_minimal()
print(male_qq)
female_qq <- ggplot(females, aes(sample = heart_rate)) + stat_qq(color = “hotpink”, size = 2.5, alpha = 0.7) + stat_qq_line(color = “steelblue”, size = 1.2) + labs(title = “Normal Q-Q Plot: Female Heart Rate”, subtitle = “Checking normality assumption”, x = “Theoretical Quantiles”, y = “Sample Quantiles”) + theme_minimal()
print(female_qq)
heart_data\(gender_label <- ifelse(heart_data\)gender == 1, “Male”, “Female”)
boxplot_comparison <- ggplot(heart_data, aes(x = gender_label, y = heart_rate, fill = gender_label)) + geom_violin(alpha = 0.3, trim = FALSE) + geom_boxplot(width = 0.4, alpha = 0.8, size = 1) + geom_jitter(width = 0.2, alpha = 0.4, size = 1.5) + scale_fill_manual(values = c(“Male” = “steelblue”, “Female” = “hotpink”)) + labs(title = “Comparison of Resting Heart Rate by Gender”, subtitle = paste0(“Male: n =”, nrow(males), “, Mean =”, round(mean(male_heart_rate), 1), ” bpm | Female: n = “, nrow(females),”, Mean = “, round(mean(female_heart_rate), 1),” bpm”), x = “Gender”, y = “Heart Rate (beats per minute)”) + theme_minimal() + theme(legend.position = “none”)
print(boxplot_comparison)
library(patchwork)
p8 <- ggplot(heart_data, aes(x = heart_rate, fill = gender_label, color = gender_label)) + geom_density(alpha = 0.4, size = 1.2) + scale_fill_manual(values = c(“Male” = “steelblue”, “Female” = “hotpink”)) + scale_color_manual(values = c(“Male” = “navy”, “Female” = “darkred”)) + labs(title = “Heart Rate Density Comparison”, subtitle = “Overlapping distributions showing gender differences”, x = “Heart Rate (beats per minute)”, y = “Density”, fill = “Gender”, color = “Gender”) + theme_minimal()
p9 <- ggplot(heart_data, aes(sample = heart_rate, color = gender_label)) + stat_qq(size = 2, alpha = 0.7) + stat_qq_line(size = 1) + facet_wrap(~gender_label, scales = “free”) + scale_color_manual(values = c(“Male” = “steelblue”, “Female” = “hotpink”)) + labs(title = “QQ Plots by Gender”, subtitle = “Assessing normality separately for each gender”, x = “Theoretical Quantiles”, y = “Sample Quantiles”) + theme_minimal() + theme(legend.position = “none”)
combined_comparison <- (p8 / p9) + plot_annotation( title = “Gender Comparison of Heart Rate Distributions”, theme = theme( plot.title = element_text(size = 18, face = “bold”, hjust = 0.5, color = “navy”, margin = margin(b = 10)) ) )
print(combined_comparison)
summary_stats <- heart_data %>% group_by(gender_label) %>% summarise( n = n(), mean = round(mean(heart_rate), 2), median = round(median(heart_rate), 2), sd = round(sd(heart_rate), 2), min = min(heart_rate), max = max(heart_rate), Q1 = round(quantile(heart_rate, 0.25), 2), Q3 = round(quantile(heart_rate, 0.75), 2), IQR = round(IQR(heart_rate), 2) )
cat(“SUMM STATISTICS BY GENDER ”) print(summary_stats)