R Markdown

This is an R Markdown document. Markdown is a simple formatting syntax for authoring HTML, PDF, and MS Word documents. For more details on using R Markdown see http://rmarkdown.rstudio.com.

When you click the Knit button a document will be generated that includes both content as well as the output of any embedded R code chunks within the document. You can embed an R code chunk like this:

library(ggplot2)
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
library(tidyverse)                        
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ tibble  3.1.7     ✔ purrr   0.3.4
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ lubridate::as.difftime() masks base::as.difftime()
## ✖ lubridate::date()        masks base::date()
## ✖ dplyr::filter()          masks plotly::filter(), stats::filter()
## ✖ lubridate::intersect()   masks base::intersect()
## ✖ dplyr::lag()             masks stats::lag()
## ✖ lubridate::setdiff()     masks base::setdiff()
## ✖ lubridate::union()       masks base::union()
Sys.setenv("plotly_username"="garrett.mcgurk")
Sys.setenv("plotly_api_key"="ChGJeBqUCnFU6vfTviw1")



#add dry creek @ SYR in
SYR <- read.csv("//skyriver.ucsd.edu/CW3E_data/CW3E_Streamflow_Archive/SYR/Processed/SYR__LevelLogger_compensated.csv",fileEncoding="UTF-8-BOM")

#write.csv(SYR.skyriver,file = "C:/Users/Garrett/Documents/R/Data/SYR_local_copy.csv")
#SYR <-read.csv("C:/Users/Garrett/Documents/R/Data/SYR_local_copy.csv")
SYR$date.time= paste(SYR$Date,SYR$Time)
SYR$date.time= as.POSIXct(SYR$date.time, tz= "UTC", format= "%m/%d/%Y %I:%M:%S %p")

head(SYR)
##       Date        Time     LEVEL.ft TEMPERATURE.C           date.time
## 1 8/5/2021  9:30:00 PM -0.003608924        34.911 2021-08-05 21:30:00
## 2 8/5/2021  9:45:00 PM -0.002624672        34.918 2021-08-05 21:45:00
## 3 8/5/2021 10:00:00 PM  2.067257284        25.441 2021-08-05 22:00:00
## 4 8/5/2021 10:15:00 PM  2.079396392        24.164 2021-08-05 22:15:00
## 5 8/5/2021 10:30:00 PM  2.070866208        24.061 2021-08-05 22:30:00
## 6 8/5/2021 10:45:00 PM  2.070210040        23.953 2021-08-05 22:45:00
#fix weird timezone issue
Sys.setenv(TZ="America/Los_Angeles")
Sys.getenv("TZ")
## [1] "America/Los_Angeles"
#add Yuba River @Marysville in
#Yuba.MRY <- read.csv("~/R/Data/Yuba@MRY_082021_122021.csv")
#Yuba.MRY$date.time= paste(Yuba.MRY$date,Yuba.MRY$time)
#Yuba.MRY$date.time= as.POSIXct(Yuba.MRY$date.time, tz= Sys.getenv("TZ"), format= "%m/%d/%Y %H:%M")

#add Yuba River @Smartsville in
YRS.NY28 <- read.csv("//skyriver.ucsd.edu/CW3E_data/CW3E_Streamflow_Archive/Yuba/20220512_NY28_ForCW3E_formatted.csv")
head(YRS.NY28)
##       date.time Discharge.cfs Level.ft
## 1 8/1/2021 0:00           930    7.580
## 2 8/1/2021 0:15           938    7.595
## 3 8/1/2021 0:30           938    7.595
## 4 8/1/2021 0:45           938    7.595
## 5 8/1/2021 1:00           938    7.595
## 6 8/1/2021 1:15           938    7.595
YRS.NY28$date.time= as.POSIXct(YRS.NY28$date.time, tz= Sys.getenv("TZ"), format= "%m/%d/%Y %H:%M")

#convert YRS to UTC time
attr(YRS.NY28$date.time, "tzone") <- "UTC"


#add the rain data in
BVS.precip <- read.csv("~/R/Data/Browns_Valley_2min_precip.csv")
BVS.precip$TIMESTAMP= as.POSIXct(BVS.precip$TIMESTAMP, tz= "UTC", format= "%Y-%m-%d %H:%M:%S")


BVS.precip$Rain_in_Tot= BVS.precip$Rain_mm_Tot*0.03937007874

rain.sub <- BVS.precip[BVS.precip$TIMESTAMP > "2021-10-20" &    # Extract data frame subset
                         BVS.precip$TIMESTAMP < "2021-11-3",]

