1+ # Create data files
2+ # remotes:::install_github("nwfsc-cb/rCAX@*release")
3+ library(rCAX )
4+ library(tidyr )
5+ x <- rCAX ::: caxesu
6+ valid_esu <- which(! str_detect(x , " XN" ) & ! str_detect(x ," N/A" ))
7+ esa.salmon <- NULL
8+ for (i in valid_esu ){
9+ esuname <- rCAX ::: caxesu [i ]
10+ print(esuname )
11+ a <- rcax_hli(" NOSA" , type = " colnames" )
12+ tab <- rcax_hli(" NOSA" , flist = list (esu_dps = esuname ))
13+ # error no data
14+ if (! is.data.frame(tab )) next
15+ # find the pops with no data and remove
16+ tab <- tab %> %
17+ subset((datastatus == " Final" | datastatus == " Reviewed" ) & bestvalue == " Yes" )
18+ if (tab $ tsaej [1 ]== " " ) tab $ value <- tab $ tsaij else tab $ value <- tab $ tsaej
19+ aa <- tab %> %
20+ group_by(esapopname , run ) %> %
21+ summarize(n = sum(value != " " & value != " 0" & majorpopgroup != " " ))
22+ bad <- aa [which(aa $ n == 0 ),]
23+ aa <- tab %> %
24+ group_by(esapopname , run ) %> %
25+ summarize(n = any(duplicated(spawningyear )))
26+ df <- tab %> %
27+ subset(! (esapopname %in% bad $ esapopname & run %in% bad $ run )) %> %
28+ mutate(value = as.numeric(value ))
29+
30+ # get the min and max years in data
31+ years <- min(df $ spawningyear [! is.na(df $ value )]): max(df $ spawningyear [! is.na(df $ value )])
32+ # fill out the missing years with NAs
33+ df <- df %> %
34+ select(species , esu_dps , majorpopgroup , esapopname , commonpopname , spawningyear , value , run ) %> %
35+ group_by(species , esu_dps , majorpopgroup , esapopname , commonpopname , run ) %> %
36+ complete(spawningyear = years , fill = list (value = NA ))
37+
38+ # Deal with pops with multiple data
39+ if (any(aa $ n )){
40+ cat(aa $ esapopname [aa $ n ], " has duplicated years\n " )
41+ df <- df %> % ungroup() %> %
42+ group_by(species , esu_dps , majorpopgroup , esapopname , run , spawningyear ) %> %
43+ summarize(value = mean(value , na.rm = TRUE ,
44+ commonpopname = commonpopname [1 ]))
45+ }
46+ # if(i == 17 | i == 20) df$value_type <- "tsaij" else df$value_type <- "tsaej"
47+ esa.salmon <- bind_rows(esa.salmon , df )
48+ }
49+ esa.salmon <- esa.salmon %> % subset(species != " " ) %> % ungroup()
50+ save(esa.salmon , file = file.path(" Lab-2" , " Data_Images/esa-salmon.rda" ))
51+
52+ unique(esa.salmon $ esu_dps )
53+ df <- esa.salmon %> % subset(species == " Steelhead" & run == " Winter" )
54+ ggplot(df , aes(x = spawningyear , y = log(value ), color = majorpopgroup )) +
55+ geom_point(size = 0.2 ) +
56+ theme(strip.text.x = element_text(size = 2 )) +
57+ theme(axis.text.x = element_text(size = 5 , angle = 90 )) +
58+ facet_wrap(~ esapopname )
0 commit comments