Module 15a

Author

Sean Lawler

# Load required libraries
library(ggplot2)
library(ggpubr)
library(ggExtra)
library(ggsci)
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
# Print working directory and files for debug
print(getwd())
[1] "/Users/seanlawler/Downloads/Intro to R/Module 15"
print(list.files())
 [1] "~$dule 15.docx"            "15a.qmd"                  
 [3] "15a.rmarkdown"             "ca_places.zip"            
 [5] "gapminderData5.csv"        "GEOG_5680_15a.html"       
 [7] "GEOG_5680_15b.html"        "GEOG_5680_15c.html"       
 [9] "GEOG_5680_15d.html"        "hsa.csv"                  
[11] "irished.csv"               "LightRail_UTA"            
[13] "LightRail_UTA.zip"         "LightRailStations_UTA"    
[15] "LightRailStations_UTA.zip" "Module 15.docx"           
[17] "Module 15.pdf"             "Module15_abc.qmd"         
[19] "Module15a_files"           "Module15a-b_files"        
[21] "Module15a-b.html"          "Module15a.html"           
[23] "orthodont.csv"             "rs.zip"                   
[25] "rsconnect"                 "slc_tract"                
[27] "slc_tract.zip"             "WNAclimate.csv"           
# Load and filter GapMinder data
gap <- read.csv("gapminderData5.csv")
gap07 <- gap %>% filter(year == 2007 & continent != "Oceania")

# Scatter Plot with regression line and correlation
p1 <- ggscatter(gap07, x = "gdpPercap", y = "lifeExp", col = "continent",
                xlab = "GDP per capita ($)", ylab = "Life Expectancy (yrs)",
                main = "GapMinder Data 2007", add = "reg.line", conf.int = TRUE) +
  xscale("log10", .format = TRUE) +
  stat_cor(aes(color = continent), method = "spearman")
print(p1)

# Histogram of Life Expectancy by Continent
p2 <- gghistogram(gap07, x = "lifeExp", fill = "continent", palette = "npg", bins = 20)
print(p2)

# Violin Plot with Boxplot and Jitter
p3 <- ggviolin(gap07, x = "continent", y = "lifeExp", fill = "continent", palette = "jco",
               add = c("boxplot", "jitter"), ylab = "Life expectancy (yrs)")
print(p3)