Mercurial > repos > greg > extract_ipm_date_interval
comparison utils.R @ 10:c2eb16ef23c0 draft
Uploaded
| author | greg |
|---|---|
| date | Tue, 07 Aug 2018 13:02:58 -0400 |
| parents | 0d9a74365270 |
| children |
comparison
equal
deleted
inserted
replaced
| 9:fd41f452c9fe | 10:c2eb16ef23c0 |
|---|---|
| 1 #!/usr/bin/env Rscript | 1 #!/usr/bin/env Rscript |
| 2 | 2 |
| 3 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) { | 3 get_file_path = function(life_stage, base_name, sub_life_stage=NULL) { |
| 4 if (!is.null(life_stage_nymph)) { | 4 if (is.null(sub_life_stage)) { |
| 5 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph); | |
| 6 file_name = paste(lsi, tolower(life_stage_nymph), base_name, sep="_"); | |
| 7 } else if (!is.null(life_stage_adult)) { | |
| 8 lsi = get_life_stage_index(life_stage, life_stage_adult=life_stage_adult); | |
| 9 file_name = paste(lsi, tolower(life_stage_adult), base_name, sep="_"); | |
| 10 } else { | |
| 11 lsi = get_life_stage_index(life_stage); | 5 lsi = get_life_stage_index(life_stage); |
| 12 file_name = paste(lsi, base_name, sep="_"); | 6 file_name = paste(lsi, base_name, sep="_"); |
| 7 } else { | |
| 8 lsi = get_life_stage_index(life_stage, sub_life_stage=sub_life_stage); | |
| 9 file_name = paste(lsi, tolower(sub_life_stage), base_name, sep="_"); | |
| 13 } | 10 } |
| 14 file_path = paste("output_plots_dir", file_name, sep="/"); | 11 file_path = paste("output_plots_dir", file_name, sep="/"); |
| 15 return(file_path); | 12 return(file_path); |
| 16 } | 13 } |
| 17 | 14 |
| 18 get_year_from_date = function(date_str) { | 15 get_year_from_date = function(date_str) { |
| 19 date_str_items = strsplit(date_str, "-")[[1]]; | 16 date_str_items = strsplit(date_str, "-")[[1]]; |
| 20 return (date_str_items[1]); | 17 return (date_str_items[1]); |
| 21 } | 18 } |
| 22 | 19 |
| 23 get_life_stage_index = function(life_stage, life_stage_nymph=NULL, life_stage_adult=NULL) { | 20 get_life_stage_index = function(life_stage, sub_life_stage=NULL) { |
| 24 # Name collection elements so that they | 21 # Name collection elements so that they |
| 25 # are displayed in logical order. | 22 # are displayed in logical order. |
| 26 if (life_stage=="Egg") { | 23 if (life_stage=="Egg") { |
| 27 lsi = "01"; | 24 lsi = "01"; |
| 28 } else if (life_stage=="Nymph") { | 25 } else if (life_stage=="Nymph") { |
| 29 if (life_stage_nymph=="Young") { | 26 if (sub_life_stage=="Young") { |
| 30 lsi = "02"; | 27 lsi = "02"; |
| 31 } else if (life_stage_nymph=="Old") { | 28 } else if (sub_life_stage=="Old") { |
| 32 lsi = "03"; | 29 lsi = "03"; |
| 33 } else if (life_stage_nymph=="Total") { | 30 } else if (sub_life_stage=="Total") { |
| 34 lsi="04"; | 31 lsi="04"; |
| 35 } | 32 } |
| 36 } else if (life_stage=="Adult") { | 33 } else if (life_stage=="Adult") { |
| 37 if (life_stage_adult=="Pre-vittelogenic") { | 34 if (sub_life_stage=="Pre-vittelogenic") { |
| 38 lsi = "05"; | 35 lsi = "05"; |
| 39 } else if (life_stage_adult=="Vittelogenic") { | 36 } else if (sub_life_stage=="Vittelogenic") { |
| 40 lsi = "06"; | 37 lsi = "06"; |
| 41 } else if (life_stage_adult=="Diapausing") { | 38 } else if (sub_life_stage=="Diapausing") { |
| 42 lsi = "07"; | 39 lsi = "07"; |
| 43 } else if (life_stage_adult=="Total") { | 40 } else if (sub_life_stage=="Total") { |
| 44 lsi = "08"; | 41 lsi = "08"; |
| 45 } | 42 } |
| 46 } else if (life_stage=="Total") { | 43 } else if (life_stage=="Total") { |
| 47 lsi = "09"; | 44 lsi = "09"; |
| 48 } | 45 } |
| 221 return(list(ticks, tick_labels)); | 218 return(list(ticks, tick_labels)); |
| 222 } | 219 } |
| 223 | 220 |
| 224 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval, | 221 render_chart = function(ticks, date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval, |
| 225 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL, | 222 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL, |
| 226 life_stages_adult=NULL, life_stages_nymph=NULL) { | 223 sub_life_stage=NULL) { |
| 227 if (chart_type=="pop_size_by_life_stage") { | 224 if (chart_type=="pop_size_by_life_stage") { |
| 228 if (life_stage=="Total") { | 225 if (life_stage=="Total") { |
| 229 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); | 226 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); |
| 230 legend_text = c("Egg", "Nymph", "Adult"); | 227 legend_text = c("Egg", "Nymph", "Adult"); |
| 231 columns = c(4, 2, 1); | 228 columns = c(4, 2, 1); |
| 250 if (life_stage=="Egg") { | 247 if (life_stage=="Egg") { |
| 251 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); | 248 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); |
| 252 legend_text = c(life_stage); | 249 legend_text = c(life_stage); |
| 253 columns = c(4); | 250 columns = c(4); |
| 254 } else if (life_stage=="Nymph") { | 251 } else if (life_stage=="Nymph") { |
| 255 stage = paste(life_stages_nymph, "Nymph Pop :", sep=" "); | 252 stage = paste(sub_life_stage, "Nymph Pop :", sep=" "); |
| 256 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); | 253 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); |
| 257 legend_text = c(paste(life_stages_nymph, life_stage, sep=" ")); | 254 legend_text = c(paste(sub_life_stage, life_stage, sep=" ")); |
| 258 columns = c(2); | 255 columns = c(2); |
| 259 } else if (life_stage=="Adult") { | 256 } else if (life_stage=="Adult") { |
| 260 stage = paste(life_stages_adult, "Adult Pop", sep=" "); | 257 stage = paste(sub_life_stage, "Adult Pop", sep=" "); |
| 261 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); | 258 title = paste(insect, ": Reps", replications, ":", stage, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); |
| 262 legend_text = c(paste(life_stages_adult, life_stage, sep=" ")); | 259 legend_text = c(paste(sub_life_stage, life_stage, sep=" ")); |
| 263 columns = c(1); | 260 columns = c(1); |
| 264 } | 261 } |
| 265 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3); | 262 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3); |
| 266 legend("topleft", legend_text, lty=c(1), col="black", cex=3); | 263 legend("topleft", legend_text, lty=c(1), col="black", cex=3); |
| 267 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3); | 264 axis(side=1, at=ticks, labels=date_labels, las=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3); |
| 276 if (life_stage=="Total") { | 273 if (life_stage=="Total") { |
| 277 title_str = ": Total Pop by Gen :"; | 274 title_str = ": Total Pop by Gen :"; |
| 278 } else if (life_stage=="Egg") { | 275 } else if (life_stage=="Egg") { |
| 279 title_str = ": Egg Pop by Gen :"; | 276 title_str = ": Egg Pop by Gen :"; |
| 280 } else if (life_stage=="Nymph") { | 277 } else if (life_stage=="Nymph") { |
| 281 title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" "); | 278 title_str = paste(":", sub_life_stage, "Nymph Pop by Gen", ":", sep=" "); |
| 282 } else if (life_stage=="Adult") { | 279 } else if (life_stage=="Adult") { |
| 283 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" "); | 280 title_str = paste(":", sub_life_stage, "Adult Pop by Gen", ":", sep=" "); |
| 284 } | 281 } |
| 285 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); | 282 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); |
| 286 legend_text = c("P", "F1", "F2"); | 283 legend_text = c("P", "F1", "F2"); |
| 287 columns = c(1, 2, 4); | 284 columns = c(1, 2, 4); |
| 288 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3); | 285 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=FALSE, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3); |
