###################################################################### # R commands Stat 515 # Lab 1 University of South Carolina ###################################################################### # This file contains the R commands for the lab. # # Lines beginning with the symbol '#' are comments in R. All other # lines contain code. # # In R for Windows, you may wish to open this file from the menu bar # (File:Display file); you can then copy commands into the command # window. (Use the mouse to highlight one or more lines; then # right-click and select "Paste to console".) ###################################################################### #### run install.packagtes if you have not done it before #install.packages(c("ggplot2", "patchwork")) library(NADA) library(GGally) library(ggplot2) library(patchwork) data(HgFish) head(HgFish) #setwd("/Users/hoyen/Desktop/STAT515-2026/Notes") ############## Scatter plot matrix############## pm <- ggpairs(HgFish, columns = c(3, 4, 5)) #png("FishMatrix.png") pm #dev.off() ############################################ mean(HgFish$Weight) mode(HgFish$Weight) median(HgFish$Weight) range(HgFish$Weight) var(HgFish$Weight) sd(HgFish$Weight) sqrt(var(HgFish$Weight)) hist(HgFish$Weight, main="Fish Weight", xlab="Kg") boxplot(HgFish$Weight, main="Fish Weight (Kg)") ############################ # boxplot + histogram ############################# library(ggplot2) library(patchwork) # 1. Build the dataframe with a proper column name df <- data.frame(Weight = HgFish$Weight) # 2. Build the top Boxplot p1 <- ggplot(df, aes(x = Weight)) + geom_boxplot(fill = "lightblue", color = "darkblue") + theme_void() # 3. Build the bottom Histogram p2 <- ggplot(df, aes(x = Weight)) + geom_histogram(fill = "lavender", color = "black", bins = 30) + labs(x = "Value Range", y = "Frequency") + theme_minimal() # 4. Combine them using patchwork (stack vertically, fix sizing ratio) png("box_hist.png") p1 / p2 + plot_layout(heights = c(1, 4)) dev.off()