Output figures comparing M2 by predator (should match Morten’s outputs)
M2_2017 <- read_csv(here("KeyRunComparisons" , "NorthSeaSMS2017", "who_eats_whom_level1.csv"))
M2_2020 <- read_csv(here("KeyRunComparisons" , "NorthSeaSMS2020", "who_eats_whom_level1.csv"))
M2_2020_old <- read_csv(here("KeyRunComparisons" , "NorthSeaSMS2020", "who_eats_whom_level1_old.csv"))
M2_2017 <- add_column(M2_2017, KeyRun = "SMS2017")
M2_2020 <- add_column(M2_2020, KeyRun = "SMS2020")
M2_2020_old <- add_column(M2_2020_old, KeyRun = "SMS2020_old")
M2_comp <- bind_rows(M2_2017, M2_2020)
M2_comp <- bind_rows(M2_comp, M2_2020_old)
# aggregate birds
M2_comp <- M2_comp %>%
mutate(Predator = as.character(Predator),
Predator = ifelse(Predator %in% c("Fulmar","Gannet","GBB. Gull","Guillemot","Her. Gull","Kittiwake","Puffin","Razorbill"), "Birds", Predator)) %>%
group_by(Year,Quarter,Predator, Predator.age,Prey, Prey.age, KeyRun) %>%
mutate(M2 = sum(Part.M2, na.rm=T)) %>%
ungroup()
# length(unique(dat3$Predator)) #http://medialab.github.io/iwanthue/
col <- c("#000047","#858a00","#ff2b47","#00d3c9", "#0188d2", "#7426d6","#e37b00","#ffa0ee","#930025","#00bd3b","yellow","black","#005144")
names(col) <- c("A. radiata", "Birds", "Cod", "G. gurnards", "Grey seal", "H. porpoise", "Haddock", "Hake", "Mackerel", "Saithe", "Whiting", "N.horse mac", "W.horse mac")
preycol <- c("#da62e7", "#549700", "#00609a", "#8dd971", "#535622")
names(preycol) <- as.factor(c("N. sandeel", "Nor. pout", "S. sandeel", "Herring", "Sprat"))
col <- c(col, preycol)
M2_2017 <- M2_comp %>% filter(KeyRun %in% "SMS2017")
M2_2020 <- M2_comp %>% filter(KeyRun %in% "SMS2020")
plist = lapply(split(M2_2017, M2_2017$Prey), function(d) {
ggplot(d, aes(Year, Part.M2, fill=Predator)) +
geom_bar(stat = "identity") +
facet_wrap(Prey~Prey.age) +
xlab("Year") +
ylab("M2") +
scale_fill_manual(values=col) +
theme_tufte() +
theme(legend.position="bottom")
})
plist$Cod
plist$Haddock
plist$Herring
plist$'N. sandeel'
plist$'S. sandeel'
plist$'Nor. pout'
plist$Sprat
plist$Whiting
plist2 = lapply(split(M2_2020, M2_2020$Prey), function(d) {
ggplot(d, aes(Year, Part.M2, fill=Predator)) +
geom_bar(stat = "identity") +
facet_wrap(Prey~Prey.age) +
xlab("Year") +
ylab("M2") +
scale_fill_manual(values=col) +
theme_tufte() +
theme(legend.position="bottom")
})
plist2$Cod
plist2$Haddock
plist2$Herring
plist2$'N. sandeel'
plist2$'S. sandeel'
plist2$'Nor. pout'
plist2$Sprat
plist2$Whiting
plist3 = lapply(split(M2_2017, M2_2017$Prey), function(d) {
ggplot(d, aes(Year, Part.M2, fill=Predator)) +
geom_bar(stat = "identity") +
facet_wrap(Prey~Prey.age, scales = "free_y") +
xlab("Year") +
ylab("M2") +
scale_fill_manual(values=col) +
theme_tufte() +
theme(legend.position="bottom")
})
plist3$Cod
plist3$Haddock
plist3$Herring
plist3$'N. sandeel'
plist3$'S. sandeel'
plist3$'Nor. pout'
plist3$Sprat
plist3$Whiting
plist4 = lapply(split(M2_2020, M2_2020$Prey), function(d) {
ggplot(d, aes(Year, Part.M2, fill=Predator)) +
geom_bar(stat = "identity") +
facet_wrap(Prey~Prey.age, scales = "free_y") +
xlab("Year") +
ylab("M2") +
scale_fill_manual(values=col) +
theme_tufte() +
theme(legend.position="bottom")
})
plist4$Cod
plist4$Haddock
plist4$Herring
plist4$'N. sandeel'
plist4$'S. sandeel'
plist4$'Nor. pout'
plist4$Sprat
plist4$Whiting
# want all the birds separated out now
M2_2017 <- read_csv(here("KeyRunComparisons" , "NorthSeaSMS2017", "who_eats_whom_level1.csv"))
M2_2020 <- read_csv(here("KeyRunComparisons" , "NorthSeaSMS2020", "who_eats_whom_level1.csv"))
M2_2020_old <- read_csv(here("KeyRunComparisons" , "NorthSeaSMS2020", "who_eats_whom_level1_old.csv"))
M2_2017 <- add_column(M2_2017, KeyRun = "SMS2017")
M2_2020 <- add_column(M2_2020, KeyRun = "SMS2020")
M2_2020_old <- add_column(M2_2020_old, KeyRun = "SMS2020_old")
M2_comp <- bind_rows(M2_2017, M2_2020)
M2_comp <- bind_rows(M2_comp, M2_2020_old)
M2_compHerring <- M2_comp %>%
filter(Prey=="Herring") %>%
group_by(Year, Predator, Prey, Prey.age, KeyRun) %>%
summarise_at(vars(eatenW, Part.M2), funs(sum))
plist5 = lapply(split(M2_compHerring, M2_compHerring$Predator), function(d) {
ggplot(d, aes(Year, Part.M2, group=KeyRun)) +
geom_point(aes(colour=KeyRun)) +
geom_line()+
facet_wrap(Prey~Prey.age, scales = "free_y", ncol = 2) +
xlab("Year") +
ylab("M2") +
theme_tufte() +
theme(legend.position="bottom")
})
plist5$Cod
plist5$Fulmar
plist5$'G. gurnards'
plist5$Gannet
plist5$'GBB. Gull'
plist5$'Grey seal'
plist5$Guillemot
plist5$'H. porpoise'
plist5$Hake
plist5$'Her. Gull'
plist5$Kittiwake
plist5$Mackerel
plist5$'N.horse mac'
plist5$Puffin
plist5$Razorbill
plist5$Saithe
plist5$Whiting
M2_compWhiting <- M2_comp %>%
filter(Prey=="Whiting") %>%
group_by(Year, Predator, Prey, Prey.age, KeyRun) %>%
summarise_at(vars(eatenW, Part.M2), funs(sum))
plist6 = lapply(split(M2_compWhiting, M2_compWhiting$Predator), function(d) {
ggplot(d, aes(Year, Part.M2, group=KeyRun)) +
geom_point(aes(colour=KeyRun)) +
geom_line()+
facet_wrap(Prey~Prey.age, scales = "free_y", ncol = 2) +
xlab("Year") +
ylab("M2") +
theme_tufte() +
theme(legend.position="bottom")
})
preds <- unique(M2_compWhiting$Predator)
for(i in 1:length(preds)) {
cat(" \n####", preds[i]," \n")
print(plist6[preds[i]])
cat(" \n")
}
$A. radiata
$Cod
$Fulmar
$G. gurnards
$GBB. Gull
$Grey seal
$Guillemot
$H. porpoise
$Her. Gull
$Kittiwake
$N.horse mac
$Razorbill
$Saithe
$Whiting
M2_compSprat <- M2_comp %>%
filter(Prey=="Sprat") %>%
group_by(Year, Predator, Prey, Prey.age, KeyRun) %>%
summarise_at(vars(eatenW, Part.M2), funs(sum))
plist7 = lapply(split(M2_compSprat, M2_compSprat$Predator), function(d) {
ggplot(d, aes(Year, Part.M2, group=KeyRun)) +
geom_point(aes(colour=KeyRun)) +
geom_line()+
facet_wrap(Prey~Prey.age, scales = "free_y", ncol = 2) +
xlab("Year") +
ylab("M2") +
theme_tufte() +
theme(legend.position="bottom")
})
preds <- unique(M2_compSprat$Predator)
for(i in 1:length(preds)) {
cat(" \n####", preds[i]," \n")
print(plist7[preds[i]])
cat(" \n")
}
$Cod
$Fulmar
$G. gurnards
$Gannet
$GBB. Gull
$Guillemot
$Her. Gull
$Kittiwake
$Mackerel
$N.horse mac
$Puffin
$Razorbill
$Whiting
M2_diffHerring <- M2_compHerring %>%
arrange(Year, Predator, Prey, Prey.age, KeyRun) %>%
group_by(Year, Predator, Prey, Prey.age) %>%
mutate(diffM2_2020 = c(diff(Part.M2), NA),
diffeatenW_2020 = c(diff(eatenW), NA)) %>%
filter(!is.na(diffM2_2020))
age2diff <- M2_diffHerring %>%
filter(Prey.age==2) %>%
arrange(diffM2_2020)
library(DT)
datatable(age2diff, rownames = FALSE, options = list(pageLength = 25))