In this lab, I will be using the ggplot() function to create various graphs and to show ggplot2’s capabilities.
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.
We will now create a histogram of salinity values for each year of the study, and then each month, using the facet_wrap() function.
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.
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.
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.
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
temp$decdate = temp$Year + temp$dDay3 / 365
Creates a variable representing the decimal date, by year and day.
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.
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.
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.
myplot6 = ggplot(temp, aes(x=decdate, y = Salinity, color = Area)) + geom_line() + facet_wrap(~Area)
myplot6
Lineplots of salinity vs time for each 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.
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()`).