forked from MortenVinther/SMSApp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_plots.R
241 lines (190 loc) · 9.54 KB
/
make_plots.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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
# file including functions for various plots
plot_other<-function(sp,firsty,lasty,chOther,firstYear=stq_year, finalYear){
x<-firstYear:finalYear
y<-rep(1.0,length(x))
names(y)<-as.character(x)
if (firstYear==firsty) y[as.character(firsty)]<-chOther
for (year in firsty:lasty) if (year>firstYear) {y[as.character(year)]<-y[as.character(year-1)]*chOther }
if (finalYear>lasty) for (year in ((lasty+1):finalYear)) y[as.character(year)]<-y[as.character(lasty)]
plot(x,y,ylab='stock abundance index',xlab='Year',main=sp,type='b')
}
plot_summary_new<-function(res,ptype=c('Yield','Fbar','SSB','Recruits','Dead','M2'),years=c(0,5000),species='Cod',splitLine=FALSE,incl.reference.points=FALSE,nox=2,noy=3) {
a<-as_tibble(subset(res$out$detail_sum,Year>=years[1] & Year<=years[2] & Species %in% species))
a2<-subset(histCondensed,Year>=years[1] & Year<=years[2] & Species %in% species)
a<-bind_rows(a,a2)%>% mutate(Year=as.integer(Year)) %>% arrange(Year)
b<-subset(res$out$detail_M2,Year>=years[1] & Year<=years[2] & Species %in% species)
b2<-subset(histAnnoM,Year>=years[1] & Year<=years[2] & Species %in% species)
b<-bind_rows(b,b2) %>% arrange(Year,Age)
if ("SSB" %in% ptype) {
pSSB<-ggplot(a,aes(x=Year,y=SSB))+
geom_line(lwd=1.5)+
geom_point(shape = 21, colour = "black", fill = "white", size = 2, stroke = 1.5)+
labs(x='',y=plotLabels['SSB'],title=paste(species,'SSB',sep=', '))+
ylim(0,max(a$SSB))+
theme(plot.title = element_text(size = 16, face = "bold",hjust=0))
if (splitLine) pSSB<- pSSB+geom_vline(xintercept=stq_year,col='blue',lty=3,lwd=1)
if (incl.reference.points) {
if (refPoints[species,'Blim']>0) pSSB<- pSSB+geom_hline(yintercept=refPoints[species,'Blim'],lty=2,lwd=1,col='red',,na.rm = TRUE)
if (refPoints[species,'Bpa']>0) pSSB<- pSSB+geom_hline(yintercept=refPoints[species,'Bpa'],lty=3,lwd=1,col='green',na.rm = TRUE)
}
}
if ("Recruits" %in% ptype) {
y <-aggregate(Recruits~Year,data=a,sum)
pRec<-ggplot(y,aes(x=Year,y=Recruits)) +
geom_bar(stat = "identity")+
labs(x='',y=plotLabels['Recruits'],title='Recruitment')+
theme(plot.title = element_text(size = 16, face = "bold"))
if (splitLine) pRec<- pRec+geom_vline(xintercept=stq_year,col='blue',lty=3,lwd=1)
}
if ("Fbar" %in% ptype) {
pF<-ggplot(a,aes(x=Year,y=Fbar))+
geom_line(lwd=1.5)+
geom_point(shape = 21, colour = "black", fill = "white", size = 2, stroke = 1.5)+
labs(x='',y=plotLabels['Fbar'],title='Fishing mortality, F')+
ylim(0,max(a$Fbar))+
theme(plot.title = element_text(size = 16, face = "bold"))
if (splitLine) pF<- pF+geom_vline(xintercept=stq_year,col='blue',lty=3,lwd=1)
if (incl.reference.points) {
if ( refPoints[species,'Flim']>0) pF<- pF+geom_hline(yintercept=refPoints[species,'Flim'],lty=2,lwd=1,col='red',na.rm = TRUE)
if (refPoints[species,'Fpa']>0) pF<- pF+geom_hline(yintercept=refPoints[species,'Fpa'],lty=3,lwd=1,col='green',na.rm = TRUE)
}
}
if ("Yield_old" %in% ptype) {
y <-aggregate(Yield~Year,data=a,sum)
pY<-ggplot(y,aes(x=Year,y=Yield)) +
geom_bar(stat = "identity")+
labs(x='',y=plotLabels['Yield'],title='Catch')+
theme(plot.title = element_text(size = 16, face = "bold"))
if (splitLine) pY<- pY+geom_vline(xintercept=stq_year,col='blue',lty=3,lwd=1)
}
if ("Yield" %in% ptype) {
y<-select(a,Year,Yield,yield.core) %>% mutate(yield.outside=Yield-yield.core,Yield=NULL) %>% select(Year,yield.outside,yield.core)
y<-as.data.frame(y)
if (any(y$yield.outside>0)) titl<-'Catch inside and outside the North Sea' else titl<-'Catch'
y<-reshape(y,direction='long',varying = list(2:3))
pY<-ggplot(y, aes(x = Year, y = yield.outside, fill = as.factor(time)) )+
geom_bar(stat = "identity")+
labs(x='',y=plotLabels['Yield'],title=titl)+
theme(plot.title = element_text(size = 16, face = "bold"),legend.position="none")
if (splitLine) pY<- pY+geom_vline(xintercept=stq_year,col='blue',lty=3,lwd=1)
}
if ("Dead" %in% ptype) {
y<-select(a,Year,Yield,DeadM1,DeadM2)
y<-as.data.frame(y)
if (any(y$DeadM2>0)) titl<-'Biomass removed\ndue to F, M1 and M2' else titl<-'Biomass removed\ndue to F and M1'
y<-reshape(y,direction='long',varying = list(2:4))
pD<-ggplot(y, aes(x = Year, y = Yield, fill = as.factor(time)) )+
geom_bar(stat = "identity")+
labs(x='',y=plotLabels['DeadM'],title=titl)+
theme(plot.title = element_text(size = 16, face = "bold"),legend.position="none")
if (splitLine) pD<- pD+geom_vline(xintercept=stq_year,col='blue',lty=3,lwd=1)
}
if ("M2" %in% ptype) if (any(b$M2>0)) {
b$Age<-as.factor(b$Age)
m2<-aggregate(M2~Year+Age,data=b,sum)
m2max<-aggregate(cbind(maxM2=M2)~Age,data=b,max)
m2<-droplevels(subset(merge(m2,m2max),maxM2>0.01 & as.character(Age)<"6"))
pM<-ggplot(m2,aes(x=Year,y=M2,color=Age, shape=Age)) +
geom_line(lwd=1)+
geom_point()+
labs(x='',y=plotLabels['Fbar'],title='M2 at age')+
ylim(0,max(m2max$maxM2))+
theme(plot.title = element_text(size = 16, face = "bold"),legend.position="none")
if (splitLine) pM<- pM+geom_vline(xintercept=stq_year,col='blue',lty=3,lwd=1)
}
if (any(b$M2>0)) out<-plot_grid(pSSB,pD,pF,pRec,pY,pM,ncol=3, nrow=2) else out<-plot_grid(pSSB,pD,pF,pRec,pY,ncol=3, nrow=2)
return(out)
}
#plot_summary_new(res=res$rv,ptype=c('Yield','Fbar','SSB','Recruits','Dead','M2'),years=c(0,5000),species='Cod',splitLine=FALSE,incl.reference.points=FALSE)
plot_who_eats<-function(x,pred,prey,predPrey='by prey',years=c(0,5000),exclHumans=TRUE,exclResidM1=TRUE){
x<-bind_rows(histEaten,x)
pp<-setNames(my.colors,predPreyFormat)
x<-filter(x,Year>=years[1] & Year<=years[2])
if (exclHumans) x<-filter(x,Predator !=predPreyFormat[2])
if (exclResidM1) x<-filter(x,Predator !=predPreyFormat[1])
if (prey != 'all preys') {
tit<-paste(prey, "eaten by");
x<-filter(x,Prey==prey)
} else tit<-'All preys eaten by'
p<-unique(x$Prey)
prey_pp<-pp[predPreyFormat %in% p]
if (pred !='all predators') {
x<-filter(x,Predator==pred);
tit<-paste(tit,pred)
} else tit<-paste(tit,'all predators')
p<-unique(x$Predator)
pred_pp<-pp[predPreyFormat %in% p]
if (dim(x)[[1]]==0) return(NULL)
if (predPrey=='by prey') {
pp<-prey_pp
x <-droplevels(aggregate(eatenW~Year+Prey,data=x,sum))
p<-ggplot(x, aes(x = Year, y = eatenW, fill = Prey))
} else if (predPrey=='by predator') {
pp<-pred_pp
x <-droplevels(aggregate(eatenW~Year+Predator,data=x,sum))
p<-ggplot(x, aes(x = Year, y = eatenW, fill = Predator))
}
p + geom_bar(stat = "identity") +
scale_fill_manual(values= pp[!is.na(names(pp))])+
labs(x="", y = paste("Biomass eaten",plotLabels['DeadM'])) +
ggtitle(tit) +
theme_minimal() +
theme(plot.title = element_text(size = 18, face = "bold"))
}
FoodWeb_plot <-function(x,pred,prey,predPrey='by prey',year=2000,width=NULL,height=NULL,margin=NULL,MyPalette=c('red','blue'),incl_sp='Cod') {
x<-bind_rows(histEaten,x)
x<-filter(x,Year==year & Predator %in% incl_sp & Prey %in% incl_sp)
#save(x,file=paste0('x2',year,'.data'))
x<-mutate(x,target=as.character(Predator),source=as.character(Prey)) %>% as.data.frame
# From these flows we need to create a node data frame: it lists every entities involved in the flow
nodes<-rbind(unique(data.frame(no=x$Predator.no, name=x$target)),
unique(data.frame(no=x$Prey.no, name=x$source))) %>% unique() %>% arrange(no) %>% select(name)
# need to reformat it.
x$IDsource=match(x$source, nodes$name)-1
x$IDtarget=match(x$target, nodes$name)-1
# prepare colour scale
#ColourScal ='d3.scaleOrdinal() .range(["#FDE725FF","#B4DE2CFF","#6DCD59FF","#35B779FF","#1F9E89FF","#26828EFF","#31688EFF","#3E4A89FF","#482878FF","#440154FF"])'
col2hex<-function (cname) {
colMat <- col2rgb(cname)
rgb(red = colMat[1, ]/255, green = colMat[2, ]/255, blue = colMat[3,
]/255)
}
ii<-NULL
for (i in col2hex(MyPalette)) ii<-paste(ii,paste0('"',i,'"'),sep=',')
ColourScal <- paste0('d3.scaleOrdinal() .range([', ii,'])')
# Make the Network
p<-sankeyNetwork(Links = x, Nodes = nodes,
Source = "IDsource", Target = "IDtarget",
Value = "eatenW", NodeID = "name",
sinksRight=FALSE,
units='kt',width=width,height=height,margin=margin,
# iterations = 0, # this will give the same layout (as defined by order of the nodes) for all years,
#colourScale=ColourScal,
nodeWidth=35, fontSize=12, nodePadding=10)
#saveWidget(p, file=file.path( paste0("sankey_",year,".html")))
return(p)
}
# Radar plot function
plot_one<-function(x,type='Yield',plot.legend = TRUE) {
a1 <- filter(x$out$b,variable==type)
a1[,2:(n.fleet+1)]<- a1[,2:(n.fleet+1)]/x$baseLine[,type]
a2<- a1
a2[,2:(n.fleet+1)]<- 1
a<-bind_rows(a1,a2)
tit<-ifelse(type=='Fbar'," F",paste0(' ',type))
a[1,'variable']<-tit
a[2,'variable']<-'Baseline'
gmax<-max( a[,2:(n.fleet+1)])
DTU.col<-c("#990000", # red
"#F6D04D") # yellow
ggradar(a,grid.max=gmax,grid.min=0,plot.title='',plot.legend=plot.legend,legend.position='top',
group.colours =DTU.col,legend.text.size = 18)
}
plot_radar_all<-function(res){
pF<-plot_one(res,type='Fbar',plot.legend = TRUE)
pYield<-plot_one(res,type='Yield')
pRec<-plot_one(res,type='Recruits')
pSSB<-plot_one(res,type='SSB')
plot_grid(pF, pYield, pRec,pSSB,
ncol = 2, nrow = 2)
}