-
Notifications
You must be signed in to change notification settings - Fork 0
/
Combine_Peak_Crop_PlotsPDF.R
261 lines (219 loc) · 11.4 KB
/
Combine_Peak_Crop_PlotsPDF.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
codedir = "~/Desktop/github/COVID_LIC"
datadir = "~/paultangerusda drive/2020_Sync/COVID analysis (Paul Tanger)/data/"
plotdir = "~/paultangerusda drive/2020_Sync/COVID analysis (Paul Tanger)/data/plots/test/"
setwd(codedir)
source('functions.R')
source('load_libs.R')
source('plot_crop_cal_function.R')
setwd(datadir)
# load files
crop_plots = readRDS("SelectCountriesAllRegionsCropPlots_20200617_1829.Robj")
other_crop_plots = readRDS("OtherCountriesAllRegionsCropPlots_20200617_1829.Robj")
# from plot_v1.R we should have a list object of ggplots = plots
plots = readRDS("peak_plots_listUKSmooth_20200617_1821.RObj")
# alternative: try to put the crop plots in the same list as peak plots (nested for each country)
plots_to_combine = plots
# TODO: fix with lapply?
#lapply(seq_along(plots_to_combine), function(y) if (names(plots_to_combine)[[y]] %in% names(crop_plots)) {y$crop_plot <- crop_plots$names(plots_to_combine)[[y]] }) #, simplify = FALSE,USE.NAMES = TRUE)
# subset plots to combine for only countries with crop plots..
peak_countries = names(plots_to_combine)
crop_countries = names(crop_plots)
crop_region_countries = names(other_crop_plots)
combine_countries = intersect(peak_countries, crop_countries)
combine_region_countries = intersect(peak_countries, crop_region_countries)
just_peak = setdiff(peak_countries, c(crop_countries, crop_region_countries))
just_GEOGLAM = setdiff(c(crop_countries, crop_region_countries), peak_countries)
# make sub-lists
plots_separate = plots_to_combine[names(plots_to_combine) %in% just_peak]
plots_by_region = plots_to_combine[names(plots_to_combine) %in% combine_region_countries]
plots_to_combine = plots_to_combine[names(plots_to_combine) %in% combine_countries]
######################## MAKE PLOTS ##################################
# convert region names to match filenames for maps
# get the file to make a new col to translate these
setwd(datadir)
sorting_cols = read.csv("JHU_UK_Katie_USAIDv4_GEO_FINAL_20200529_1529.csv")
geoglam = read.csv("GEOGLAM_crop_calendars_v3.csv")
geoglam = geoglam[,c(1,2)]
# put regions, countries and codes together
sorting_cols = sorting_cols[,c(1,3,9)]
map_filenames = merge(geoglam, sorting_cols, by.x = "country", by.y = "GEOGLAM_Country", all.x=T)
map_filenames$region = gsub(" ", "_", map_filenames$region)
map_filenames$region = gsub("_-_", "_", map_filenames$region)
# default is just country code and png
map_filenames$map_filename = paste0(map_filenames$Country_Code, ".png")
# determine if we are plotting regions
map_filenames$plot_regions = F
map_filenames$plot_regions[map_filenames$USAID_Country %in% combine_region_countries] = T
# redo the filenames for these
#map_filenames$map_filename2[map_filenames$plot_regions == T] = paste0(map_filenames$Country_Code, "_", map_filenames$region, ".png")
map_filenames$map_filename = ifelse(map_filenames$plot_regions == T, paste0(map_filenames$Country_Code, "_", map_filenames$region, ".png"), map_filenames$map_filename)
filename = addStampToFilename("map_filenames", "csv")
# write.csv(map_filenames, filename, row.names = F, na="")
# subset into two
map_filenames.countries = unique(map_filenames[map_filenames$plot_regions == F, c(3,5)])
map_filenames.regions = unique(map_filenames[map_filenames$plot_regions == T, c(3,5)])
map_filenames.countries = map_filenames.countries[map_filenames.countries$USAID_Country %in% combine_countries,]
# make plots
setwd("~/paultangerusda drive/2020_Sync/COVID analysis (Paul Tanger)/data/GEOGLAM_map_files/")
# order the country lists
combine_countries = sort(combine_countries)
map.plots = vector(mode = "list", length = length(combine_countries))
names(map.plots) = combine_countries
# for regions
combine_region_countries = sort(combine_region_countries)
map.plots.regions = vector(mode = "list", length = length(combine_region_countries))
names(map.plots.regions) = combine_region_countries
map.plots.regions = map.plots.regions[order(names(map.plots.regions))]
# now create text objects
mytheme <- ttheme_default(base_size = 7)
for(i in seq(map.plots)){
# subset data for each country
#paste(names(map.plots)[i], " ", filename)
countrytext = ggdraw() + draw_label(paste0("Data sources: London School of Hygiene & Tropical Medicine (LSHTM) for COVID-19 modeling\n Group on Earth Observations Global Agricultural Monitoring Initiative (GEOGLAM) for crop calendars\n Johns Hopkins University for date of 50th confirmed cases. Contact: CHillbruner@usaid.gov"),
size = 7, x = 0.1, y = .95, hjust=0)
map.plots[[i]]$map_text <- countrytext
}
# # for regions
for(i in seq(map.plots.regions)){
# subset data for each country
#paste(names(map.plots.regions)[i], " ", filename)
countrytext = ggdraw() + draw_label(paste0("Data sources: London School of Hygiene & Tropical Medicine (LSHTM) for COVID-19 modeling\n Group on Earth Observations Global Agricultural Monitoring Initiative (GEOGLAM) for crop calendars\n Johns Hopkins University for date of 50th confirmed cases. Contact: CHillbruner@usaid.gov"),
size = 7, x = 0.1, y = .95, hjust=0)
map.plots.regions[[i]]$map_text <- countrytext
}
# big loop to make the whole figures
# or use code below.. or the resultant lists:
# for countries: plots_to_combine
# for regions: plots_by_region
# combine plots for countries with both peak and crop plots
# first subset crop plot list
crop_plots_to_combine = crop_plots[names(crop_plots) %in% combine_countries]
plots_to_combine_backup = plots_to_combine
#plots_to_combine = plots_to_combine_backup
# maybe we could rewrite the loop to just match names in two separate lists
# instead of combining into one..
# order lists
crop_plots_to_combine = crop_plots_to_combine[order(names(crop_plots_to_combine))]
plots_to_combine = plots_to_combine[order(names(plots_to_combine))]
for(i in seq(plots_to_combine)){
if (names(plots_to_combine)[[i]] == names(crop_plots_to_combine[i])){
print(paste0("crop_plots$", names(plots_to_combine)[i]))
temp_country_name = names(plots_to_combine)[[i]]
plots_to_combine[[i]]$crop_plot <- crop_plots_to_combine[[temp_country_name]]
}
}
for(i in seq(map.plots)){
if (names(map.plots)[[i]] == names(plots_to_combine[i])){
map.plots[[i]]$title <- ggdraw() + draw_label(names(map.plots[i]), fontface='bold')
bottom <- plot_grid(plots_to_combine[[i]]$cases, plots_to_combine[[i]]$crop_plot, ncol=1, align="v", axis="l", rel_heights = c(2, 1))
map.plots[[i]]$bottom <- bottom
}
}
# save this version with "bottom" plots
setwd(datadir)
filename = addStampToFilename("map.plots_pdf", "RDS")
# saveRDS(map.plots, filename)
###############
# first subset crop plot list
crop_plots_region_to_combine = other_crop_plots[names(other_crop_plots) %in% combine_region_countries]
names(plots_by_region)
names(crop_plots_region_to_combine)
# order lists
crop_plots_region_to_combine = crop_plots_region_to_combine[order(names(crop_plots_region_to_combine))]
plots_by_region = plots_by_region[order(names(plots_by_region))]
# regions
# backup test
map.plots.regions.backup = map.plots.regions
# map.plots.regions = map.plots.regions.backup
# order the list regions the same..
map.plots.regions = map.plots.regions[order(names(map.plots.regions))]
map_filenames.regions = map_filenames.regions[with(map_filenames.regions, order(USAID_Country, map_filename)),]
# fix things where two words not sorting correctly
rownames(map_filenames.regions) <- NULL
# this includes:
map_filenames.regions = map_filenames.regions[c(1:61,63,62,64:124,126,125,127:147),] # 63,62,126,125
# # combine plots into one big list
for(i in seq(plots_by_region)){
if (names(plots_by_region)[[i]] == names(crop_plots_region_to_combine[i])){
print(paste0("crop_plots$", names(plots_by_region)[i]))
temp_country_name = names(plots_by_region)[[i]]
plots_by_region[[i]]$crop_plots <- crop_plots_region_to_combine[[temp_country_name]]
}
}
j=1
for(i in seq(map.plots.regions)){
if (names(map.plots.regions)[[i]] == names(plots_by_region[i])){
map.plots.regions[[i]]$title <- ggdraw() + draw_label(names(map.plots.regions[i]), fontface='bold')
# bottom and map_plot will need to be sublists..
print(names(map.plots.regions[i]))
j=1
for(j in j:length(plots_by_region[[i]]$crop_plots)){
temp_region_name = names(plots_by_region[[i]]$crop_plots[j])
print(temp_region_name)
bottom <- plot_grid(plots_by_region[[i]]$cases, plots_by_region[[i]]$crop_plots[j][[1]][[1]], ncol=1, align="v", axis="l", rel_heights = c(2, 1))
map.plots.regions[[i]]$bottom_plots[temp_region_name] = list(bottom)
#map.plots.regions[[i]][[temp_region_name]] = c(map.plots.regions[[i]][[temp_region_name]] , list(bottom))
}
}
}
# redo the map plots - one for each region
backupmap.plots.regions = map.plots.regions
setwd("~/paultangerusda drive/2020_Sync/COVID analysis (Paul Tanger)/data/GEOGLAM_map_files/")
for(i in seq(map.plots.regions)){
j=1
for(j in j:length(map.plots.regions[[i]]$bottom_plots)){
filename = map_filenames.regions$map_filename[names(map.plots.regions)[i] == map_filenames.regions$USAID_Country][j]
print(filename)
#paste(names(map.plots.regions)[i], " ", filename)
# test = map_filenames.regions$map_filename[names(map.plots.regions)[i] == map_filenames.regions$USAID_Country]
# print(test[j])
# #print(map_filenames.regions$map_filename[names(map.plots.regions)[i] == map_filenames.regions$USAID_Country][j])
mapimg = readPNG(filename)
mapimg = rasterGrob(mapimg)
temp_region_name = names(map.plots.regions[[i]]$bottom_plots[j])
map.plots.regions[[i]]$map_plot[temp_region_name] = list(mapimg)
}
}
region.plots = vector(mode = "list", length = length(map.plots.regions))
for(i in seq(map.plots.regions)){
j = 1
for(j in j:length(map.plots.regions[[i]]$bottom_plots)){
region.plots[[i]][[ names(map.plots.regions[[i]]$bottom_plots[j]) ]] = arrangeGrob(map.plots.regions[[i]]$title, map.plots.regions[[i]]$bottom_plots[[j]], map.plots.regions[[i]]$map_text, map.plots.regions[[i]]$map_plot[[j]], layout_matrix = cbind(c(1,2,2,3), c(1,2,2,4)), heights=c(.4,3,1,1))
#arrangeGrob(map.plots.regions[[i]]$title, map.plots.regions[[i]]$bottom_plots[[j]], map.plots.regions[[i]]$map_text, map.plots.regions[[i]]$map_plot[[j]], layout_matrix = cbind(c(1,2,2,3), c(1,2,2,4)), heights=c(.4,3,1,1))
}
}
names(region.plots) = names(map.plots.regions)
# put into big list
allplots = vector(mode = "list", length = length(map.plots))
# reload map plots
setwd(datadir)
map.plots = readRDS("map.plots_pdf_20200623_1715.RDS")
i=1
for(i in i:length(map.plots)){
if (names(map.plots)[[i]] == names(plots_to_combine[i])){
allplots[[names(map.plots)[[i]]]] = list(arrangeGrob(map.plots[[i]]$title, map.plots[[i]]$bottom, map.plots[[i]]$map_text, map.plots[[i]]$map_plot, layout_matrix = cbind(c(1,2,2,3), c(1,2,2,4)), heights=c(.4,3,1,1)))
}
}
# not sure why this is twice as long..
allplots = allplots[c(36:70)]
# add the region ones..
allplots = c(allplots, region.plots)
# order it
allplots = allplots[order(names(allplots))]
# save it
setwd(datadir)
filename = addStampToFilename("allplots", "RDS")
#saveRDS(allplots, filename)
filename = addStampToFilename("region.plots_pdf", "RDS")
#saveRDS(region.plots, filename)
# try printing together
setwd(plotdir)
filename = addStampToFilename("AllPlotsTogether", "pdf")
pdf(filename, width=8.5, height=11)
for (i in seq(allplots)) {
for (j in seq(allplots[[i]])) {
grid.newpage()
grid.draw(allplots[[i]][[j]])
}
}
dev.off()