-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.Rhistory
170 lines (170 loc) · 5.98 KB
/
.Rhistory
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#Figure production
#Created by Arthur Barros
#EMP Zooplankton Enviornmental Scientist
#last updated 8/3/2021
rm( list = ls()) #clear env
#read in packages
library(tidyr)
library(dplyr)
library(ggplot2)
library(plotrix)
library(purrr)
library(cowplot)
library(plyr)
library(gridExtra)
Zoop_chl<-readRDS("outputs/Zoop_chl.rds")
target_adults<-c("ACARTELA","ACARTIA","EURYTEM","PDIAPFOR","SINOCAL","OITHDAV","LIMNOSPP","LIMNOSINE","LIMNOTET","BOSMINA","DAPHNIA","DIAPHAN","H_longirostris","N_kadiakensis","N_mercedis")
#create sampling coverage heat map
samples<-unique(dplyr::select(Zoop_chl,Year,Survey,SampleDate,Gear,DWRStationNo))
samples<-samples%>%
group_by(Year,Survey,Gear)%>%
summarize(tows=length(DWRStationNo))
gear_types<-c("CB","Pump","Mysid")
plot_list<-list()
for(i in 1:3){
z<-target_adults[i]
d1<-d%>%
filter(ZooCode==z)
d=samples%>%
p<-ggplot(d,aes(Year,Survey))+
geom_tile(aes(fill=tows))+
theme_classic()+
theme(axis.text.x=element_text(angle=90,size=10),axis.text.y = element_text(size=10),legend.position = "none")+
scale_fill_gradient(low="white",high="red")+
geom_text(aes(label=round(tows,1)),size=4)+
guides(colour=F)+
ggtitle(paste(target_gear,"Sampling Coverage with DWR Chl data",sep=" "))
plot_list[[i]]<-p
}
gear_plots<-plot_grid(plotlist=plot_list,labels = "AUTO", align = "v",ncol=1)
gear_plots
saveRDS(gear_plots,"Data/sampling_coverage.rds")
save_plot("Figures/chl_sampling_coverage.png", gear_plots,ncol=1,base_height = 14,base_width = 10)
#Lets do some linear modeling
#this function creates the model from the dataset (d), then creates a character string with the equation, r-squared, and p-value. This is then used in the plotting
lmp <- function (modelobject) {
coeffs<- coef(summary(modelobject))
p<-if (dim(coeffs)[[1]]==1) # if only one row of pvalues then NA
p.value <- NA
else
p.value <- coeffs[2,4]
return(p)
}
lm_eqn <- function(df){
m <- lm(CPUE ~ chl_taxa, df);
eq <- substitute(~~italic(r)^2~"="~r2*","~~italic(p-value)*"="~pvalue,
list(intercept = format(coef(m)[1], digits = 2),
slope = format(coef(m)[2], digits = 2),
r2 = format(summary(m)$r.squared, digits = 3),
pvalue=format(lmp(m),digits=4)))
as.character(as.expression(eq));
}
#let's run the data grouped by survey and year
#Run for loops for gams by taxa and chl species
d<-dplyr::select(Zoop_chl,Year,Survey,DWRStationNo,SampleDate,ZooCode,CommonName,CPUE,27:35)
for(i in 1:length(target_adults)){
z<-target_adults[i]
d1<-d%>%
filter(ZooCode==z)
for(s in 1:length(chl_species)){
chl_taxa<-chl_species[s]
d2<-dplyr::select(d1,Year,Survey,DWRStationNo,SampleDate,CommonName,CPUE,matches(chl_taxa))
d2<-dplyr::rename(d2,"chl_taxa"=7)
eq<-lm_eqn(d2)#apply linear model function
p <- ggplot(data = d2, aes(x = chl_taxa, y = CPUE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point(aes())+
geom_smooth(se = T, method = "lm") +
ggtitle(paste(z,chl_taxa,sep=" "))+
theme_classic()+
geom_text(x=-Inf,y=Inf,vjust="top",hjust="left",label=lm_eqn(d2),parse=T)
p
plot_list[[s]]<-p
}
taxa_plots<-plot_grid(plotlist=plot_list,align = "h",nrow=1)
taxa_plots
save_plot(paste("figures/",z,chl_taxa,".png",sep=""),taxa_plots,base_height = 6,base_width = 24)
}
d<-dplyr::select(Zoop_chl,Year,Survey,DWRStationNo,SampleDate,ZooCode,CommonName,CPUE,27:35)
d<-dplyr::select(Zoop_chl,Year,Survey,DWRStationNo,SampleDate,ZooCode,CommonName,CPUE,27:35)
for(i in 1:length(target_adults)){
z<-target_adults[i]
d1<-d%>%
filter(ZooCode==z)
for(s in 1:length(chl_species)){
chl_taxa<-chl_species[s]
d2<-dplyr::select(d1,Year,Survey,DWRStationNo,SampleDate,CommonName,CPUE,matches(chl_taxa))
d2<-dplyr::rename(d2,"chl_taxa"=7)
eq<-lm_eqn(d2)#apply linear model function
p <- ggplot(data = d2, aes(x = chl_taxa, y = CPUE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point(aes())+
geom_smooth(se = T, method = "lm") +
ggtitle(paste(z,chl_taxa,sep=" "))+
theme_classic()+
geom_text(x=-Inf,y=Inf,vjust="top",hjust="left",label=lm_eqn(d2),parse=T)
p
plot_list[[s]]<-p
}
taxa_plots<-plot_grid(plotlist=plot_list,align = "h",nrow=1)
taxa_plots
save_plot(paste("figures/",z,chl_taxa,".png",sep=""),taxa_plots,base_height = 6,base_width = 24)
}
View(d)
i=1
s=1
z<-target_adults[i]
d1<-d%>%
filter(ZooCode==z)
chl_taxa<-chl_species[s]
chl_species<-colnames(gam_data[8:16])
d<-dplyr::select(Zoop_chl,Year,Survey,DWRStationNo,SampleDate,ZooCode,CommonName,CPUE,27:35)
for(i in 1:length(target_adults)){
z<-target_adults[i]
d1<-d%>%
filter(ZooCode==z)
for(s in 1:length(chl_species)){
chl_taxa<-chl_species[s]
d2<-dplyr::select(d1,Year,Survey,DWRStationNo,SampleDate,CommonName,CPUE,matches(chl_taxa))
d2<-dplyr::rename(d2,"chl_taxa"=7)
eq<-lm_eqn(d2)#apply linear model function
p <- ggplot(data = d2, aes(x = chl_taxa, y = CPUE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point(aes())+
geom_smooth(se = T, method = "lm") +
ggtitle(paste(z,chl_taxa,sep=" "))+
theme_classic()+
geom_text(x=-Inf,y=Inf,vjust="top",hjust="left",label=lm_eqn(d2),parse=T)
p
plot_list[[s]]<-p
}
taxa_plots<-plot_grid(plotlist=plot_list,align = "h",nrow=1)
taxa_plots
save_plot(paste("figures/",z,chl_taxa,".png",sep=""),taxa_plots,base_height = 6,base_width = 24)
}
chl_species<-colnames(gam_data[8:16])
chl_species<-colnames(d[8:16])
for(i in 1:length(target_adults)){
z<-target_adults[i]
d1<-d%>%
filter(ZooCode==z)
for(s in 1:length(chl_species)){
chl_taxa<-chl_species[s]
d2<-dplyr::select(d1,Year,Survey,DWRStationNo,SampleDate,CommonName,CPUE,matches(chl_taxa))
d2<-dplyr::rename(d2,"chl_taxa"=7)
eq<-lm_eqn(d2)#apply linear model function
p <- ggplot(data = d2, aes(x = chl_taxa, y = CPUE)) +
geom_smooth(method = "lm", se=FALSE, color="black", formula = y ~ x) +
geom_point(aes())+
geom_smooth(se = T, method = "lm") +
ggtitle(paste(z,chl_taxa,sep=" "))+
theme_classic()+
geom_text(x=-Inf,y=Inf,vjust="top",hjust="left",label=lm_eqn(d2),parse=T)
p
plot_list[[s]]<-p
}
taxa_plots<-plot_grid(plotlist=plot_list,align = "h",nrow=1)
taxa_plots
save_plot(paste("figures/",z,chl_taxa,".png",sep=""),taxa_plots,base_height = 6,base_width = 24)
}