Part 1

In this lab, I will be using the ggplot() function to create various graphs and to show ggplot2’s capabilities.

Histogram of Salinity Values

First, we need to read in the data we will use. For this lab, we will be using the Temperature dataset.

temp = read.csv("Temperature.csv")

Now, we will create our plot.

library(ggplot2)
myplot = ggplot(temp, aes(x = Salinity)) 
myplot + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 798 rows containing non-finite values (`stat_bin()`).

Basic histogram of Salinity Values for the Temperature.csv dataset.

Histogram of Salinity Values for each Year and Month

We will now create a histogram of salinity values for each year of the study, and then each month, using the facet_wrap() function.

Each Year

myplot + geom_histogram() + facet_wrap(~Year)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 798 rows containing non-finite values (`stat_bin()`).

Histogram of salinity for every year in the dataset.

Each Month

myplot + geom_histogram() + facet_wrap(~Month)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 798 rows containing non-finite values (`stat_bin()`).

Histogram of salinity for every month in the dataset.

Boxplot of Temperature Values for Each Station

Next, we will create a boxplot of temperature values for each individual station.

myplot2 = ggplot(temp, aes(x=Station, y= Temperature))
myplot2 = myplot2 + geom_boxplot()
myplot2
## Warning: Removed 927 rows containing non-finite values (`stat_boxplot()`).

Boxplot of temperature values for each station.

Saving the Figure as a PNG

ggsave("Temperature_boxplot.png", myplot2)
## Saving 7 x 5 in image
## Warning: Removed 927 rows containing non-finite values (`stat_boxplot()`).

Saves figure as PNG to working directory

Part 2 of Lab 7

Creating Time Series Graphs

temp$decdate = temp$Year + temp$dDay3 / 365

Creates a variable representing the decimal date, by year and day.

Scatterplot of Temperature and Salinity Over Time

Temperature

myplot3 = ggplot(temp, aes(x=decdate, y=Temperature)) + geom_point(color = "lightblue")
myplot3
## Warning: Removed 927 rows containing missing values (`geom_point()`).

Scatterplot of Temperature VS Time.

Salinity

myplot4 = ggplot(temp, aes(x=decdate, y=Salinity)) + geom_point(color = "green")
myplot4
## Warning: Removed 798 rows containing missing values (`geom_point()`).

Scatterplot of Salinity VS Time.

Scatterplots of Salinity, Separated by Area

myplot5 = ggplot(temp, aes(x=decdate, y = Salinity)) + geom_point(color="red") + facet_wrap(~Area)
myplot5
## Warning: Removed 798 rows containing missing values (`geom_point()`).

Scatterplots of salinity vs time for each area.

Lineplot of Salinity Values for Each Station

myplot6 = ggplot(temp, aes(x=decdate, y = Salinity, color = Area)) + geom_line() + facet_wrap(~Area)
myplot6

Lineplots of salinity vs time for each area.

Lineplot of Salinity Values for only OS Area

myplot7 = ggplot(subset(temp, Area == "OS"),
                 aes(x=decdate, y = Salinity)) + geom_line(color = "pink")
myplot7

Lineplot of salinity values for only OS Area.

End of Lab Note:

For the bonus of Part 1, I could not get the code to actually reorder the boxplots by median temperature for each station, but I will submit my best effort as a code just incase here:

myplot8 = ggplot(temp, aes(x=Station, y=Temperature)) + geom_boxplot(aes(reorder(Station, Temperature, median)))
myplot8
## Warning: Removed 927 rows containing non-finite values (`stat_boxplot()`).

Thanks!