-
Notifications
You must be signed in to change notification settings - Fork 0
/
figureYY.R
201 lines (172 loc) · 5.33 KB
/
figureYY.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
#-
#-
#- Fisheries advice example for the Celtic Seas
#-
#- Figure yy. Distribution of effort deployed by EU nations by gear type within the Celtic Seas ecoregion, 2003-2013. Data source: STECF database.
#-
#-
#------------> Filepaths <----------
dataPath <- "~/r/advice/data/"
outPath <- "~/r/advice/output/"
codePath <- "~/r/advice/code/"
stecfPath <- "~/r/rdata/"
pathList <- as.list(c(dataPath, outPath, codePath))
for (p in pathList) {
if (!dir.exists(p)) {
dir.create(p, recursive = TRUE)
}
}
#- Check if STECF data is downloaded, if not grabs data from STECF data repo (Warning: large file -- may take a while to complete download)
source("~/r/FisheriesAdvice/stecfDataGrabber.R")
#------------||
# LOAD NEEDED LIBS+PACKAGAGES
library(scales)
library(plyr)
library(reshape2)
library(foreach)
library(RColorBrewer)
library(extrafont)
# library(data.table)
#--- simple data load -- should be made fancier when time permits (i.e. after deadline of advice example 2015)
fao27 <-
read.delim(
"~/r/rdata/data/stecf-economic-fleet.data", header = TRUE, fill = TRUE, stringsAsFactors = FALSE, dec = "."
)
#
fishtech.list <-
read.csv(
"~/r/FisheriesAdvice/keys-lists/stecf-fishingtech.list", header = TRUE, stringsAsFactors = FALSE
)
gear.list <-
read.csv(
"~/r/FisheriesAdvice/keys-lists/stecf-gear.list", header = TRUE, stringsAsFactors = FALSE
)
#
# reg.list <-
# read.delim(
# "~/r/ices/keys-lists/ices.regions.list", header = TRUE, sep = ",", stringsAsFactors = FALSE, fileEncoding = "UTF-8"
# )
#
# guild.list <-
# read.delim(
# "~/r/ices/keys-lists/guild.list", header = TRUE, sep = ",", stringsAsFactors = FALSE, fileEncoding = "UTF-8"
# )
#
# species.list <-
# read.csv(
# "~/r/ices/keys-lists/species.list", header = TRUE, stringsAsFactors = FALSE
# )
#
#
#
# # append subregion key:
#
# reg.apply <- c(reg.list$eco.reg.shrt)
# names(reg.apply) <- c(reg.list$ices.code)
# ices27.df$ecoregion <- reg.apply[ices27.df$sub_reg]
#
# sciname.apply <- c(species.list$scientific_name)
# names(sciname.apply) <- c(species.list$species)
# ices27.df$sci.name <- sciname.apply[ices27.df$species_code]
#
# guild.apply <- c(guild.list$feeding.guild)
# names(guild.apply) <- c(guild.list$scientific.name)
# ices27.df$feeding.guild <- guild.apply[ices27.df$sci.name]
#- Append indicator of active/passive gears:
activegear.apply <- c(fishtech.list$towed_static)
names(activegear.apply) <- c(fishtech.list$fishing_tech)
fao27$towed_static <- activegear.apply[fao27$fishing_tech]
geartype.apply <- c(gear.list$type)
names(geartype.apply) <- c(gear.list$code)
fao27$gear_group <- geartype.apply[fao27$gear_type]
ecoArea <- c("27.7.a",
"27.7.b",
"27.7.c",
"27.7.e",
"27.7.f",
"27.7.g",
"27.7.j",
"27.7.k",
"27.6.a",
"27.6.b")
#- select sub areas in CEL-region:
dataArea <- fao27[fao27$sub_reg %in% ecoArea,]
#- select only active gears
dataArea <- dataArea[dataArea$towed_static == "Towed",]
dataEffort <- subset.data.frame(
dataArea, subset = c(variable_name == "kWfishing days"), select = c("country_name", "year","variable_name","value", "gear_group")
)
dataEffort$value <- as.numeric(dataEffort$value)
#--- End of subsetting, file check
#--get effort, kWdays
# Aggregate by gear, year and country
p.dataCountry <- ddply(dataEffort, .(country_name, year, gear_group),
summarize,
"kw_fishingdays" = sum(value))
#- convert to wide format, and sort individually small => large per column.
dataCountry <- dcast(p.dataCountry, year ~ gear_group, value.var = "kw_fishingdays", fun.aggregate = sum)
row.names(dataCountry) <- unique(dataCountry$year)
dataCountry <- dataCountry[,-1]
# orderCountry <- names(sort(apply(dataCountry, 2, sd)))
orderCountry <- names(sort(colSums(dataCountry)))
dataCountry <- dataCountry[,(orderCountry)]
# this is somehow not working -- whyyyy
# if (ncol(dataCountry > 10) {
# dataOthers <-
# data.frame(dataCountry[,9:1], "Other" = rowSums(dataCountry[,10:ncol(dataCountry)]))
# } else {
# (dataOthers <- dataCountry)
# }
#
dataOthers <- dataCountry
# Stacked line graph
colList <- c(brewer.pal(n = ncol(dataOthers) - 1, name = 'Spectral'), "grey40")
ylim = c(0, max(rowSums(dataOthers, na.rm = T)))
#
png(filename = "~/figYYtest7.png",
width = 172.4,
height = 172.4,
units = "mm",
res = 600)
#
par(xpd = T,
mar=c(2.15, 2.25, 0.45, 3.5),
oma = c(0, 0, 0, 0),
mgp = c(1.25, 0.35, 0),
tck = -0.01,
family = "Calibri")
#
xval = as.numeric(row.names(dataOthers))
summary = rep(0, nrow(dataOthers))
recent = summary
#
# Create empty plot
plot(c(-100),
c(-100),
xlim = range(xval, na.rm = T),
ylim = ylim,
ylab = "Effort (kW Fishingdays)",
xlab = "Year")
# One polygon per column
cols = names(dataOthers)
for(c in 1:length(cols)) {
current = dataOthers[[cols[[c]]]]
summary = summary + current
polygon(
x = c(xval, rev(xval)),
y = c(summary, rev(recent)),
col = colList[[c]],
border = "grey90"
)
recent = summary
}
#
legend(x = 2013.5, y = max(ylim),
rev(cols),
col = rev(colList),
cex = 0.8,
pch = 15,
xpd = TRUE,
bty = "n")
#
dev.off()