# Load libraries
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 4.3.2
## Warning: package 'ggplot2' was built under R version 4.3.2
## Warning: package 'tidyr' was built under R version 4.3.2
## Warning: package 'readr' was built under R version 4.3.2
## Warning: package 'dplyr' was built under R version 4.3.2
## Warning: package 'stringr' was built under R version 4.3.2
## Warning: package 'forcats' was built under R version 4.3.2
## Warning: package 'lubridate' was built under R version 4.3.2
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr 1.1.4 ✔ readr 2.1.5
## ✔ forcats 1.0.0 ✔ stringr 1.5.1
## ✔ ggplot2 3.4.4 ✔ tibble 3.2.1
## ✔ lubridate 1.9.3 ✔ tidyr 1.3.1
## ✔ purrr 1.0.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(ggplot2)
library(dplyr)
library(kableExtra)
## Warning: package 'kableExtra' was built under R version 4.3.2
##
## Attaching package: 'kableExtra'
##
## The following object is masked from 'package:dplyr':
##
## group_rows
library(knitr)
## Warning: package 'knitr' was built under R version 4.3.2
library(cowplot)
## Warning: package 'cowplot' was built under R version 4.3.2
##
## Attaching package: 'cowplot'
##
## The following object is masked from 'package:lubridate':
##
## stamp
Read data
summary_stats_df <- "hw2_DataSet.csv"
if (file.exists("hw2_DataSet.csv")) {
print("File exists.")
} else {
print("File not found.")
}
## [1] "File exists."
summary_stats <- read.csv("hw2_DataSet.csv")
First quartile values
RandomA_FirstQuartile_Formatted <- quantile(summary_stats$random_A, 0.25)
RandomB_FirstQuartile_Formatted <- quantile(summary_stats$random_B, 0.25)
RandomC_FirstQuartile_Formatted <- quantile(summary_stats$random_C, 0.25)
RandomD_FirstQuartile_Formatted <- quantile(summary_stats$random_D, 0.25)
Third quartile values
RandomA_ThirdQuartile_Formatted <- quantile(summary_stats$random_A, 0.75)
RandomB_ThirdQuartile_Formatted <- quantile(summary_stats$random_B, 0.75)
RandomC_ThirdQuartile_Formatted <- quantile(summary_stats$random_C, 0.75)
RandomD_ThirdQuartile_Formatted <- quantile(summary_stats$random_D, 0.75)
Table
myTable <- data.frame(
Group = c("RandomA", "RandomB", "RandomC", "RandomD"),
Mean = c(RandomA_Mean_Formatted, RandomB_Mean_Formatted, RandomC_Mean_Formatted, RandomD_Mean_Formatted),
Median = c(RandomA_Median_Formatted, RandomB_Median_Formatted, RandomC_Median_Formatted, RandomD_Median_Formatted),
First_Quartile = c(RandomA_FirstQuartile_Formatted, RandomB_FirstQuartile_Formatted, RandomC_FirstQuartile_Formatted, RandomD_FirstQuartile_Formatted),
Third_Quartile = c(RandomA_ThirdQuartile_Formatted, RandomB_ThirdQuartile_Formatted, RandomC_ThirdQuartile_Formatted, RandomD_ThirdQuartile_Formatted)
)
kable(myTable, format = "html", table.attr = 'class="table"') %>%
kable_styling(bootstrap_options = "striped", full_width = FALSE)
|
Group
|
Mean
|
Median
|
First_Quartile
|
Third_Quartile
|
|
RandomA
|
5.163338
|
5.1799846
|
4.1570680
|
6.109085
|
|
RandomB
|
1.662378
|
0.9444801
|
0.3662825
|
1.980843
|
|
RandomC
|
4.930255
|
4.8561822
|
2.5451912
|
7.315131
|
|
RandomD
|
5.020810
|
5.0802557
|
3.0699077
|
7.083904
|
1D Scatter Plot
create_strip_chart <- function(data, column_name) {
ggplot(data, aes(x = 1, y = !!sym(column_name))) +
geom_jitter(position = position_jitter(width = 0.1), color = "black", alpha = 0.7) +
geom_hline(yintercept = mean(data[[column_name]], na.rm = TRUE), color = "black", linetype = "solid", size = 1) +
geom_hline(yintercept = median(data[[column_name]], na.rm = TRUE), color = "green", linetype = "dashed", size = 1) +
geom_hline(yintercept = quantile(data[[column_name]], 0.25, na.rm = TRUE), color = "blue", linetype = "dashed", size = 1) +
geom_hline(yintercept = quantile(data[[column_name]], 0.75, na.rm = TRUE), color = "blue", linetype = "dashed", size = 1) +
theme_minimal() +
labs(title = paste("1-D Scatter Plot -", column_name),
x = NULL, y = "Values") +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) # Remove x-axis labels and ticks
}
Box Plot
create_box_plot <- function(data, column_name) {
ggplot(data, aes(x = "", y = !!sym(column_name))) +
geom_boxplot(color = "black", fill = "white", alpha = 0.7) +
geom_hline(yintercept = mean(data[[column_name]], na.rm = TRUE), color = "black", linetype = "solid", size = 1) +
geom_hline(yintercept = median(data[[column_name]], na.rm = TRUE), color = "green", linetype = "dashed", size = 1) +
geom_hline(yintercept = quantile(data[[column_name]], 0.25, na.rm = TRUE), color = "blue", linetype = "dashed", size = 1) +
geom_hline(yintercept = quantile(data[[column_name]], 0.75, na.rm = TRUE), color = "blue", linetype = "dashed", size = 1) +
theme_minimal() +
labs(title = paste("Box Plot -", column_name),
x = NULL, y = "Values") +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) # Remove x-axis labels and ticks
}
Histogram
create_histogram <- function(data, column_name) {
ggplot(data, aes(x = !!sym(column_name))) +
geom_histogram(color = "black", fill = "white", alpha = 0.7, bins = 30) +
geom_vline(xintercept = mean(data[[column_name]], na.rm = TRUE), color = "black", linetype = "solid", size = 1) +
geom_vline(xintercept = median(data[[column_name]], na.rm = TRUE), color = "green", linetype = "dashed", size = 1) +
geom_vline(xintercept = quantile(data[[column_name]], 0.25, na.rm = TRUE), color = "blue", linetype = "dashed", size = 1) +
geom_vline(xintercept = quantile(data[[column_name]], 0.75, na.rm = TRUE), color = "blue", linetype = "dashed", size = 1) +
theme_minimal() +
labs(title = paste("Histogram -", column_name),
x = "Values", y = "Frequency") +
theme(axis.text.x = element_blank(), axis.ticks.x = element_blank()) # Remove x-axis labels and ticks
}
Create 3 Plots, arrange, and print randomA
# Create the three plots for RandomA
plot_randomA_strip <- create_strip_chart(summary_stats, "random_A")
## 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.
plot_randomA_box <- create_box_plot(summary_stats, "random_A")
plot_randomA_histogram <- create_histogram(summary_stats, "random_A")
# Arrange plots in a grid for RandomA
grid_randomA <- plot_grid(
plot_randomA_strip, plot_randomA_box, plot_randomA_histogram,
nrow = 1, align = 'h', rel_widths = c(1, 1, 1)
)
# Print the grid for RandomA
print(grid_randomA)

