Phosphorus Loading results to date for TP, Orthophosphate, and Chlorophyll a by site.

Updated July 24, 2015

library(dplyr)
library(ggplot2)
library(reshape2)
library(lubridate)
library(pander)
library(xtable)
setwd("\\\\psf/Home/Dropbox/")
#setwd("c:/Users/Katrina/Dropbox/")

# Assemble data into one dataframe
options(stringsAsFactors=TRUE)

phos1<- read.csv("Phosphorus/raw_grab/20150414/lab/15040023.csv", strip.white=TRUE)
phos2 <- read.csv("Phosphorus/raw_grab/20150428/lab/15040050.csv", strip.white=TRUE)
phos3 <- read.csv("Phosphorus/raw_grab/20150512/lab/15050020.csv", strip.white=TRUE)
phos4 <- read.csv("Phosphorus/raw_grab/20150526/lab/15050041.csv", strip.white=TRUE)
phos5 <- read.csv("Phosphorus/raw_grab/20150609/lab/PN15060012CHLA.csv", strip.white=TRUE)
phos6 <- read.csv("Phosphorus/raw_grab/20150707/lab/15070011.csv", strip.white=TRUE)

phos <- rbind(phos1, phos2, phos3, phos4, phos5, phos6)

# Add manual Chlorophyll data 
chl1 <- read.csv("Phosphorus/raw_grab/20150414/lab/15040023_Chl.csv", strip.white=TRUE)
chl2 <- read.csv("Phosphorus/raw_grab/20150428/lab/15040050_Chl.csv", strip.white=TRUE)
chl3 <- read.csv("Phosphorus/raw_grab/20150512/lab/15050020_Chl.csv", strip.white=TRUE)
chl4 <- read.csv("Phosphorus/raw_grab/20150526/lab/15050041_Chl.csv", strip.white=TRUE)
chl5 <- read.csv("Phosphorus/raw_grab/20150623/lab/20150623_Chl.csv", strip.white=TRUE)

chl1$RESULT <- as.factor(chl1$RESULT)
chl2$RESULT <- as.factor(chl2$RESULT)
chl3$RESULT <- as.factor(chl3$RESULT)
chl4$RESULT <- as.factor(chl4$RESULT)
chl5$RESULT <- as.factor(chl5$RESULT)
chl <- rbind(chl1, chl2, chl3, chl4, chl5)

phos <- phos %>%
  select(SAMPLE_NUMBER, COLLECTION_DATE, ANALYSIS_CODE, RESULT = COMBINATION_RESULT, ANALYSIS_UNIT)
phos <- rbind_all(list(phos, chl))

#ALIASES:   
#CHL data comes from two sources; change EPA value to CHL
phos$ANALYSIS_CODE <- as.character(phos$ANALYSIS_CODE)
phos$ANALYSIS_CODE[phos$ANALYSIS_CODE == "CLPHYL"] <- "CHLA"
phos$ANALYSIS_CODE <- as.factor(phos$ANALYSIS_CODE)

#CHANGE MAR006 to MAR003; early samples had wrong name; correct some other sample names
phos$SAMPLE_NUMBER <- as.character(phos$SAMPLE_NUMBER)
phos$SAMPLE_NUMBER[phos$SAMPLE_NUMBER == "MAR006"] <- "MAR003"
phos$SAMPLE_NUMBER[phos$SAMPLE_NUMBER == "MYR071-B"] <- "MYR071"
phos$SAMPLE_NUMBER[phos$SAMPLE_NUMBER == "MYR036"] <- "MAR036"
phos$SAMPLE_NUMBER <- as.factor(phos$SAMPLE_NUMBER)

phos$COLLECTION_DATE <- as.Date(phos$COLLECTION_DATE, "%m/%d/%y")

# For tables, organize results to display by site and date.
TP_to_date <- tbl_df(phos) %>%
  filter(ANALYSIS_CODE == "TP") 
OPHOS_to_date <- tbl_df(phos) %>%
  filter(ANALYSIS_CODE == "OPHOS")
CHL_to_date <- tbl_df(phos) %>%
  filter(ANALYSIS_CODE == "CHLA") 

TP_to_date <- dcast(TP_to_date, SAMPLE_NUMBER ~ COLLECTION_DATE, value.var = "RESULT")
OPHOS_to_date <- dcast(OPHOS_to_date, SAMPLE_NUMBER ~ COLLECTION_DATE, value.var = "RESULT")
CHL_to_date <- dcast(CHL_to_date, SAMPLE_NUMBER ~ COLLECTION_DATE, value.var = "RESULT")

TP and CHLA

