Airquality HW

Load in libraries and data

library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.3     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ ggplot2   3.4.3     ✔ tibble    3.2.1
## ✔ lubridate 1.9.2     ✔ tidyr     1.3.0
## ✔ 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
data("airquality")
head(airquality)
##   Ozone Solar.R Wind Temp Month Day
## 1    41     190  7.4   67     5   1
## 2    36     118  8.0   72     5   2
## 3    12     149 12.6   74     5   3
## 4    18     313 11.5   62     5   4
## 5    NA      NA 14.3   56     5   5
## 6    28      NA 14.9   66     5   6

Caluculate Summary Statistics

mean(airquality$Temp)
## [1] 77.88235
median(airquality$Temp)
## [1] 79
sd(airquality$Wind)
## [1] 3.523001
var(airquality$Wind)
## [1] 12.41154

Rename the Months from number to names

airquality$Month[airquality$Month == 5]<- "May"
airquality$Month[airquality$Month == 6]<- "June"
airquality$Month[airquality$Month == 7]<- "July"
airquality$Month[airquality$Month == 8]<- "August"
airquality$Month[airquality$Month == 9]<- "September"

Look at summary statistics of dataset

summary(airquality$Month)
##    Length     Class      Mode 
##       153 character character

Reorder months

airquality$Month<-factor(airquality$Month, levels=c("May", "June","July", "August", "September"))

Plot 1: Create a histogram categorized by Month

plot1 <- ggplot(airquality, aes(x = Temp, fill = Month)) +
  geom_histogram(bins = 30, position = "identity") +
  labs(
    title = "Histogram of Monthly Temperatures from May - September, 1973",
    xlab("Monthly Temperatures from May-Sept"),
    ylab("Frequency of Temps"),
    caption = "New York State Department of Conservation and National Weather Service"
  )
?labs
plot1

Plot 2: Improve the histogram using ggplot

plot2 <- ggplot(airquality, aes(x = Temp, fill = Month)) +
  geom_histogram(bins = 30, position = "identity", binwidth = 5, color = "white", alpha = 0.5) +
  labs(
    title = "Histogram of Monthly Temperatures from May - September, 1973",
    xlab("Monthly Temperatures from May-Sept"),
    ylab("Frequency of Temps"),
    caption = "New York State Department of Conservation and National Weather Service"
  )
plot2

Plot 3: Create Side by Side Boxplots by Month

plot3 <- ggplot(airquality, aes(x = Month, y = Temp, fill = Month)) +
  geom_boxplot() +
  labs(
    title = "Side-by-Side Boxplot of Monthly Temperatures",
    x = "Months from May through September",
    y = "Temperatures",
    caption = "New York State Department of Conservation and National Weather Service"
  )
plot3

Plot 4: Make the same side-by-side boxplots, but in grey-scale

plot4 <- ggplot(airquality, aes(x = Month, y = Temp, fill = Month)) +
  geom_boxplot() +
  scale_fill_grey(name = "Month") +
  labs(
    title = "Side-by-Side Boxplot of Monthly Temperatures",
    x = "Months from May through September",
    y = "Temperatures",
    caption = "New York State Department of Conservation and National Weather Service"
  )
plot4

Plot 5: Now make one new plot on your own, that is meaningfully different from the 4 I have shown you. You can select any of the variables in this dataset. Be sure to explore the dataset to see which variables are included that we have not explored yet. You may create a scatterplot, histogram, boxplot, or something else.

ggplot(airquality, aes(Ozone, Temp, color = Month, alpha = 0.5)) +
  geom_point(na.rm = TRUE) +
  labs(
    title = "Scatterplot Showing the Relationship between Ozone and Wind based on Month",
    x = "Ozone in PPB",
    y = "Temperature in Fahrenheit",
    caption = "New York State Department of Conservation and National Weather Service") +
  facet_wrap(~Month) +
  geom_smooth(se = FALSE, method = "lm", na.rm = TRUE)
## `geom_smooth()` using formula = 'y ~ x'

In this plot, I am showing the correlation between the amount of ozone in PPB and the temperature in Fahrenheit. This plot shows that as the ozone levels increase, so does the temperature. This relationship can be seen in every month. However, in some months, the slope of the linear regression line is steeper, like in September. The slope being steeper signifies a sharper increase in ozone levels and temperatures. May is different though, even thought the ozone levels stay more or less the same, the temperature still continues to increase. I used facet_wrap to separate each graph into months to clearly see how each of the months differ from each other. Also, I used ‘se = FALSE’ to take off the standard error shading that would come with putting a linear regression model. Although the graphs are already separated by Month, assigning a color to each month makes the graph more visually appealing and distinct.