Create 3 Plots, arrange, and print randomB
# Create the three plots for RandomB
plot_randomB_strip <- create_strip_chart(summary_stats, "random_B")
plot_randomB_box <- create_box_plot(summary_stats, "random_B")
plot_randomB_histogram <- create_histogram(summary_stats, "random_B")
# Arrange plots in a grid for RandomB
grid_randomB <- plot_grid(
plot_randomB_strip, plot_randomB_box, plot_randomB_histogram,
nrow = 1, align = 'h', rel_widths = c(1, 1, 1)
)
# Print the grid for RandomB
print(grid_randomB)

Create 3 Plots, arrange, and print randomC
# Create the three plots for RandomC
plot_randomC_strip <- create_strip_chart(summary_stats, "random_C")
plot_randomC_box <- create_box_plot(summary_stats, "random_C")
plot_randomC_histogram <- create_histogram(summary_stats, "random_C")
# Arrange plots in a grid for RandomC
grid_randomC <- plot_grid(
plot_randomC_strip, plot_randomC_box, plot_randomC_histogram,
nrow = 1, align = 'h', rel_widths = c(1, 1, 1)
)
# Print the grid for RandomC
print(grid_randomC)

Create 3 Plots, arrange, and print randomD
# Create the three plots for RandomD
plot_randomD_strip <- create_strip_chart(summary_stats, "random_D")
plot_randomD_box <- create_box_plot(summary_stats, "random_D")
plot_randomD_histogram <- create_histogram(summary_stats, "random_D")
# Arrange plots in a grid for RandomD
grid_randomD <- plot_grid(
plot_randomD_strip, plot_randomD_box, plot_randomD_histogram,
nrow = 1, align = 'h', rel_widths = c(1, 1, 1)
)
# Print the grid for RandomD
print(grid_randomD)

The End