## Air Quality Tutorial and Homework Assignment

# install.packages("tidyverse")
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.4.1     ✔ purrr   1.0.1
## ✔ tibble  3.1.8     ✔ dplyr   1.1.0
## ✔ tidyr   1.3.0     ✔ stringr 1.5.0
## ✔ readr   2.1.4     ✔ forcats 1.0.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()

Load the dataset into your global environment

airquality <- airquality

Look at the structure of the data

str(airquality)
## 'data.frame':    153 obs. of  6 variables:
##  $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
##  $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
##  $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
##  $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
##  $ Month  : int  5 5 5 5 5 5 5 5 5 5 ...
##  $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...

Calculating Summary Statistics

mean(airquality$Temp)
## [1] 77.88235
mean(airquality[,4])
## [1] 77.88235

##Calculate Median, Standard Deviation, and Variance

median(airquality$Temp)
## [1] 79
sd(airquality$Wind)
## [1] 3.523001
var(airquality)
##         Ozone Solar.R        Wind       Temp      Month         Day
## Ozone      NA      NA          NA         NA         NA          NA
## Solar.R    NA      NA          NA         NA         NA          NA
## Wind       NA      NA  12.4115385 -15.272136 -0.8897532   0.8488519
## Temp       NA      NA -15.2721362  89.591331  5.6439628 -10.9574303
## Month      NA      NA  -0.8897532   5.643963  2.0065359  -0.0999742
## Day        NA      NA   0.8488519 -10.957430 -0.0999742  78.5797214

Change the Months from 5-9 to May through September

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 the summary statistics of the dataset, and see how Month has changed to have characteristics instead of numbers

str(airquality)
## 'data.frame':    153 obs. of  6 variables:
##  $ Ozone  : int  41 36 12 18 NA 28 23 19 8 NA ...
##  $ Solar.R: int  190 118 149 313 NA NA 299 99 19 194 ...
##  $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...
##  $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ...
##  $ Month  : chr  "May" "May" "May" "May" ...
##  $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...
summary(airquality)
##      Ozone           Solar.R           Wind             Temp      
##  Min.   :  1.00   Min.   :  7.0   Min.   : 1.700   Min.   :56.00  
##  1st Qu.: 18.00   1st Qu.:115.8   1st Qu.: 7.400   1st Qu.:72.00  
##  Median : 31.50   Median :205.0   Median : 9.700   Median :79.00  
##  Mean   : 42.13   Mean   :185.9   Mean   : 9.958   Mean   :77.88  
##  3rd Qu.: 63.25   3rd Qu.:258.8   3rd Qu.:11.500   3rd Qu.:85.00  
##  Max.   :168.00   Max.   :334.0   Max.   :20.700   Max.   :97.00  
##  NA's   :37       NA's   :7                                       
##     Month                Day      
##  Length:153         Min.   : 1.0  
##  Class :character   1st Qu.: 8.0  
##  Mode  :character   Median :16.0  
##                     Mean   :15.8  
##                     3rd Qu.:23.0  
##                     Max.   :31.0  
## 

Month is a categorical variable with different levels, called factors.

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

Plot 1: Create a histogram categorized by Month with qplot

p1 <- qplot(data = airquality,Temp,fill = Month,geom = "histogram", bins = 20)
## Warning: `qplot()` was deprecated in ggplot2 3.4.0.
p1

## Plot2: Histogram of Average Temperature by Month

p2 <- airquality %>%
  ggplot(aes(x=Temp, fill=Month)) +
  geom_histogram(position="identity", alpha=0.5, binwidth = 5, color = "white")+
  scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
p2

# Plot 3: Side by Side Boxplots of Average Temeperature by Month

p3 <- airquality %>%
  ggplot(aes(Month, Temp, fill = Month)) + 
  ggtitle("Temperatures") +
  xlab("Monthly Temperatures") +
  ylab("Frequency") +
  geom_boxplot() +
  scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
p3 

## Plot 4: Make the same side by side boxplots in Gray Scale

p4 <- airquality %>%
  ggplot(aes(Month, Temp, fill = Month)) + 
  ggtitle("Monthly Temperature Variations") +
  xlab("Monthly Temperatures") +
  ylab("Frequency") +
  geom_boxplot()+
  scale_fill_grey(name = "Month", labels = c("May", "June","July", "August", "September"))
p4

## Plot 5: Making my own plot of any of the variables in this dataset

p5 <- airquality %>% ggplot(aes(Month, Wind, fill = Month)) + ggtitle("Wind Readings") + xlab("Wind Readings") + ylab("Frequency") + geom_boxplot() + scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September")) 
p5

## Essay regarding plot number 5 This is my plot of wind readings from the months of May to September using data from the “airquality” dataset. I believe that this plot is the most effective way of demonstrating differences in wind reading between months. You are able to see the maximum, minumum, and median readings (although I do understand that “frequency” may be redundant here). The code I used is very similar to that of plot 3. Embedded in the code is directions to pick up values from “Wind” and provide boxplots. The code also labels the x and y axis appropriately, as well as provides a title for the visualization.