Plotting precip2 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(precip2[,i]) #indicator variable to take out NAs of plots
station_title <- paste(station_ids_names[i ,2],
"Precipitation") #changing title for each station for plots
plot(date2[indicator], precip2[indicator, i], col = "blue", main = station_title,
xlab = "Time (months)", ylab = "Total Precipitation (in)", type = "h")
}
#Cal Poly SLO Plot
indicator <- !is.na(precip2[,35])
station_title <- "Cal Poly SLO Precipitation"
plot(date2[indicator], precip2[indicator, 35], col = "blue", main = station_title,
xlab = "Time (months)", ylab = "Total Precipitation (in)", type = "h")
Plotting precip2 Data (by time)
good <- 1:36
good <- good[-c(3, 9, 19, 20, 21)]
plot(date2, precip2[, 1], type = "h", pch = 1,
main = "Precipitation over Time", xlab = "Time (months)",
ylab = "Total Precipitation (in)", col = "blue", ylim = c(0, 27))
for (x in good){
points(date2, precip2[, x], type = "h", pch = 1, col = "blue")
}
Plotting temps_min2 and temps_max2 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(temps_min2[,x]) & #indicator to remove NAs from plot
!is.na(temps_max2[,x])
temp_title <- paste(station_ids_names[x, 2], "Max/Min Temperatures") #changing title
temp_d <- date2[indicator]
y1 <- temps_min2[indicator, x]
y2 <- temps_max2[indicator, x]
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(temps_min2[,35]) & !is.na(temps_max2[,35])
temp_title <- "Cal Poly SLO Max/Min Temperatures"
temp_d <- date2[indicator]
y1 <- temps_min2[indicator, 35]
y2 <- temps_max2[indicator, 35]
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(34,90), 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)
Plotting temps_min2 and temps_max2 Data (by time)
good2 <- 1:36
good2 <- good2[-c(2, 6, 7, 8, 13, 16, 22,
23, 24, 26, 27, 30, 31,
32, 33, 34, 36)]
plot(date2, temps_min2[, 1], type = "p", pch = 1, main = "Mean Temperature over Time",
xlab = "Time (months)", ylab = "Mean Temperature (Fahrenheit)",
col = "blue", ylim = c(15, 110))
avg_slope1_num <- 0 #initializing slope numerator for min temps
avg_slope2_num <- 0 #initializing slope numerator for max temps
avg_intercept1_num <- 0 #initializing intercapt numerator for min temps
avg_intercept2_num <- 0 #initializing intercapt numerator for max temps
c = 0 #initializing index variable
for (z in good2){
points(date2, temps_min2[, z], type = "p",
pch = 1, col = "blue") #adding min temps points in blue
points(date2, temps_max2[, z], type = "p",
pch = 1, col = "red") #adding max temps points in red
require(stats)
reg1 <- lm(temps_min2[, z] ~ date2) #regression for min temps
reg2 <- lm(temps_max2[, z] ~ date2) #regression for max temps
coeff1 <- coefficients(reg1) #coefficients for min temps
coeff2 <- coefficients(reg2) #coefficients for max temps
slope1 <- round(coeff1[2],1) #slope for min temps
slope2 <- round(coeff2[2],1) #slope for max temps
intercept1 <- round(coeff1[1],1) #intercept for min temps
intercept2 <- round(coeff2[1],1) #intercept for max temps
avg_slope1_num <- avg_slope1_num + slope1 #adding min temps slope to avg slope numerator
avg_slope2_num <- avg_slope2_num + slope2 #adding max temps slope to avg slope numerator
avg_intercept1_num <- avg_intercept1_num +
intercept1 #adding min temps intercept to avg intercept num
avg_intercept2_num <- avg_intercept2_num +
intercept2 #adding max temps intercept to avg intercept num
c = c + 1 #indexing
}
avg_slope1 = avg_slope1_num / c #avg min temps slope num divded by indexing number
avg_slope2 = avg_slope2_num / c #avg max temps slope num divded by indexing number
avg_intercept1 = avg_intercept1_num / c #avg min temps interc num divded by indexing number
avg_intercept2 = avg_intercept2_num / c #avg max temps interc num divded by indexing number
abline(avg_intercept1, avg_slope1, lwd = 2) #regression line for min temps
abline(avg_intercept2, avg_slope2, lwd = 2) #regression line for max temps