Plotting precip Data (by time and station)

#Precipitation over Days Scatter Plot For Each Station
date <- unique(data2[, 6])                       #date
date <- format(sort(date))                       #sorting dates and converting from numeric to char string
date2 <- as.Date(date, format = "%Y%m%d")        #converting dates into proper date format
station_ids_names <- unique(data2[, 1:2])        #station names
good <- 1:36
good <- good[-c(3, 9, 19, 20, 21, 35)]           #takes out stations that don't have precipitations
for (i in good){                                 #loop goes through stations that have precipitations
  indicator <- !is.na(new_precip2[,i])           #indicator variable to take out NAs of plots
  date_precip <- date2[indicator]
  data_precip <- new_precip2[indicator, i]
  indicator2 <- data_precip >= 25                #indicator to remove values greater than 25
  ymax <- quantile(data_precip, .99)             #changing outlier to 99th percentile
  data_precip[indicator2] <- ymax
  station_title <- paste(station_ids_names[i ,2], "Precipitation") #changing title for each station
  plot(date_precip, data_precip, col = "blue", main = station_title, 
       xlab = "Time (months)", ylab = "Total Precipitation (in)", type = "h")
}

#Cal Poly SLO Plot
indicator <- !is.na(new_precip2[,35])     
date_precip <- date2[indicator]
data_precip <- new_precip2[indicator, 35]
indicator2 <- data_precip >= 25          
ymax <- quantile(data_precip, .99)
data_precip[indicator2] <- ymax
station_title <- "Cal Poly SLO Precipitation"
plot(date_precip, data_precip, col = "blue", main = station_title, 
     xlab = "Time (months)", ylab = "Total Precipitation (in)", type = "h")


Plotting temps_min and temps_max Data (by time and station)

#Temperatures over Time Scatter Plot
station_ids_names <- unique(data2[, 1:2])    #station ids and names
date <- unique(data2[, 6])                   #date
date <- format(sort(date))                   #sorting dates and converting from numeric to character string
date2 <- as.Date(date, format = "%Y%m%d")  
good2 <- 1:36
good2 <- good2[-c(2, 6, 7, 8, 13, 16, 22,
                  23, 24, 26, 27, 30, 31, 
                  32, 33, 34, 35, 36)]       #removing all stations without min and max temperatures
for (x in good2) {                           #loops through all stations with min and max temperatures
  indicator <- !is.na(new_temps_min2[,x]) & !is.na(new_temps_max2[,x])
  temp_title <- paste(station_ids_names[x, 2], "Max/Min Temperatures") #changing title
  temp_d <- date2[indicator]
  y1 <- new_temps_min2[indicator, x]
  y2 <- new_temps_max2[indicator, x]
  indicator2 <- y1 < 5                      #indicator for min values less than 5
  indicator3 <- y1 > 85                     #indicator for min values greater than 85
  indicator4 <- y2 < 40                     #indicator for max values less than 40
  indicator5 <- y2 > 120                    #indicator for max values greater than 120
  y1min <- quantile(y1, .01)                #setting outliers to 1st percentile
  y1[indicator2] <- y1min
  y1max <- quantile(y1, .99)                #setting outliers to 99th percentile
  y1[indicator3] <- y1max
  y2min <- quantile(y2, .01)                #setting outliers to 1st percentile
  y2[indicator4] <- y2min
  y2max <- quantile(y2, .99)                #setting outliers to 99th percentile
  y2[indicator5] <- y2max
  yr <- range(c(y1, y2))
  plot(temp_d, y1, type = "p", pch = 1, main = temp_title, xlab = "Time (months)", 
       ylab = "Temperature (Fahrenheit)", col = "blue", ylim = yr)
  points(temp_d, y2, type = "p", pch = 1, col = "red")
  abline(lm(y1~temp_d), col = "blue", lwd = 2)
  abline(lm(y2~temp_d), col = "red", lwd = 2)
}

#Cal Poly SLO Plot
indicator <- !is.na(new_temps_min2[,35]) & !is.na(new_temps_max2[,35])
temp_title <- "Cal Poly SLO Max/Min Temperatures"
temp_d <- date2[indicator]
y1 <- new_temps_min2[indicator, 35]
y2 <- new_temps_max2[indicator, 35]
indicator2 <- y1 < 5
indicator3 <- y1 > 85
indicator4 <- y2 < 40
indicator5 <- y2 > 120
y1min <- quantile(y1, .01)
y1[indicator2] <- y1min
y1max <- quantile(y1, .99)
y1[indicator3] <- y1max
y2min <- quantile(y2, .01)
y2[indicator4] <- y2min
y2max <- quantile(y2, .99)
y2[indicator5] <- y2max
yr <- range(c(y1, y2))
trange <- as.Date(c("19280101", "20170101"), format = "%Y%m%d")
plot(temp_d, y1, type = "p", pch = 1, main = temp_title, xlab = "Time (months)", 
     ylab = "Temperature (Fahrenheit)", col = "blue", ylim = c(32, 92), xlim = trange)
points(temp_d, y2, type = "p", pch = 1, col = "red")
abline(lm(y1~temp_d), col = "blue", lwd = 2)
abline(lm(y2~temp_d), col = "red", lwd = 2)