exercise07

Author

Hangu Lee

0. Setting

# Load the package
library(ggplot2)

# Load the dataset
temp = read.csv("./data/Temperature.csv")

# Data Inspection
str(temp)
'data.frame':   8528 obs. of  16 variables:
 $ Sample     : chr  "DANT.19900110" "DANT.19900206" "DANT.19900308" "DANT.19900404" ...
 $ Date       : int  19900110 19900206 19900308 19900404 19900509 19900620 19900718 19900815 19900919 19901017 ...
 $ DateNr     : chr  "10/1/90" "6/2/90" "8/3/90" "4/4/90" ...
 $ dDay1      : int  7 34 64 91 126 168 196 224 259 287 ...
 $ dDay2      : int  9 36 66 93 128 170 198 226 261 289 ...
 $ dDay3      : int  9 36 66 93 128 170 198 226 261 289 ...
 $ Station    : chr  "DANT" "DANT" "DANT" "DANT" ...
 $ Area       : chr  "WZ" "WZ" "WZ" "WZ" ...
 $ X31UE_ED50 : num  681380 681380 681380 681380 681380 ...
 $ X31UN_ED50 : num  5920571 5920571 5920571 5920571 5920571 ...
 $ Year       : int  1990 1990 1990 1990 1990 1990 1990 1990 1990 1990 ...
 $ Month      : int  1 2 3 4 5 6 7 8 9 10 ...
 $ Season     : chr  "winter" "winter" "spring" "spring" ...
 $ Salinity   : num  29.2 27.4 25 28.8 33.3 ...
 $ Temperature: num  4 6 7.3 8.2 17.4 18.1 17 21 15.1 13.8 ...
 $ CHLFa      : num  1.3 NA 21.1 25 10.2 6.2 7.9 7.85 13 11.8 ...

1. Part1

Salinity Histogram

ggplot(temp, aes(x = Salinity)) + 
  geom_histogram(fill = "lightblue") +
  ggtitle("Histogram of Salinity")
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Warning: Removed 798 rows containing non-finite outside the scale range
(`stat_bin()`).

Salinity Histograms by Year

ggplot(temp, aes(x = Salinity)) + 
  geom_histogram(fill = "darkgreen") +
  facet_wrap(~ factor(Year)) +
  ggtitle("Salinity Histograms by Year")
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Warning: Removed 798 rows containing non-finite outside the scale range
(`stat_bin()`).

Salinity Histograms by Month

ggplot(temp, aes(x = Salinity)) + 
  geom_histogram(fill = "orange") +
  facet_wrap(~ factor(Month)) +
  ggtitle("Salinity Histograms by Month")
`stat_bin()` using `bins = 30`. Pick better value `binwidth`.
Warning: Removed 798 rows containing non-finite outside the scale range
(`stat_bin()`).

Temperature Boxplots by Station

ggplot(temp, aes(x = reorder(Station, Temperature, median), y = Temperature)) + 
  geom_boxplot(fill = "lightyellow") +
  ggtitle("Temperature Boxplots by Station (Ordered by Median Temperature)") +
  xlab("Station") + ylab("Temperature")
Warning: Removed 927 rows containing non-finite outside the scale range
(`stat_boxplot()`).

Save the Figure

ggsave("station_temp_boxplot.png")
Saving 7 x 5 in image
Warning: Removed 927 rows containing non-finite outside the scale range
(`stat_boxplot()`).

2. Part 2

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

Scatterplots of Temperature and Salinity Over Time

# Temperature Changes over Time
ggplot(temp, aes(x = decdate, y = Temperature)) + 
  geom_point(color = "darkorange") +
  ggtitle("Temperature Changes over Time") + 
  xlab("Decimal Date") + ylab("Temperature")
Warning: Removed 927 rows containing missing values or values outside the scale range
(`geom_point()`).

# Salinity Changes over Time
ggplot(temp, aes(x = decdate, y = Salinity)) + 
  geom_point(color = "blue") +
  ggtitle("Salinity Changes over Time") + 
  xlab("Decimal Date") + ylab("Salinity")
Warning: Removed 798 rows containing missing values or values outside the scale range
(`geom_point()`).

Salinity Scatterplot Faceted by Areas

# Salinity Scatterplot Faceted by Areas
ggplot(temp, aes(x = decdate, y = Salinity, color = Area)) + 
  geom_point() +
  facet_wrap(~ Area) +
  ggtitle("Salinity Time Series Faceted by Area") + 
  xlab("Decimal Date") + ylab("Salinity")
Warning: Removed 798 rows containing missing values or values outside the scale range
(`geom_point()`).

Salinity Lineplot for Each Station by Areas

ggplot(temp, aes(x = decdate, y = Salinity, color = Station)) + 
  geom_line(aes(group = Station)) + 
  facet_wrap(~ Area) +
  ggtitle("Salinity Lineplot for Each Station by Areas") +
  xlab("Decimal Date") + ylab("Salinity")
Warning: Removed 4 rows containing missing values or values outside the scale range
(`geom_line()`).

Salinity Lineplot for Area ‘OS’ Only

ggplot(subset(temp, Area == "OS"), aes(x = decdate, y = Salinity, color = Station)) + 
  geom_line(aes(group = Station)) + 
  geom_point() + 
  ggtitle("Salinity Lineplot for Area 'OS' Only") +
  xlab("Decimal Date") + ylab("Salinity")
Warning: Removed 23 rows containing missing values or values outside the scale range
(`geom_point()`).