##rm(list = ls())
library(tidyverse)
library(readxl)
library(data.table)
library(stringi)
library(lubridate)
library(xlsx)
library(ggplot2)
##another big old test
##gonna push this then change back to before
##this lets us look for case insensitive patterns!
`%likeci%` <- function (x, pattern) {
stringi::stri_detect_regex(x, pattern, case_insensitive=TRUE)
}
##set the working directory so the computer knows where to work out of
setwd("C:/Users/allan/OneDrive/New_Graph/Current/2019-005_FLNR_Duncan/")
list.files("data/deliverables")
excel_sheets("./data/deliverables/duncan_data_2016-2018.xlsx")
fish_all <- read_excel("./data/deliverables/duncan_data_2016-2018.xlsx",
sheet = "fish_data_gill_trawl", .name_repair = ~ make.names(.x, unique = TRUE))
##now we need to convert these columns to factors for plotting
cols.fac <- c("Year", "depth", "Age")
fish_all[cols.fac] <- lapply(fish_all[cols.fac],factor)
##-------------------now lets make some plots-----------------------------------------##
##lets create a file wiht just KO for plotting
fish_KO <- fish_all %>% filter(Species == "KO")
fish_non_KO <- fish_all %>% filter(Species != "KO")
names(fish_KO)
bin_1 <- floor(min(fish_KO$Fork_Length)/10)*10
bin_n <- ceiling(max(fish_KO$Fork_Length)/10)*10
bins <- seq(bin_1,bin_n, by = 10)
fish_hist_by_year <- ggplot(fish_KO, aes(x=Fork_Length, fill=Age,
color = Age)) +
geom_histogram(breaks = bins, alpha=0.5,
position="identity", size = 0.75)+
labs(x = "Fork Length (mm)", y = "Count (#)")+
facet_wrap(~Year)+
scale_color_grey() +
scale_fill_grey() +
theme_bw(base_size = 8)+
scale_x_continuous(breaks = bins[seq(1, length(bins), by = 2)])+
scale_color_manual(values=c("grey90", "grey60", "grey30", "grey0"))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
geom_histogram(aes(y=..density..), breaks = bins, alpha=0.5,
position="identity", size = 0.75)
fish_hist_by_year
ggsave(plot = fish_hist_by_year, filename = "./report/figures/fish_hist_by_year.png",
h=9.66, w=14.5, units="cm", dpi=300)
##lets try a density plot on top of the histogram
fish_hist_dens_all <- ggplot(fish_KO, aes(x=Fork_Length, fill = Age,
color = Age)) +
geom_histogram(aes(y=..density..), breaks = bins, alpha=0.8,
position="identity", size = 0.75)+
labs(x = "Fork Length (mm)", y = "Density")+
geom_density(alpha=.8)+
scale_fill_grey() +
scale_color_grey() +
scale_color_manual(values=c("grey90", "grey60", "grey30", "grey0"))+
theme_bw()
fish_hist_dens_all
ggsave(plot = fish_hist_dens_all, filename = "./report/figures/fish_hist_dens_all.png",
h=9.66, w=14.5, units="cm", dpi=300)
##lets try a density plot on its own
fish_dens_all <- ggplot(fish_KO, aes(x=Fork_Length, fill = Age,
color = Age)) +
labs(x = "Fork Length (mm)", y = "Density")+
geom_density(alpha=.8)+
scale_fill_grey() +
scale_color_grey() +
scale_color_manual(values=c("grey90", "grey60", "grey30", "grey0"))+
theme_bw()
fish_dens_all
##lets do a wrap by year for the density plots
fish_dens_by_year <- ggplot(fish_KO, aes(x=Fork_Length, fill = Age,
color = Age)) +
geom_histogram(aes(y=..density..), breaks = bins, alpha=0.5,
position="identity", size = 0.75)+
labs(x = "Fork Length (mm)", y = "Density")+
geom_density(alpha=.8)+
facet_wrap(~Year)+
scale_fill_grey() +
scale_color_grey() +
scale_color_manual(values=c("grey70", "grey50", "grey30", "grey0"))+
theme_bw()
fish_dens_by_year
ggsave(plot = fish_dens_by_year, filename = "./report/figures/fish_dens_by_year.png",
h=9.66, w=14.5, units="cm", dpi=300)
## data by depth for all the data
fish_hist_by_depth <- ggplot(filter(fish_KO, !is.na(depth)), aes(x=Fork_Length, fill=Age, color = Age)) +
geom_histogram(breaks = bins, alpha=0.5,
position="identity", size = 0.75)+
facet_wrap(~depth)+
scale_color_grey() +
scale_fill_grey() +
theme_bw(base_size = 7)+
scale_x_continuous(breaks = bins)+
scale_color_manual(values=c("grey70", "grey50", "grey30", "grey0"))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
fish_hist_by_depth
##data by depth
fish_hist_by_depth_year <- ggplot(filter(fish_KO, !is.na(depth)), aes(x=Fork_Length, fill=Age,
color = Age)) +
geom_histogram(breaks = bins, alpha=0.5,
position="identity", size = 0.75)+
facet_grid(depth ~ Year)+
scale_color_grey() +
scale_fill_grey() +
theme_bw(base_size = 7)+
scale_x_continuous(breaks = bins)+
scale_color_manual(values=c("grey70", "grey50", "grey30", "grey0"))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))
fish_hist_by_depth_year
##graph showing the length frequency for immature and maturing fish
##fix the inconistnecy in the labelling
unique(fish_KO$Maturity)
fish_KO <- fish_KO %>%
mutate(Maturity = case_when(Maturity == "Imm" ~ "Immature",
Maturity == "immature" ~ "Immature",
Maturity == "Mat" ~ "Maturing",
Maturity == "mat" ~ "Maturing",
TRUE ~ Maturity))
##use factor levls to organize fry first in graphsfish_size,
fish_KO$Maturity = factor(fish_KO$Maturity,
levels=c('Immature', 'Maturing'))
fish_hist_mature <- ggplot(filter(fish_KO, !is.na(Maturity)), aes(x=Fork_Length, fill=Maturity,
color = Maturity)) +
geom_histogram(breaks = bins, alpha=0.5,
position="identity", size = 0.75)+
labs(x = "Fork Length (mm)", y = "Count (#)")+
facet_wrap(~Year)+
scale_color_grey() +
scale_fill_grey() +
theme_bw(base_size = 8)+
scale_x_continuous(breaks = bins[seq(1, length(bins), by = 2)])+
scale_color_manual(values=c("grey90", "grey0"))+
theme(axis.text.x = element_text(angle = 45, hjust = 1))+
geom_histogram(aes(y=..density..), breaks = bins, alpha=0.5,
position="identity", size = 0.75)
fish_hist_mature
ggsave(plot = fish_hist_mature, filename = "./report/figures/fish_hist_mature.png",
h=9.66, w=14.5, units="cm", dpi=300)
##what percentage of age 2 ko were maturing --this doe
count(filter(fish_KO, Maturity == "Maturing" & Age == '2' & Year == "2018"))/count(filter(fish_KO, Year == "2018" & Age == '2'))
count(filter(fish_KO, Maturity == "Maturing" & Year == "2017" & Age == '2'))/count(filter(fish_KO, Year == "2017" & Age == '2'))
count(filter(fish_KO, Maturity == "Maturing" & Year == "2016" & Age == '2'))/count(filter(fish_KO, Year == "2016" & Age == '2'))
count(filter(fish_KO, Maturity == "Maturing" & Age == '3' & Year == "2018"))/count(filter(fish_KO, Year == "2018" & Age == '3'))
count(filter(fish_KO, Maturity == "Maturing" & Year == "2017" & Age == '3'))/count(filter(fish_KO, Year == "2017" & Age == '3'))
count(filter(fish_KO, Maturity == "Maturing" & Year == "2016" & Age == '3'))/count(filter(fish_KO, Year == "2016" & Age == '3'))
##report tables-------------------------------------------------------------------------------------------------
##here I want a summary table for the report
names(fish_all)
lapply(fish_all,class)
unique(fish_all$Age)
##want to convert class so easy to work with later
cols.fact <- c("Year","Species", "Age")
fish_all[cols.fact] <- lapply(fish_all[cols.fact],as.factor)
###length summary
fish_table_fl <- fish_all %>%
select(Year, Species, Fork_Length, Age) %>%
group_by(Year, Species,Age) %>%
summarize_if(is.numeric, funs(mean,sd,min,max,n())) %>%
mutate('Mean Length (mm)' = round(mean,0), 'SD (mm)' = round(sd,0),
'Range (mm)'= paste0(round(min,0),"-", round(max,0)), N = n) %>%
select(-mean, -sd, -min, -max, -n)
##weight summary
fish_table_wt <- fish_all %>%
select(Year, Species, Weight, Age) %>%
drop_na(Weight, Age) %>%
group_by(Year, Species,Age) %>%
summarize_if(is.numeric, funs(mean,sd)) %>%
mutate('Mean Weight (g)' = round(mean,1),
'SD (g)' = round(sd,0)) %>%
select(-mean, -sd)
names(fish_all)
##condition factor summary
fish_table_cf <- fish_all %>%
select(Year, Species, Condition_Factor, Age) %>%
drop_na(Condition_Factor, Age) %>%
group_by(Year, Species,Age) %>%
summarize_if(is.numeric, funs(mean,sd)) %>%
mutate('Condition Factor' = round(mean,2),
'SD' = round(sd,3)) %>%
select(-mean, -sd)
##weight and length and condition joined
fish_table_report <- left_join(fish_table_fl, fish_table_wt,
by = c("Year","Species","Age"))
fish_table_report <- left_join(fish_table_report, fish_table_cf,
by = c("Year","Species","Age"))
write.table(fish_table_report, file = "./report/tables/fish_table.txt", sep = ",", quote = FALSE, row.names = F)
##save a timestamped image of your workspace
save.image(file = paste0(getwd(),"/code/duncan_fish_plots",format(Sys.time(),"_%Y%m%d_%H%M.RData")))
##-boxplots-------------------------------------------------------------------
##filter out so it is just ko
ko <- fish_all %>%
filter(Species == "KO" & Condition_Factor != 0)
# Plot weight by group and length/condition by group
names(ko)
##need to remove rows with condition factor = 0
# New facet label names for supp variable
labs <- c("0+", "1+","2+","3+")
names(labs) <- c("0","1","2","3")
fish_wt_boxplot <- ggboxplot(data =ko, x = "Year", "Weight",
ylab = "Weight (g)")+
facet_wrap(~Age, scales ="free_y",
labeller = labeller(Age = labs))+
theme_bw()+
theme(legend.position = "none")
fish_wt_boxplot
ggsave(plot = fish_wt_boxplot, filename = "./report/figures/fish_wt_boxplot.png",
h=9.66, w=14.5, units="cm", dpi=300)
fish_fl_boxplot <- ggboxplot(data =ko, x = "Year", "Fork_Length",
ylab = "Fork Length (mm)")+
facet_wrap(~Age, scales ="free_y",
labeller = labeller(Age = labs))+
theme_bw()+
theme(legend.position = "none")
fish_fl_boxplot
ggsave(plot = fish_fl_boxplot, filename = "./report/figures/fish_fl_boxplot.png",
h=9.66, w=14.5, units="cm", dpi=300)
fish_cond_boxplot <- ggboxplot(data = ko, x = "Year", "Condition_Factor",
ylab = "Condition Factor")+
facet_wrap(~Age, scales ="free_y",
labeller = labeller(Age = labs))+
theme_bw()+
theme(legend.position = "none")
fish_cond_boxplot
ggsave(plot = fish_cond_boxplot, filename = "./report/figures/fish_cond_boxplot.png",
h=9.66, w=14.5, units="cm", dpi=300)
##this is good for visualizing the data. Jitterplot added with notches around medians
ggplot(ko, aes(x=Year, y=Weight)) +
geom_boxplot(notch=TRUE)+
facet_wrap(~Age, scales = "free_y",
labeller = labeller(Age = labs))+
geom_dotplot(binaxis='y', stackdir='center', dotsize=1)+
geom_jitter(shape=16, position=position_jitter(0.2))+
theme_bw()
ggplot(ko, aes(x=Year, y=Fork_Length)) +
geom_boxplot(notch=TRUE)+
facet_wrap(~Age, scales = "free_y",
labeller = labeller(Age = labs))+
geom_dotplot(binaxis='y', stackdir='center', dotsize=1)+
geom_jitter(shape=16, position=position_jitter(0.2))+
theme_bw()
ggplot(ko, aes(x=Year, y=Condition_Factor)) +
geom_boxplot(notch=TRUE)+
facet_wrap(~Age, scales = "free_y",
labeller = labeller(Age = labs))+
geom_dotplot(binaxis='y', stackdir='center', dotsize=1)+
geom_jitter(shape=16, position=position_jitter(0.2))+
theme_bw()
##left this example of 1 age plotted
ggboxplot(data =filter(ko, Age == 1), x = "Year", y = "Weight",
order = c("2016", "2017", "2018"),
ylab = "Weight (g)", xlab = "Year")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.