Objective:

In this lab, you will practice creating APA-compliant graphs using ggplot2. Please complete each exercise by filling in the code chunks and interpreting the resulting graphs. Once you have completed the exercises, knit this document to HTML and publish it to RPubs. Make sure your YAML header includes a title, your name, and the date.

# Load ggplot2
library(ggplot2)
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
apa_theme <- theme_classic() +
  theme(
    panel.grid.major = element_blank(),
    panel.grid.minor = element_blank(),
    legend.position = "bottom",
    text = element_text(family = "serif", size = 12),
    axis.title = element_text(size = 12),
    plot.title = element_text(size = 14, hjust = 0.5)
  )

Exercise 1: Creating an APA-Compliant Bar Graph

# Summarize iris data
iris_summary <- iris %>%
  group_by(Species) %>%
  summarize(mean_sepal = mean(Sepal.Length), 
            se_sepal = sd(Sepal.Length) / sqrt(n()))

# Create APA bar plot
ggplot(iris_summary, aes(x = Species, y = mean_sepal)) +
  geom_bar(stat = "identity", fill = "steelblue") +
  geom_errorbar(aes(ymin = mean_sepal - se_sepal, ymax = mean_sepal + se_sepal), width = 0.2) +
  labs(title = "Mean Sepal Length by Iris Species",
       x = "Species",
       y = "Mean Sepal Length (cm)") +
  apa_theme

Interpretation:
Setosa has the shortest average sepal length, while Virginica has the longest. The error bars are relatively small, suggesting that the means are reliable estimates and differences between species are likely meaningful.


Exercise 2: Modifying a ggplot2 Scatter Plot

# Scatter plot with trend line
ggplot(mtcars, aes(x = hp, y = qsec)) +
  geom_point(color = "darkred") +
  geom_smooth(method = "lm", se = FALSE, color = "black") +
  labs(title = "Quarter-Mile Time vs Horsepower",
       x = "Horsepower (hp)",
       y = "Quarter-Mile Time (seconds)") +
  apa_theme
## `geom_smooth()` using formula = 'y ~ x'

Interpretation:
The trend line indicates that cars with higher horsepower tend to have shorter quarter-mile times, meaning they are faster. This inverse relationship suggests that horsepower is a good predictor of drag racing performance.


Exercise 3: Creating an APA-Compliant Line Graph

# Summarize airquality data
monthly_avg_temp <- airquality %>%
  group_by(Month) %>%
  summarize(mean_temp = mean(Temp, na.rm = TRUE))

# Line graph
ggplot(monthly_avg_temp, aes(x = Month, y = mean_temp)) +
  geom_line(color = "blue", size = 1) +
  geom_point(size = 2) +
  labs(title = "Average Monthly Temperature in New York (1973)",
       x = "Month",
       y = "Average Temperature (°F)") +
  scale_x_continuous(breaks = monthly_avg_temp$Month) +
  apa_theme
## 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.

Interpretation:
The average temperature increases from May to July, peaking in July, and then begins to decline through September. This pattern follows typical seasonal trends in New York, indicating a warm summer period.


Submission Instructions:

Ensure to knit your document to HTML format, checking that all content is correctly displayed before submission. Submit the RPubs URL to Canvas Assignments.