#create 15 minute rain data
BVS.15=BVS.precip
BVS.15$date.time = cut(BVS.precip$TIMESTAMP, breaks="15 min")
BVS.precip.15= aggregate(Rain_in_Tot ~ date.time, FUN=sum, data=BVS.15)
#convert it back to posix
BVS.precip.15$date.time= as.POSIXct(BVS.precip.15$date.time, tz= "UTC", format= "%Y-%m-%d %H:%M:%S")



#set date range for plots
plot.dates= as.POSIXct(c("2021-10-01", "2022-03-05"))

#plot whole 15min rain timeseries
BVS.precip.plot <- ggplot(BVS.precip.15, aes(date.time, Rain_in_Tot)) +
  geom_bar(stat = 'identity', fill = "blue") +
  theme_bw() +
  ylab("Precip (in)") +
  scale_x_datetime(limits = plot.dates)+
  #labs(title = "BVS") +
  scale_y_reverse()#+

ggplotly(BVS.precip.plot,dynamicTicks = TRUE)
## Warning: Removed 16282 rows containing missing values (position_stack).
#Plot individual Hydrographs for sites
SYR.hydrograph<-ggplot(data=SYR,aes(date.time,LEVEL.ft))+
  geom_line(color="#477ed6")+
  xlab("Date") + ylab("Level (ft)")+
  scale_x_datetime(limits = plot.dates)
ggplotly(SYR.hydrograph,dynamicTicks = TRUE)
#Yuba.MRY.hydrograph<-ggplot(data=Yuba.MRY,aes(date.time,Level.ft))+
 # geom_line(color="#477ed6")+
  #xlab("Date") + ylab("Level (ft)")+
  #scale_x_datetime(limits = plot.dates)
#ggplotly(Yuba.MRY.hydrograph,dynamicTicks = TRUE)

YRS.NY28.hydrograph.cfs<-ggplot(data=YRS.NY28,aes(date.time,Discharge.cfs))+
  geom_line(color="#477ed6")+
  xlab("Date") + ylab("Discharge (cfs)")+
  scale_x_datetime(limits = plot.dates)
ggplotly(YRS.NY28.hydrograph.cfs,dynamicTicks = TRUE)
YRS.NY28.hydrograph.ft<-ggplot(data=YRS.NY28,aes(date.time,Level.ft))+
  geom_line(color="#477ed6")+
  xlab("Date") + ylab("Level (ft)")+scale_x_datetime(limits = plot.dates)
ggplotly(YRS.NY28.hydrograph.ft,dynamicTicks = TRUE)
#plot the level vs flow hydrograph with rain data 
full.hydros.cfs<- subplot(ggplotly(BVS.precip.plot,dynamicTicks = TRUE),
                ggplotly(SYR.hydrograph,dynamicTicks = TRUE), 
                ggplotly(YRS.NY28.hydrograph.cfs,dynamicTicks = TRUE),
                nrows = 3, shareX = T,titleY = TRUE)
## Warning: Removed 16282 rows containing missing values (position_stack).
annotations = list( 
  list( 
    x = 0.8,  
    y = 0.9,  
    text = "BVS",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),
  list( 
    x = 0.8,  
    y = 0.6,  
    text = "SYR",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),  
  list( 
    x = 0.8,  
    y = 0.2,  
    text = "Yuba NY28 Smartsville",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  )
)
full.hydros.labs <- full.hydros.cfs %>%layout(annotations = annotations) 
full.hydros.labs
#plot the level hydrograph with rain data 
full.hydros.ft <- subplot(ggplotly(BVS.precip.plot,dynamicTicks = TRUE),
                          ggplotly(SYR.hydrograph,dynamicTicks = TRUE), 
                          ggplotly(YRS.NY28.hydrograph.ft,dynamicTicks = TRUE),
                          nrows = 3, shareX = T,titleY = TRUE)
## Warning: Removed 16282 rows containing missing values (position_stack).
annotations = list( 
  list( 
    x = 0.8,  
    y = 0.9,  
    text = "BVS",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),
  list( 
    x = 0.8,  
    y = 0.6,  
    text = "SYR",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  ),  
  list( 
    x = 0.8,  
    y = 0.2,  
    text = "Yuba NY28 Smartsville",  
    xref = "paper",  
    yref = "paper",  
    xanchor = "center",  
    yanchor = "bottom",  
    showarrow = FALSE 
  )
)
full.hydros.labs.ft <- full.hydros.ft %>%layout(annotations = annotations, scale_x_datetime(limits = plot.dates))
full.hydros.labs.ft

Including Plots

You can also embed plots, for example:

Note that the echo = FALSE parameter was added to the code chunk to prevent printing of the R code that generated the plot.