0
|
1 #!/usr/bin/env Rscript
|
|
2
|
|
3 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) {
|
|
4 if (!is.null(life_stage_nymph)) {
|
|
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);
|
|
12 file_name = paste(lsi, base_name, sep="_");
|
|
13 }
|
|
14 file_path = paste("output_plots_dir", file_name, sep="/");
|
|
15 return(file_path);
|
|
16 }
|
|
17
|
2
|
18 get_year_from_date = function(date_str) {
|
|
19 date_str_items = strsplit(date_str, "-")[[1]];
|
|
20 return (date_str_items[1]);
|
|
21 }
|
|
22
|
0
|
23 get_life_stage_index = function(life_stage, life_stage_nymph=NULL, life_stage_adult=NULL) {
|
|
24 # Name collection elements so that they
|
|
25 # are displayed in logical order.
|
|
26 if (life_stage=="Egg") {
|
|
27 lsi = "01";
|
|
28 } else if (life_stage=="Nymph") {
|
|
29 if (life_stage_nymph=="Young") {
|
|
30 lsi = "02";
|
|
31 } else if (life_stage_nymph=="Old") {
|
|
32 lsi = "03";
|
|
33 } else if (life_stage_nymph=="Total") {
|
|
34 lsi="04";
|
|
35 }
|
|
36 } else if (life_stage=="Adult") {
|
|
37 if (life_stage_adult=="Pre-vittelogenic") {
|
|
38 lsi = "05";
|
|
39 } else if (life_stage_adult=="Vittelogenic") {
|
|
40 lsi = "06";
|
|
41 } else if (life_stage_adult=="Diapausing") {
|
|
42 lsi = "07";
|
|
43 } else if (life_stage_adult=="Total") {
|
|
44 lsi = "08";
|
|
45 }
|
|
46 } else if (life_stage=="Total") {
|
|
47 lsi = "09";
|
|
48 }
|
|
49 return(lsi);
|
|
50 }
|
|
51
|
|
52 get_mean_and_std_error = function(p_replications, f1_replications, f2_replications) {
|
|
53 # P mean.
|
|
54 p_m = apply(p_replications, 1, mean);
|
|
55 # P standard error.
|
|
56 p_se = apply(p_replications, 1, sd) / sqrt(opt$replications);
|
|
57 # F1 mean.
|
|
58 f1_m = apply(f1_replications, 1, mean);
|
|
59 # F1 standard error.
|
|
60 f1_se = apply(f1_replications, 1, sd) / sqrt(opt$replications);
|
|
61 # F2 mean.
|
|
62 f2_m = apply(f2_replications, 1, mean);
|
|
63 # F2 standard error.
|
|
64 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications);
|
|
65 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se))
|
|
66 }
|
|
67
|
|
68 get_tick_index = function(index, last_tick, ticks, tick_labels, tick_sep) {
|
|
69 # The R code tries hard not to draw overlapping tick labels, and so
|
|
70 # will omit labels where they would abut or overlap previously drawn
|
|
71 # labels. This can result in, for example, every other tick being
|
|
72 # labelled. We'll keep track of the last tick to make sure all of
|
|
73 # the month labels are displayed, and missing ticks are restricted
|
|
74 # to Sundays which have no labels anyway.
|
|
75 if (last_tick==0) {
|
|
76 return(length(ticks)+1);
|
|
77 }
|
|
78 last_saved_tick = ticks[[length(ticks)]];
|
|
79 if (index-last_saved_tick<tick_sep) {
|
|
80 last_saved_month = tick_labels[[length(tick_labels)]];
|
|
81 if (last_saved_month=="") {
|
|
82 # We're safe overwriting a tick
|
|
83 # with no label (i.e., a Sunday tick).
|
|
84 return(length(ticks));
|
|
85 } else {
|
|
86 # Don't eliminate a Month label.
|
|
87 return(NULL);
|
|
88 }
|
|
89 }
|
|
90 return(length(ticks)+1);
|
|
91 }
|
|
92
|
|
93 get_total_days = function(is_leap_year) {
|
|
94 # Get the total number of days in the current year.
|
|
95 if (is_leap_year) {
|
|
96 return(366);
|
|
97 } else {
|
|
98 return(365);
|
|
99 }
|
|
100 }
|
|
101
|
6
|
102 get_x_axis_ticks_and_labels = function(temperature_data_frame, prepend_end_doy_norm=0, append_start_doy_norm=0, date_interval=FALSE) {
|
0
|
103 # Generate a list of ticks and labels for plotting the x axis.
|
6
|
104 if (prepend_end_doy_norm > 0) {
|
0
|
105 prepend_end_norm_row = which(temperature_data_frame$DOY==prepend_end_doy_norm);
|
|
106 } else {
|
|
107 prepend_end_norm_row = 0;
|
|
108 }
|
6
|
109 if (append_start_doy_norm > 0) {
|
0
|
110 append_start_norm_row = which(temperature_data_frame$DOY==append_start_doy_norm);
|
|
111 } else {
|
|
112 append_start_norm_row = 0;
|
|
113 }
|
|
114 num_rows = dim(temperature_data_frame)[1];
|
|
115 tick_labels = list();
|
|
116 ticks = list();
|
|
117 current_month_label = NULL;
|
|
118 last_tick = 0;
|
|
119 if (date_interval) {
|
|
120 tick_sep = 0;
|
|
121 } else {
|
|
122 tick_sep = 3;
|
|
123 }
|
|
124 for (i in 1:num_rows) {
|
|
125 # Get the year and month from the date which
|
|
126 # has the format YYYY-MM-DD.
|
|
127 date = format(temperature_data_frame$DATE[i]);
|
|
128 # Get the month label.
|
|
129 items = strsplit(date, "-")[[1]];
|
|
130 month = items[2];
|
|
131 month_label = month.abb[as.integer(month)];
|
|
132 day = as.integer(items[3]);
|
|
133 doy = as.integer(temperature_data_frame$DOY[i]);
|
|
134 # We're plotting the entire year, so ticks will
|
|
135 # occur on Sundays and the first of each month.
|
|
136 if (i == prepend_end_norm_row) {
|
|
137 # Add a tick for the end of the 30 year normnals data
|
|
138 # that was prepended to the year-to-date data.
|
|
139 label_str = "End prepended 30 year normals";
|
|
140 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
|
|
141 ticks[tick_index] = i;
|
|
142 if (date_interval) {
|
|
143 # Append the day to label_str
|
|
144 tick_labels[tick_index] = paste(label_str, day, sep=" ");
|
|
145 } else {
|
|
146 tick_labels[tick_index] = label_str;
|
|
147 }
|
|
148 last_tick = i;
|
|
149 } else if (doy == append_start_doy_norm) {
|
|
150 # Add a tick for the start of the 30 year normnals data
|
|
151 # that was appended to the year-to-date data.
|
|
152 label_str = "Start appended 30 year normals";
|
|
153 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
|
|
154 ticks[tick_index] = i;
|
|
155 if (!identical(current_month_label, month_label)) {
|
|
156 # Append the month to label_str.
|
|
157 label_str = paste(label_str, month_label, spe=" ");
|
|
158 current_month_label = month_label;
|
|
159 }
|
|
160 if (date_interval) {
|
|
161 # Append the day to label_str
|
|
162 label_str = paste(label_str, day, sep=" ");
|
|
163 }
|
|
164 tick_labels[tick_index] = label_str;
|
|
165 last_tick = i;
|
|
166 } else if (i==num_rows) {
|
|
167 # Add a tick for the last day of the year.
|
|
168 label_str = "";
|
|
169 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
|
|
170 ticks[tick_index] = i;
|
|
171 if (!identical(current_month_label, month_label)) {
|
|
172 # Append the month to label_str.
|
|
173 label_str = month_label;
|
|
174 current_month_label = month_label;
|
|
175 }
|
|
176 if (date_interval) {
|
|
177 # Append the day to label_str
|
|
178 label_str = paste(label_str, day, sep=" ");
|
|
179 }
|
|
180 tick_labels[tick_index] = label_str;
|
|
181 } else {
|
|
182 if (!identical(current_month_label, month_label)) {
|
|
183 # Add a tick for the month.
|
|
184 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
|
|
185 ticks[tick_index] = i;
|
|
186 if (date_interval) {
|
|
187 # Append the day to the month.
|
|
188 tick_labels[tick_index] = paste(month_label, day, sep=" ");
|
|
189 } else {
|
|
190 tick_labels[tick_index] = month_label;
|
|
191 }
|
|
192 current_month_label = month_label;
|
|
193 last_tick = i;
|
|
194 }
|
|
195 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
|
|
196 if (!is.null(tick_index)) {
|
|
197 if (date_interval) {
|
|
198 # Add a tick for every day. The first tick is the
|
|
199 # month label, so add a tick only if i is not 1
|
|
200 if (i>1 & day>1) {
|
|
201 tick_index = get_tick_index(i, last_tick, ticks, tick_labels, tick_sep)
|
|
202 ticks[tick_index] = i;
|
|
203 # Add the day as the label.
|
|
204 tick_labels[tick_index] = day;
|
|
205 last_tick = i;
|
|
206 }
|
|
207 } else {
|
|
208 # Get the day.
|
|
209 day = weekdays(as.Date(date));
|
|
210 if (day=="Sunday") {
|
|
211 # Add a tick if we're on a Sunday.
|
|
212 ticks[tick_index] = i;
|
|
213 # Add a blank month label so it is not displayed.
|
|
214 tick_labels[tick_index] = "";
|
|
215 last_tick = i;
|
|
216 }
|
|
217 }
|
|
218 }
|
|
219 }
|
|
220 }
|
|
221 return(list(ticks, tick_labels));
|
|
222 }
|
|
223
|
|
224 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,
|
|
226 life_stages_adult=NULL, life_stages_nymph=NULL) {
|
|
227 if (chart_type=="pop_size_by_life_stage") {
|
|
228 if (life_stage=="Total") {
|
|
229 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
230 legend_text = c("Egg", "Nymph", "Adult");
|
|
231 columns = c(4, 2, 1);
|
|
232 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);
|
|
233 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
|
|
234 lines(days, group2, lwd=2, lty=1, col=2);
|
|
235 lines(days, group3, lwd=2, lty=1, col=4);
|
|
236 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);
|
|
237 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
|
|
238 if (plot_std_error=="yes") {
|
|
239 # Standard error for group.
|
|
240 lines(days, group+group_std_error, lty=2);
|
|
241 lines(days, group-group_std_error, lty=2);
|
|
242 # Standard error for group2.
|
|
243 lines(days, group2+group2_std_error, col=2, lty=2);
|
|
244 lines(days, group2-group2_std_error, col=2, lty=2);
|
|
245 # Standard error for group3.
|
|
246 lines(days, group3+group3_std_error, col=4, lty=2);
|
|
247 lines(days, group3-group3_std_error, col=4, lty=2);
|
|
248 }
|
|
249 } else {
|
|
250 if (life_stage=="Egg") {
|
|
251 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
252 legend_text = c(life_stage);
|
|
253 columns = c(4);
|
|
254 } else if (life_stage=="Nymph") {
|
|
255 stage = paste(life_stages_nymph, "Nymph Pop :", sep=" ");
|
|
256 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=" "));
|
|
258 columns = c(2);
|
|
259 } else if (life_stage=="Adult") {
|
|
260 stage = paste(life_stages_adult, "Adult Pop", sep=" ");
|
|
261 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=" "));
|
|
263 columns = c(1);
|
|
264 }
|
|
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);
|
|
266 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);
|
|
268 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
|
|
269 if (plot_std_error=="yes") {
|
|
270 # Standard error for group.
|
|
271 lines(days, group+group_std_error, lty=2);
|
|
272 lines(days, group-group_std_error, lty=2);
|
|
273 }
|
|
274 }
|
|
275 } else if (chart_type=="pop_size_by_generation") {
|
|
276 if (life_stage=="Total") {
|
|
277 title_str = ": Total Pop by Gen :";
|
|
278 } else if (life_stage=="Egg") {
|
|
279 title_str = ": Egg Pop by Gen :";
|
|
280 } else if (life_stage=="Nymph") {
|
|
281 title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" ");
|
|
282 } else if (life_stage=="Adult") {
|
|
283 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" ");
|
|
284 }
|
|
285 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" ");
|
|
286 legend_text = c("P", "F1", "F2");
|
|
287 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);
|
|
289 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3);
|
|
290 lines(days, group2, lwd=2, lty=1, col=2);
|
|
291 lines(days, group3, lwd=2, lty=1, col=4);
|
|
292 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);
|
|
293 axis(side=2, font.axis=3, xpd=TRUE, cex=3, cex.lab=3, cex.axis=3, cex.main=3);
|
|
294 if (plot_std_error=="yes") {
|
|
295 # Standard error for group.
|
|
296 lines(days, group+group_std_error, lty=2);
|
|
297 lines(days, group-group_std_error, lty=2);
|
|
298 # Standard error for group2.
|
|
299 lines(days, group2+group2_std_error, col=2, lty=2);
|
|
300 lines(days, group2-group2_std_error, col=2, lty=2);
|
|
301 # Standard error for group3.
|
|
302 lines(days, group3+group3_std_error, col=4, lty=2);
|
|
303 lines(days, group3-group3_std_error, col=4, lty=2);
|
|
304 }
|
|
305 }
|
|
306 }
|
|
307
|
|
308 stop_err = function(msg) {
|
|
309 cat(msg, file=stderr());
|
|
310 quit(save="no", status=1);
|
|
311 }
|
|
312
|
|
313 validate_date = function(date_str) {
|
|
314 valid_date = as.Date(date_str, format="%Y-%m-%d");
|
|
315 if( class(valid_date)=="try-error" || is.na(valid_date)) {
|
|
316 msg = paste("Invalid date: ", date_str, ", valid date format is yyyy-mm-dd.", sep="");
|
|
317 stop_err(msg);
|
|
318 }
|
|
319 return(valid_date);
|
|
320 }
|