This exercise uses the Temperature.csv, which contains sea temperature and salinity recorded from 31 stations over 15 years. The stations are located in one of 10 areas.

temp <- read.csv("Temperature.csv")
head(temp)
##          Sample     Date    DateNr dDay1 dDay2 dDay3 Station Area X31UE_ED50
## 1 DANT.19900110 19900110   10/1/90     7     9     9    DANT   WZ   681379.6
## 2 DANT.19900206 19900206    6/2/90    34    36    36    DANT   WZ   681379.6
## 3 DANT.19900308 19900308    8/3/90    64    66    66    DANT   WZ   681379.6
## 4 DANT.19900404 19900404    4/4/90    91    93    93    DANT   WZ   681379.6
## 5 DANT.19900509 19900509    9/5/90   126   128   128    DANT   WZ   681379.6
## 6 DANT.19900620 19900620 6/20/1990   168   170   170    DANT   WZ   681379.6
##   X31UN_ED50 Year Month Season Salinity Temperature CHLFa
## 1    5920571 1990     1 winter    29.19         4.0   1.3
## 2    5920571 1990     2 winter    27.37         6.0    NA
## 3    5920571 1990     3 spring    24.99         7.3  21.1
## 4    5920571 1990     4 spring    28.79         8.2  25.0
## 5    5920571 1990     5 spring    33.28        17.4  10.2
## 6    5920571 1990     6 summer    32.69        18.1   6.2
library(ggplot2)

Part 1

Step one: Use ggplot() to produce a histogram of salinity values

temp.plot.hist = ggplot(temp, aes(x = Salinity))
temp.plot.hist + geom_histogram(binwidth=1)
## Warning: Removed 798 rows containing non-finite values (`stat_bin()`).

Step two: Make a histogram of salinity values for each year of study, and then for each month

  • By year
temp.plot.hist + geom_histogram(color='darkred', fill='white') + facet_wrap(~ Year)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 798 rows containing non-finite values (`stat_bin()`).

  • By month
temp.plot.hist + geom_histogram(color='darkred', fill='white') + facet_wrap(~ Month)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
## Warning: Removed 798 rows containing non-finite values (`stat_bin()`).

Step three: Make a boxplot of temperature values for each station

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

Step four: Save the last figure to a png file

temp.plot.boxplot = temp.plot.boxplot + geom_boxplot()
ggsave("Temperature by Station Boxplot.png", temp.plot.boxplot)
## Saving 7 x 5 in image
## Warning: Removed 927 rows containing non-finite values (`stat_boxplot()`).

Part 2

Now we make some time series plots of temperature and salinity time series. We will need a variable to represent continuous time from the start of the observations and can do this by creating a decimal date as the sum of the year and the day number / 365 (if temp is the dataframe holding the data):

temp$decdate <- temp$Year + temp$dDay3 / 365

Step one: Use this variable make a scatterplot of temperature and salinity over time

temp.plot.scatterplot = ggplot(temp, aes(x = temp$decdate, y = Salinity)) 
temp.plot.scatterplot + geom_point()
## Warning: Use of `temp$decdate` is discouraged.
## ℹ Use `decdate` instead.
## Warning: Removed 798 rows containing missing values (`geom_point()`).

Step two: Make a scatterplot of salinity, grouped using facet_wrap() into different ‘Areas’

temp.plot.scatterplot = temp.plot.scatterplot + geom_point()

temp.plot.scatterplot + geom_point(color='darkblue', fill='white') + facet_wrap(~ Area)
## Warning: Use of `temp$decdate` is discouraged.
## ℹ Use `decdate` instead.
## Use of `temp$decdate` is discouraged.
## ℹ Use `decdate` instead.
## Warning: Removed 798 rows containing missing values (`geom_point()`).
## Removed 798 rows containing missing values (`geom_point()`).

Step three: Make a lineplot of salinity for each station, grouped into different ‘Areas’

temp.plot.scatterplot = ggplot(temp, aes(x = temp$decdate, y = Salinity))
temp.plot.scatterplot + facet_wrap(~ Station) + geom_line(aes(group=Area, color=Area))
## Warning: Use of `temp$decdate` is discouraged.
## ℹ Use `decdate` instead.

Step four: Do the same but only for area ‘OS’

temp.plot.scatterplot <- ggplot(subset(temp, Area == "OS"), aes(x = decdate, y = Salinity))
temp.plot.scatterplot <- temp.plot.scatterplot + geom_line(aes(group = Station), color = "darkblue") + facet_wrap(~ Station)
temp.plot.scatterplot