#Airquality Tutorial and Homework Assignment
Because airquality is a pre-built dataset, we can write it to our data directory to store it for later use.
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.2 ──
## ✔ ggplot2 3.3.6 ✔ purrr 0.3.4
## ✔ tibble 3.1.8 ✔ dplyr 1.0.10
## ✔ tidyr 1.2.0 ✔ stringr 1.4.1
## ✔ readr 2.1.2 ✔ forcats 0.5.2
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag() masks stats::lag()
airquality <- airquality
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 ...
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
median(airquality$Temp)
## [1] 79
sd(airquality$Wind)
## [1] 3.523001
var(airquality$Wind)
## [1] 12.41154
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"
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
##
Reorder the Months so they do not default to alphabetical
airquality$Month<-factor(airquality$Month, levels=c("May", "June","July", "August", "September"))
Qplot stands for “Quick-Plot” (in the ggplot2 package)
p1 <- qplot(data = airquality,Temp,fill = Month,geom = "histogram", bins = 20)
p1
ggplot is more sophisticated than qplot, but still uses ggplot2 package
Reorder the legend so that it is not the default (alphabetical), but rather in order that months come
Outline the bars in white using the color = “white” command
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
fill=Month command fills each boxplot with a different color in the aesthetics
scale_fill_discrete makes the legend on the side for discrete color values
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
Use the scale_fill_grey command for the grey-scale legend, and again, use fill=Month in the aesthetics
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
p5 <- airquality %>%
ggplot(aes(Month, Wind, fill = Month)) +
ggtitle("Wind Speed According to Months") +
xlab("Months") +
ylab("Wind Speed") +
geom_boxplot() +
scale_fill_ordinal(name = "Month", labels = c("May", "June","July", "August", "September"))
p5
I created a boxplot to show the data of wind speed and how it changes over the months. Honestly, I struggled a bit trying to figure out what data I could use together in order to make an accurate graph, and code it correctly. Initially I had intended to make a boxplot of ozone levels according to temperature, however I wasn’t sure how correct I was, and decided to go with wind speed instead. Also, you can notice that the only two outliers in this data are in the month of June, otherwise May could be noted as the month with the highest wind speed. I used the code from plot 4 as my guide to make this boxplot, but something I did different was change the color scale. I think its really cool that when you type “scale_fill” in r, it automatically gives you a variety of suggestions you can choose from to include in your code with a brief description of what kind of data each fill is used for.