annotate utils.R @ 5:d62034b36200 draft default tip

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