forked from OSUCliMates/DataDash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup_plot_sawtooth.R
99 lines (88 loc) · 3.23 KB
/
setup_plot_sawtooth.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# #find variable indeces
# times <- 25550:56939
# gbg<-tidync("/home/ST505/CESM-LENS/historical/PREC.nc")%>%
# hyper_filter(lat = lat<18)%>%
# hyper_filter(lon = lon<224)%>%
# hyper_filter(time= time==25550)%>%
# hyper_tibble()
# members <- gbg$mem
# gbg<-tidync("/home/ST505/CESM-LENS/historical/PREC.nc")%>%
# hyper_filter(mem = mem==1)%>%
# hyper_filter(time= time==25550)%>%
# hyper_tibble()
# latitudes <- unique(gbg$lat)
# longitudes <- unique(gbg$lon)
# remove(gbg)
#This function needs the following data frame to run:
decadal_average_cumulative_prec_waveforms <- readRDS("Data/yearly_cumulative_prec.rds")
#use ggplot to make an error message
err_plot <- ggplot()+
annotate(geom="text",y=1,x=1,label=
"Oops! Your selection doesn't have any Station Locations in it.
Please drag and drop on the sidebar map again to make a selection that covers one or more of the points.",
size=5)+
theme_void()
# lat_min <- latitudes[6]
# lat_max <- latitudes[8]
# lon_min <- longitudes[24]
# lon_max <- longitudes[26]
plot_sawtooths <- function(lat_min,lat_max,lon_min,lon_max){
#first we check to see that there were raster points selected
dat <- decadal_average_cumulative_prec_waveforms%>%
filter(latitude>=lat_min&latitude<=lat_max)%>%
filter(longitude>=lon_min&longitude<=lon_max)
ndat <- dat%>%
select(avg_cumulative_prec)%>%
summarise(n=n())%>%
as.numeric()
#After checking to make sure that there were raster points selected, this code runs
if(ndat==0){err_plot}else{
#generate cumulative rainfall (sawtooth) aggregate plots
dat%>%
group_by(water_day,decade)%>%
summarise(cum_prec = mean(avg_cumulative_prec))%>%
ggplot()+
geom_line(aes(x=water_day,y=cum_prec,color=as.factor(decade)))+
scale_color_discrete_sequential(palette = "viridis")+
theme_dd()+
labs(x="Day",
y="Mean Cumulative Precipitation (cm)",
color="Decade")+
scale_x_continuous(
breaks=c(1,62,124,183,244,305),
labels=c("Oct 1","Dec 1","Feb 1","Apr 1","Jun 1","Aug 1")
)
}
}
plot_num_der <- function(lat_min,lat_max,lon_min,lon_max){
#first we check to see that there were raster points selected
dat <- decadal_average_cumulative_prec_waveforms%>%
filter(latitude>=lat_min&latitude<=lat_max)%>%
filter(longitude>=lon_min&longitude<=lon_max)
ndat <- dat%>%
select(avg_cumulative_prec)%>%
summarise(n=n())%>%
as.numeric()
#After checking to make sure that there were raster points selected, this code runs
if(ndat==0){ggplot()}else{
#generate cumulative rainfall (sawtooth) aggregate plots
dat%>%
group_by(water_day,decade)%>%
summarise(cum_prec = mean(avg_cumulative_prec))%>%
group_by(decade)%>%
nest()%>%
mutate(agg_prec=map(data,nderiv))%>%
unnest()%>%
ggplot()+
geom_line(aes(x=water_day,y=agg_prec,color=as.factor(decade)))+
scale_color_discrete_sequential(palette = "viridis")+
theme_dd()+
labs(x="Day",
y="Average Precipitation (cm/day)",
color="Decade")+
scale_x_continuous(
breaks=c(1,62,124,183,244,305),
labels=c("Oct 1","Dec 1","Feb 1","Apr 1","Jun 1","Aug 1")
)
}
}