# install.packages("tidyverse")
library(tidyverse)Airquality Assignment
Airquality Tutorial and Homework Assignment
Load in the Dataset
Because airquality is a pre-built dataset, we can write it to our data directory to store it for later use.
The source for this dataset is the New York State Department of Conservation and the National Weather Service of 1973 for five months from May to September recorded daily.
Load the dataset into your global environment
data("airquality")Look at the structure of the data
The function, head, will only display the first 6 rows of the dataset. Notice in the global environment to the right, there are 153 observations (rows).
View the data using the “head” function
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
Calculate Summary Statistics
If you want to look at specific statistics, here are some variations on coding. Here are 2 different ways to calculate “mean.”
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$Wind)[1] 12.41154
Rename the Months from number to names
Number 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"Now look at the summary statistics of the dataset
See how Month has changed to have characters instead of numbers
summary(airquality$Month) Length Class Mode
153 character character
Month is a categorical variable with different levels, called factors.
This is one way to reorder the Months so they do not default to alphabetical (you will see another way to reorder DIRECTLY in the chunk that creates the plot below in Plot 1)
airquality$Month<-factor(airquality$Month, levels=c("May", "June","July", "August", "September"))Plot 1: Create a histogram categorized by Month
Here is a first attempt at viewing a histogram of temperature by the months May through September. We will see that temperatures increase over these months. The median temperature appears to be about 75 degrees.
Reorder the legend so that it is not the default (alphabetical), but rather in chronological order.
fill = Month colors the histogram by months between May - Sept.
scale_fill_discrete(name = “Month”…) provides the month names on the right side as a legend.
p1 <- airquality |>
ggplot(aes(x=Temp, fill=Month)) +
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") #provide the data source
p1`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Plot 2: Improve the histogram using ggplot
Outline the bars in white using the color = “white” command
Use alpha to add some transparency (values between 0 and 1)
Change the binwidth
Histogram of Average Temperature by Month
Add some transparency and white borders around the histogram bars. Here July stands out for having high frequency of 85 degree temperatures. The dark purple color indicates overlaps of months due to the transparency.
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")) +
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")
p2Plot 3: Create side-by-side boxplots categorized by Month
We can see that August has the highest temperatures based on the boxplot distribution.
p3 <- airquality |>
ggplot(aes(Month, Temp, fill = Month)) +
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 Notice that the points above and below the boxplots in June and July are outliers.
Plot 4: Make the same side-by-side boxplots, but in grey-scale
Use the scale_fill_grey command for the grey-scale legend, and again, use fill=Month in the aesthetics
Side by Side Boxplots in Gray Scale
Here we just changed the color palette to gray scale using scale_fill_grey
p4 <- airquality |>
ggplot(aes(Month, Temp, fill = Month)) +
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"))
p4Self-Made Plots
Now make one plot on your own of any of the variables in this dataset. It may be a scatterplot, histogram, or boxplot.
Plot 5 - Create a pairwise scatterplot matrix.
I started out with a pairwise scatterplot to compare the relationships between the variables. By doing a scatter plot matrix you can compare each variable against each other.
# Reload dataset
data("airquality")
# Create a pairwise scatterplot matrix
pairs(airquality[c("Ozone", "Solar.R", "Wind", "Temp")])Plot 6 - Plot the strongest relationship
After an eyecheck, using the variables in the provided dataset, it looks like Ozone and Temperature have the strongest relationship. The scatter plot was mapped out with Ozone on the (y) axis and Temperature on the (x) axis. A linear polynomial trendline was added to look at the relationship. As Ozone increases, do does temperature.
# Reload dataset
data("airquality")
p6 <- airquality |>
ggplot(aes(Temp, Ozone)) +
labs(x = "Temperature", y = "Ozone",
title = "Scatter Plot of Ozone vs. Temperature with Trendline",
caption = "New York State Department of Conservation and the National Weather Service") +
geom_point() +
geom_smooth(method = "lm", formula = y ~ poly(x, 2), se = FALSE, color = "blue") + # Add a linear trendline
theme_minimal()
p6Warning: Removed 37 rows containing non-finite values (`stat_smooth()`).
Warning: Removed 37 rows containing missing values (`geom_point()`).
Plot 7 - Plot temperature over time with rolling average
This scatter plot was created to look at temperature over time with a 7-day moving average. With the moving average on the graph, it’s easy to spot outliers and look at the distribution. Boxplots are good for this, but sometimes it seems to hide the story because it’s difficult to see the real picture. The graph shows the seasonal nature of the weather and the variable temperature over time.
# Reload dataset
data("airquality")
# Load libraries
library(tidyverse)
library(zoo)
Attaching package: 'zoo'
The following objects are masked from 'package:base':
as.Date, as.Date.numeric
# Create a pseudo-date column by combining "Month" and "Day"
airquality <- transform(airquality, Date = as.Date(paste(1973, Month, Day, sep = "-")))
# Calculate the 7-day rolling average of Temperature using zoo::rollmean
airquality <- airquality %>%
arrange(Date) %>%
mutate(RollingAvgTemp = zoo::rollmean(Temp, k = 7, fill = NA, align = "right"))
# Create a scatter plot of Temperature vs. Date with a 7-day moving average trendline
ggplot(data = airquality, aes(Date, Temp)) +
geom_point() +
labs(x = "Month", y = "Temperature",
title = "Scatter Plot of Temperature vs. Date",
caption = "New York State Department of Conservation and the National Weather Service") +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) + # Rotate x-axis labels
geom_smooth(aes(Date, RollingAvgTemp), method = "loess", se = FALSE, color = "blue")`geom_smooth()` using formula = 'y ~ x'
Warning: Removed 6 rows containing non-finite values (`stat_smooth()`).