R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

# Install and load required packages
if (!require("rmarkdown")) {
  install.packages("rmarkdown", dependencies = TRUE)
  library(rmarkdown)
}
## Loading required package: rmarkdown
## Warning: package 'rmarkdown' was built under R version 4.3.3
if (!require("knitr")) {
  install.packages("knitr")
  library(knitr)
}
## Loading required package: knitr
## Warning: package 'knitr' was built under R version 4.3.3
if (!require("tinytex")) {
  install.packages("tinytex")
  library(tinytex)
  tinytex::install_tinytex()  # Install TinyTeX distribution
}
## Loading required package: tinytex
## Warning: package 'tinytex' was built under R version 4.3.3
# List of additional required packages
packages <- c("ggplot2", "readr", "tidyverse", "dplyr", "ggpubr")

# Function to check and install missing packages
check_install_packages <- function(pkg) {
  if (!require(pkg, character.only = TRUE)) {
    install.packages(pkg, dependencies = TRUE)
    library(pkg, character.only = TRUE)
  }
}

# Apply the package installation function
sapply(packages, check_install_packages)
## Loading required package: ggplot2
## Loading required package: readr
## Loading required package: tidyverse
## Warning: package 'lubridate' was built under R version 4.3.3
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.4     ✔ stringr   1.5.1
## ✔ forcats   1.0.0     ✔ tibble    3.2.1
## ✔ lubridate 1.9.4     ✔ 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
## Loading required package: ggpubr
## $ggplot2
## NULL
## 
## $readr
## NULL
## 
## $tidyverse
## NULL
## 
## $dplyr
## NULL
## 
## $ggpubr
## NULL
# Load the USArrests dataset
data("USArrests")
head(USArrests)
##            Murder Assault UrbanPop Rape
## Alabama      13.2     236       58 21.2
## Alaska       10.0     263       48 44.5
## Arizona       8.1     294       80 31.0
## Arkansas      8.8     190       50 19.5
## California    9.0     276       91 40.6
## Colorado      7.9     204       78 38.7
# Create the first ggplot
ggplot(mtcars, aes(x = mpg, y = hp)) +
  geom_point()

# Create the customized ggplot
ggplot(mtcars, aes(x = hp, y = mpg, color = as.factor(cyl))) +
  geom_point(size = 2.4, shape = 8) +
  theme_minimal() +
  theme(legend.position = "bottom") +
  labs(
    title = "Effect of Horsepower on Fuel Efficiency",
    subtitle = "Categorized by Number of Cylinders",
    x = "Horsepower",
    y = "Fuel Efficiency (MPG)",
    color = "cyl"
  ) +
  scale_color_brewer(palette = "Blues")