title: "Air Quality" author: "Hana Rose" format: html editor: visual ---
Load library tidyverse in order to access dplyr and ggplot2
{r} library(tidyverse)
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.
Because airquality is a pre-built dataset, we can write it to our data directory to store it for later use.
{r} data("airquality")
In the global environment, click on the row with the airquality dataset and it will take you to a “spreadsheet” view of the data.
The function, head, will only disply the first 6 rows of the dataset. Notice in the global environment to the right, there are 153 observations (rows)
{r} head(airquality)
Notice that all the variables are classified as either integers or continuous values.
If you want to look at specific statistics, here are some variations on coding. Here are 2 different ways to calculate “mean.”
{r} mean(airquality$Temp)
{r} mean(airquality[,4])
For the second way to calculate the mean, the matrix [row,column] is looking for column #4, which is the Temp column and we use all rows.
{r} median(airquality$Temp)
{r} sd(airquality$Wind)
{r} var(airquality$Wind)
Sometimes we prefer the months to be numerical, but here, we need them as the month names. There are MANY ways to do this. Here is one way to convert numbers 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"
See how Month has changed to have characters instead of numbers (it is now classified as “character” rather than “integer”)
{r} summary(airquality$Month)
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"))
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.
fill = Month colors the histogram by months between May - Sept.
scalefilldiscrete(name = “Month”…) provides the month names on the right side as a legend in chronological order. This is a different way to order than what was shown above.
labs allows us to add a title, axes labels, and a caption for the data source.
{r} 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
Is this plot useful in answering questions about monthly temperature values?
Outline the bars in white using the color = “white” command
Use alpha to add some transparency (values between 0 and 1)
Change the binwidth
Add some transparency and white borders around the histogram bars.
{r} 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") p2
Here July stands out for having high frequency of 85 degree temperatures. The dark purple color indicates overlaps of months due to the transparency.
Did this improve the readability of the plot?
We can see that August has the highest temperatures based on the boxplot distribution.
{r} 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.
Make the same side-by-side boxplots, but in grey-scale.
Use the scalefillgrey command for the grey-scale legend, and again, use fill=Month in the aesthetics.
{r} 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")) p4
Here we just changed the color palette to gray scale using scalefillgrey.
{r} p5 <- airquality %>% ggplot(mapping = aes(x = Solar.R, y = Ozone, color = Solar.R)) + geom_point(na.rm = TRUE) + scale_color_gradient(low = "#ADDAFF", high = "#0064B7", name = "Solar Radiation") + labs( x = "Solar Radiation", y = "Ozone Concentration", title = "Scatterplot of Ozone Concentration by Solar Radiation", caption = "New York State Conservation and the National Weather Service") p5
```{r} solarradiation <- airquality$Solar.R
ozoneconcentration <- airquality$Ozone
correlation <- cor(solarradiation, ozoneconcentration,
use = "complete.obs") print(correlation) {r}
correlationtest <- cor.test(airquality$Solar.R, airquality$Ozone)
correlationpvalue <- correlationtest$p.value
print(paste("Correlation P-value:", correlationpvalue)) ```
Describe the plot type you have created
Any insights that the plot shows
Describe any special code you used to make this plot
This scatterplot depicts the relationship between solar radiation and ozone concentration using the airquality dataset. It shows a weak positive correlation of r = 0.3 and an extremely statistically significant P-value of p = 0.0001, indicating ozone concentration increases with solar radiation and has a meaningful relationship. This confirmed my hypothesis that ozone concentration increases with solar radiation, as well as it being due to a direct cause-and-effect relationship rather than being a spurious correlation. This is because the UV radiation in sunlight reacts with nitrogen oxides and volatile organic compounds to create ozone, leading to higher concentrations of tropospheric ozone under more sunlight. Here is an explanation of my codes:
Line 1: p5 <- airquality
Explanation: p5 acts as "storage" for the code, while the arrow (<-) puts the code inside it. The piping (%>%) passes the data from the airquality data set (airquality) onto the next function.
Line 2: ggplot(mapping = aes(x = Solar.R, y = Ozone, color = Solar.R)) +
Explanation: ggplot creates the plot, and mapping = aes() sets up the plot's aesthetics. Sx = Solar.R and y = Ozone establish the scatterplot's x and y variables. Color = Solar.R corresponds to the color of the plot as proportional to the degree of solar radiation.
Line 3: geom_point(na.rm = TRUE) +
Explanation: geom_plot adds points to the "frame" of the scatterplot to represent measured values from the dataset, and (na.rm =TRUE) filters out any null values in the dataset before creating a plot.
Line 4: scalecolorgradient(low = "#ADDAFF", high = "#0064B7", name = "Solar Radiation") +
Explanation: scalecolorgradient adds a color gradient to the plot, low = "#ADDAFF" and high = "#0064B7" correspond measurements with solar radiation, with lower values being lighter and higher values being darker, and name = "Solar Radiation" creates a label for the color scale.
Line 5: labs( x = "Solar Radiation", y = "Ozone Concentration", title = "Scatterplot of Ozone Concentration by Solar Radiation", caption = "New York State Conservation and the National Weather Service")
Explanation: labs() adds labels to the chart. x = "Solar Radiation" is the label for the x axis, y = "Ozone Concentration" is the label for the y axis, title = "Scatterplot of Ozone Concentration by Solar Radiation" is the label for the title, and caption = "New York State Conservation and the National Weather Service") is the label for the caption.
Line 6: p5
Explanation: p5 displays the plot.
Line 1: solar_radiation <- airquality$Solar.R
Explanation: The solar radiation column (Solar.R) of the airquality dataset (airquality) is transferred to a "storage" called solarradiation (solarradiation) by the arrow (<-).
Line 2: ozone_concentration <- airquality$Ozone
Explanation: The ozone column (Ozone) of the airquality dataset (airquality) is transferred to a new "box" called solarradiation (ozoneconcentration) by the arrow (<-).
Line 3: correlation <- cor(solarradiation, ozoneconcentration, use = "complete.obs")
Explanation: The correlation coefficient between solarradiation (solarradiation) and ozone concentration (ozone_concentration) is calculated by the function cor(). "Complete.obs" ensures only data points where both solar radiation and ozone concentrations are present are included in the calculation and then put into the "storage" called correlation (correlation) by an arrow (<-).
Line 4: print(correlation)
Explanation: print(correlation) displays the correlation coefficient.
Line 1: correlation_test <- cor.test(airquality$Solar.R, airquality$Ozone)
Explanation: The ozone (Ozone) and solar radiation (Solar.R) columns of the dataset (airquality) are extracted through the dollar sign ($) to calculate a correlation through cor.test and are then put into a "storage" called correlationtest (correlationtest) by an arrow (<-).
Line 2: correlationpvalue <- correlation_test$p.value
Explanation: The p-value (p.value) is extracted from the correlation test (correlationtest) and put into the "storage" called correlationpvalue (correlationp_value) by an arrow (<-).
Line 3: print(paste("Correlation P-value:", correlationpvalue))
Explanation: paste() stores the value of the p-value (correlationpvalue) into the text ("Correlation P-Value"), which is then displayed by print().