# remove sample numbers not needed for graphs
phosGraph <- phos %>%
  filter(ANALYSIS_CODE =="TP" |ANALYSIS_CODE == "CHLA" ) %>%
  filter(SAMPLE_NUMBER != "CHL_DUP" & SAMPLE_NUMBER != "CHL_LAB_DUP" & 
           SAMPLE_NUMBER != "TP-DUP" & 
           SAMPLE_NUMBER != "LABDUP" & 
           SAMPLE_NUMBER != "CHL-DUP" & 
           SAMPLE_NUMBER != "TP-blank-new" & SAMPLE_NUMBER != "TP-blank-old" &
          SAMPLE_NUMBER != "TP-Dup" ) 

phosGraph$RESULT <- as.numeric(phosGraph$RESULT)
phosGraph$COLLECTION_DATE <- as.Date(phosGraph$COLLECTION_DATE, "%m/%d/%y")
  
g <- ggplot(phosGraph, aes(COLLECTION_DATE, RESULT, color = ANALYSIS_CODE, 
                           group=interaction(ANALYSIS_CODE, SAMPLE_NUMBER), 
                           fill= ANALYSIS_CODE)) 

g + geom_line() + geom_point() + facet_wrap(~ SAMPLE_NUMBER, ncol = 3) + 
  scale_color_manual(values = c("darkolivegreen4",  "violetred3")) +
  theme_bw() + theme(axis.text.x = element_text(angle = 45, hjust = 1))
## geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
## geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?
## geom_path: Each group consist of only one observation. Do you need to adjust the group aesthetic?

# g  + geom_bar(stat = "identity", position = "dodge", width = .5)  +  
# scale_color_brewer(palette = "Paired") +
#   scale_fill_brewer(palette = "Paired") + 
#   facet_wrap(~ SAMPLE_NUMBER, ncol = 3) +theme_bw() + 
#   theme(axis.text.x = element_text(angle = 45, hjust = 1))

TP results:

# tab <- xtable(TP_to_date, align = rep("c", dim(TP_to_date)[2] + 1))
# print(tab, type = "html")
panderOptions('table.split.table', Inf)
panderOptions('table.style', 'rmarkdown')
pander(TP_to_date)
SAMPLE_NUMBER 2015-04-14 2015-04-28 2015-05-12 2015-05-26 2015-06-09 2015-07-07
ABR006 22 27 21 29 28 33
ALB006 69 50 37 41 51 58.7
HOB002 12 16 8.1 18 11 16
MAR003 25 28 14 28 37 26
MAR036 NA 29 25 55 55 36
MIB0045 34 41 35 25 36 26.2
MYR071 NA 14 14 11 22 13
MYR33 26 24 17 28 27 50
MYR43 NA 24 24 24 28 26
SPPCTR 23 27 21 20 24 16.8
TP-blank-new NA NA NA NA ND NA
TP-blank-old NA NA NA NA ND NA
TP-Dup NA NA NA NA NA 57
TP-DUP NA 27 7.3 8.4 35 NA
UPLCTR 15 13 6.2 8.2 17 11
UPLUPL 26 NA NA NA NA NA
WEPCTR 15 17 6.9 7.4 21 17

Orthophosphate results:

pander(OPHOS_to_date)
SAMPLE_NUMBER 2015-04-28 2015-05-26
ABR006 ND ND
ALB006 ND 7.8
HOB002 ND 5.15
MAR003 ND ND
MAR036 ND ND
MIB0045 5.2 ND
MYR071 ND ND
MYR33 ND ND
MYR43 NA ND
OPHOS-DUP 1 5 NA
OPHOS-DUP 2 ND NA
OPHOS-DUP1 NA ND
OPHOS-DUP2 NA ND
SPPCTR ND ND
UPLCTR ND ND
WEPCTR ND ND

Chlorophyll results:

panderOptions('date', '%m/%d/%yy')
pander(CHL_to_date)
SAMPLE_NUMBER 2015-04-14 2015-04-28 2015-05-12 2015-05-26 2015-06-09 2015-06-23
CHL-DUP NA NA NA NA 12.34 NA
CHL_DUP NA 16 11 9.3 NA 20
CHL_LAB_DUP NA NA 5.3 NA NA NA
HOPCTR NA NA NA 8.3 NA NA
LABDUP 5.1 NA NA NA NA NA
LOLCTR NA NA NA NA 7.65 NA
MAR003 2.5 22 34 56 21.22 26
MYR33 4.1 22 37 39 20.62 24
MYR43 3.7 13 28 31 11.18 12
SPPCTR 14 36 9.4 10 8.71 12
UPLCTR 2.1 9.4 6.6 16 12.47 8.3
UPLUPL 1.8 NA NA NA NA 4.6
WEPCTR 5.1 14 6.6 5.3 5.42 8.9
WIPCTR NA 12 NA NA NA NA