annotate utils.R @ 51:3f827fe31756 draft

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