AirQuality Assignment

{

# Loading the data and tidyverse.
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.2.1     ✔ readr     2.2.0
✔ forcats   1.0.1     ✔ stringr   1.6.0
✔ ggplot2   4.0.3     ✔ tibble    3.3.1
✔ lubridate 1.9.5     ✔ tidyr     1.3.2
✔ purrr     1.2.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")

# Recoding month variable
airquality1 <- airquality |>
  mutate(month_name = case_when(Month == 4 ~ "April",
                                Month == 5 ~ "May",
                                Month == 6 ~ "June",
                                Month == 7 ~ "July",
                                Month == 8 ~ "August",
                                Month == 9 ~ "September"))
airquality1$Month<-factor(airquality1$month_name, 
                          levels=c("May", "June","July", "August",
                                   "September"))

Plot 1

p1 <- airquality1 |>
  ggplot(aes(x=Temp, fill=month_name)) +
  geom_histogram(position="identity")+
  scale_fill_discrete(name = "Month", 
                      labels = c("May", "June","July", "August", "September")) +
  labs(x = "Monthly Temperatures from May - Sept", 
       y = "Frequency of Temps",
       title = "Histogram of Monthly Temperatures from May - Sept, 1973",
       caption = "New York State Department of Conservation and the National Weather Service")
p1
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.

This plot shows a histogram of the frequency of different daily temperature values for months May - September in 1973. The histogram is colored by month, showing how many of each temperature value came from each month.

Plot 2

p2 <- airquality1 |>
  ggplot(aes(x=Temp, fill=month_name)) +
  geom_histogram(position="identity", alpha=0.5, binwidth = 5, color = "white")+
  scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September")) +
  labs(x = "Monthly Temperatures from May - Sept", 
       y = "Frequency of Temps",
       title = "Histogram of Monthly Temperatures from May - Sept, 1973",
       caption = "New York State Department of Conservation and the National Weather Service")
p2

The data in this plot is the same as plot 1, but the bars are wider, are outlined in white, and are slightly transparent.

Plot 3

p3 <- airquality1 |>
  ggplot(aes(Month, Temp, fill =  month_name)) + 
  labs(x = "Months from May through September", y = "Temperatures", 
       title = "Side-by-Side Boxplot of Monthly Temperatures",
       caption = "New York State Department of Conservation and the National Weather Service") +
  geom_boxplot() +
  scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
p3

This plot shows a box plot of daily temperatures recorded for each month in May - September in 1973.

Plot 4

p4 <- airquality1 |>
  ggplot(aes(Month, Temp, fill = month_name)) + 
  labs(x = "Monthly Temperatures", y = "Temperatures", 
       title = "Side-by-Side Boxplot of Monthly Temperatures",
       caption = "New York State Department of Conservation and the National Weather Service") +
  geom_boxplot()+
  scale_fill_grey(name = "Month", labels = c("May", "June","July", "August", "September"))
p4

This plot contains the same data as plot 3, but it is in grayscale instead of color.

Plot 5

p5 <- airquality1 |>
  ggplot(aes(x=Solar.R, y=Ozone, color=Month)) +
  geom_point() +
  labs(x = "Solar Radiation (lang)", 
       y = "Ozone",
       title = "Scatter Plot of Solar Radiation vs Ozone in New York State from May - Sept, 1973",
       caption = "New York State Department of Conservation and the National Weather Service")
p5
Warning: Removed 42 rows containing missing values or values outside the scale range
(`geom_point()`).

I have created a scatter plot with the variable solar radiation on the x axis and ozone on the y axis. Furthermore, the dots are colored to correspond to the month the data point come from. The graph indicates there might be a positive linear relationship between solar radiation and ozone. Furthermore, the measurements with the highest solar radiation mostly occurred in in May and July. The measurements with the highest ozone mostly occurred in July and August.

To make this, I used the geom_point function instead of using the geom_bar or geom_histogram or geom_boxplot function. I used color=“month” in the aes argument to color the dots by month (I first tried fill= and that didn’t work, so then I tried color=). Otherwise, I defined x and y in aes as normal and defined the labels as normal.