library(tidyverse)Week 2 - Assinment
Github link
Airquality Assignment
Load in the library
data("airquality")Look at the structure of the data
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
mean(airquality$Temp)[1] 77.88235
mean(airquality[,4]) #not especific row, but especific colunm: 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
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"))Now look at the summary statistics of the dataset
summary(airquality1$month_name) Length N.unique N.blank Min.nchar Max.nchar
153 5 0 3 9
Month is a categorical variable with different levels, called factors.
airquality1$Month<-factor(airquality1$month_name,
levels=c("May", "June","July", "August",
"September"))Plot 1: Create a histogram categorized by Month
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") #provide the data source
p1`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Plot 2: Improve the histogram of Average Temperature by Month
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")
p2Plot 3: Create side-by-side boxplots categorized by Month
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"))
p3Plot 4: Side by Side Boxplots in Gray Scale
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"))
p4Plot 5: My own plot
median(airquality1$Wind)[1] 9.7
airquality1 <- airquality1 |>
mutate(month_name = factor(month_name,
levels = c("May", "June", "July", "August", "September")))p5 <- airquality1 |>
ggplot(aes(month_name, Wind, fill = month_name)) +
labs(x = "Mounth", y = "Wind Speed (MPH)",
title = "Side-by-Side Boxplot of Monthly Wind Speed",
caption = "New York Air Quality Measurements") +
geom_boxplot()+
scale_fill_discrete(name = "Month", labels = c("May", "June","July", "August", "September"))
p5This box-plot above illustrates the distribution of daily Average wind speed in miles per hour at 0700 and 1000 hours at LaGuardia Airport from May 1, 1973 (a Tuesday) to September 30, 1973. Each box-plot represents the speed range for a especific month, the central line in the box is the median wind speed. If we exam the spread of the box, May have a higher median and larger variability when compared to months like June and July, which shows the lowest median speeds. As the months passes by, the wind speed increases again in August and September.
Additionally, June have two outlier (dots on the graph), one representing an unusual low wind speed and other representing a high wind Speed.
For the code, I used the box-plot generated with ggplot library, then selected two variables: Month_name, created previously by the author of the Assignment, and Wind. The command fill was used to ensure the months were chronological on the output. After that the command labs was used to give the graph label and name the variables. Finally, the command scale_fill was used to give color and legend to the illustration.