task 1: scatter plot and regression line

#Load data 
data("USArrests")
#view the data set 
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
#load proper packages
library(ggplot2)
#create a scatter plot with the chosen x and y variables
ggplot(USArrests, aes(y = Assault, x =Murder)) + geom_point() +
  #set theme 
  theme_classic() + 
  #add labels 
  labs(title = "Scatter Plot of Assault vs. Murder Rates", x = "Murder Rate", y = 
         "Assault Rate") + 
  #add a linear model regression line 
    geom_smooth(method = lm, color = "black", fill = "red") + 
  #changing aesthetics 
  geom_point(size = 3.5, shape = 21, fill = "lightblue") + 
  #change the font sizes a little 
  theme(plot.title = element_text(size=16, face = "bold"), axis.title.x = element_text(size = 14), axis.title.y = element_text(size = 14))

#create a new column for the states
USArrests$State <- rownames(USArrests)
#creating a column for the means
USArrests$AverageCrimeRate <- rowMeans(USArrests[, c("Murder", "Assault", "Rape")])

# Create the line plot
ggplot(data = USArrests, aes(x = State, y = AverageCrimeRate, group = 1)) +
  geom_line(color = "steelblue", size = 1) +  # Line
  geom_point(color = "red", size = 3) +   # Points
  labs(title = "Line Plot of Average Crime Rate by State",
       x = "State",
       y = "Average Crime Rate") +
  theme_classic() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1))  # Rotate x-axis labels

#notes: couldn't figure out how to add the states to the x axis the first time