-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_cwc_basins.R
1171 lines (982 loc) · 62.7 KB
/
plot_cwc_basins.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Author - Eva Sinha, Stanford University, [email protected]
# Date - 13th July, 2017
#
# Function details
#
library(ggplot2)
library(ggthemes)
library(rgdal)
library(raster) # projection
library(maptools) # unionSpatialPolygons
library(ggrepel) # geom_text_repel
# Mean annual precip from 1980-2015
# Brahmani and Baitarni - 1429 mm/yr
# Subernarekha - 1374 mm/yr
# Narmada - 1146 mm/yr
# Mahanadi - 1299 mm/yr
# Godavari - 1106 mm/yr
# Cauvery - 903 mm/yr
# Krishna - 753 mm/yr
color_pal <- c('1429' = '#2f5d9e',
'1374' = '#2f799e',
'1146' = '#77d3ec',
'1299' = '#43add0',
'1106' = '#8fdbea',
'903' = '#afe5ed',
'753' = '#d7f2f6',
'Brahmani and Baitarni' = '#2f5d9e',
'Subernarekha' = '#2f799e',
'Narmada' = '#77d3ec',
'Mahanadi' = '#43add0',
'Godavari' = '#8fdbea',
'Cauvery' = '#afe5ed',
'Krishna' = '#d7f2f6',
'Indus (Up to border)' = '#f1fcb3',
'Barak and others' = '#fcebd7',
'Brahmaputra' = '#b6d4fc',
'East flowing rivers between Mahanadi and Pennar' = '#ccfcd5',
'East flowing rivers between Pennar and Kanyakumari' = '#b6f4fc',
'Ganga' = 'deepskyblue',
'Ganga (within India)' = 'deepskyblue',
'Mahi' = '#A020F0',
'Minor rivers draining into Myanmar and Bangladesh' = '#f4fcc7',
'Pennar' = '#e7fcd7',
'Sabarmati' = '#fcb3df',
'Tapi' = 'darkred',
'West flowing rivers of Kutch & Saurashtra in. Luni' = '#d7fcf1',
'West flowing rivers from Tadri to Kanyakumari' = '#d4d9fc',
'West flowing rivers from Tapi to Tadri' = '#c5c7fc')
# ______________________________________________________________________________
# Plot CWC basins and locations of selected stations
# REMAKE PLOTS USING CWC BASINS BASED ON '~/Documents/repos/India_wq/Shapefiles/Hydroshed/select_as_bas_15s_major_CWC_basins'
plot_CWC_basins <- function(filePath, fileName) {
filepath <- '~/Documents/repos/india_wq/India_WRIS/Hydro_ShinyApp/'
f.name <- 'CWC_select_stations.txt'
select.sta.loc <- read.csv(paste(filepath,f.name,sep=''), header=T, sep='\t')
f.name <- 'CWC_basins.txt'
cwc.basin.precip <- read.csv(paste(filepath,f.name,sep=''), header=T, sep='\t')
# Read fertilizer consumption by CWC basins in India based on E and S [kg/km2]
in_folder <- '~/Documents/repos/india_wq/EandS/'
cwc.basin.fert <- read_excel(paste(in_folder,'India_fertilizer_consumption.xlsx',sep=''), sheet='RiverBasin_Nitrogen_kg_km2', skip=1)
# Convert to long format
cwc.basin.fert <- gather(cwc.basin.fert, key=Year, value=fert_kgN_km2, -Basin)
cwc.basin.fert$Year <- as.numeric(substr(cwc.basin.fert$Year,1,4))
# Only keep 2015 fertilizer application rates
cwc.basin.fert <- filter(cwc.basin.fert, Year == 2015)
cwc.basin.fert[which(cwc.basin.fert$Basin == 'Ganga (within India)'), 'Basin'] <- 'Ganga'
# Rename column
colnames(cwc.basin.fert)[which(colnames(cwc.basin.fert) == 'Basin')] <- 'CWC_basin'
# Read CWC shapefile
setwd('~/Documents/repos/india_wq/Shapefiles/Hydroshed/')
cwc.basins.hydroshed <- readOGR(dsn=getwd(),layer='select_as_bas_15s_major_CWC_basins')
setwd('~/Documents/repos/india_wq/Shapefiles/Hydro1K/')
cwc.basins.hydro1k <- readOGR(dsn=getwd(),layer='hydro1K_as_basins')
wq.basins <- readOGR(dsn=getwd(),layer='basin_wq_stations')
streamlines <- readOGR(dsn=getwd(),layer='hydro1K_as_streamlines')
# Remove Musiri station basin
wq.basins <- wq.basins[which(wq.basins@data$WQ_station != 'Musiri'),]
select.sta.loc <- select.sta.loc[which(select.sta.loc$Station != 'Musiri'),]
# Only keep watersheds with CWC basin name
cwc.basins.hydro1k <- cwc.basins.hydro1k[which(!is.na(cwc.basins.hydro1k@data$CWC_basin)),]
streamlines <- streamlines[which(!is.na(streamlines@data$CWC_basin)),]
cwc.basins.hydroshed <- cwc.basins.hydroshed[which(cwc.basins.hydroshed$CWC_basin %in% cwc.basin.precip$CWC_basin),]
cwc.basins.hydro1k <- cwc.basins.hydro1k[which(cwc.basins.hydro1k$CWC_basin %in% cwc.basin.precip$CWC_basin),]
streamlines <- streamlines[which(streamlines$CWC_basin %in% cwc.basin.precip$CWC_basin),]
# Modify projection to WGS84
proj.cwc.basins <- CRS('+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0')
cwc.basins.hydroshed <- spTransform(cwc.basins.hydroshed, proj.cwc.basins)
cwc.basins.hydro1k <- spTransform(cwc.basins.hydro1k, proj.cwc.basins)
wq.basins <- spTransform(wq.basins, proj.cwc.basins)
streamlines <- spTransform(streamlines, proj.cwc.basins)
# CWC basins polygon centroids for placing labels
cwc.basins.centroids <- data.frame(coordinates(cwc.basins.hydroshed), CWC_basin=cwc.basins.hydroshed@data$CWC_basin)
names(cwc.basins.centroids) <- c('long','lat','CWC_basin')
#prepare shapefiles for ploting in ggplot2
cwc.basins.hydro1k@data$id <- rownames(cwc.basins.hydro1k@data)
cwc.basins.hydro1k.points <- fortify(cwc.basins.hydro1k, region='id')
cwc.basins.hydro1k <- merge(cwc.basins.hydro1k.points, cwc.basins.hydro1k@data, by='id')
# To ensure that id gets CWC_basin info that will be used in geom_map.
# A separate column of CWC_basin should also be present for merging with plotting data
cwc.basins.hydroshed@data$id <- cwc.basins.hydroshed@data$CWC_basin
cwc.basins.hydroshed.points <- fortify(cwc.basins.hydroshed, region='id')
cwc.basins.hydroshed <- merge(cwc.basins.hydroshed.points, cwc.basins.hydroshed@data, by='id')
wq.basins@data$id <- rownames(wq.basins@data)
wq.basins.points <- fortify(wq.basins, region='id')
wq.basins <- merge(wq.basins.points, wq.basins@data, by='id')
streamlines@data$id <- rownames(streamlines@data)
streamlines.points <- fortify(streamlines, region='id')
streamlines <- merge(streamlines.points, streamlines@data, by='id')
# India boundary map
world.map <- map_data('world')
india.map <- world.map[which(world.map$region == 'India'),]
# Breaks for annual precipitation
breaks <- c(250, 500, 750, 1000, 1250, 1500, 3000)
color_pal_tmp <- c('[250,500]' = '#e7f7fa',
'(500,750]' = '#bfeaf1',
'(750,1000]' = '#7ed6e7',
'(1000,1250]' = '#2f99bc',
'(1250,1500]' = '#256da7', # '#2f5d9e',
'(1500,3000]' = '#19508f') # '#1d3a63')
# Add range for annual precipitation
cwc.basin.precip$range <- cut(cwc.basin.precip$Avg_precip_mm_yr, dig.lab=5, breaks=breaks, include.lowest=TRUE)
# Update maximum range and color-pal for maximum range
max_precip <- round(max(cwc.basin.precip$Avg_precip_mm_yr, na.rm=TRUE),0)
cwc.basin.precip$range <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), cwc.basin.precip$range)
names(color_pal_tmp) <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), names(color_pal_tmp))
# Update minimum range and color-pal for minimum range
min_precip <- round(min(cwc.basin.precip$Avg_precip_mm_yr, na.rm=TRUE),0)
cwc.basin.precip$range <- gsub(paste('\\[',breaks[max(which(breaks < min_precip))],',',sep=''), paste('\\[',min_precip,',',sep=''), cwc.basin.precip$range)
names(color_pal_tmp) <- gsub(paste('\\[',breaks[max(which(breaks < min_precip))],',',sep=''), paste('\\[',min_precip,',',sep=''), names(color_pal_tmp))
print(table(cwc.basin.precip$range))
# Replace square brackets with round brackets
cwc.basin.precip$range <- gsub('\\[', '(', cwc.basin.precip$range)
cwc.basin.precip$range <- gsub('\\]', ')', cwc.basin.precip$range)
names(color_pal_tmp) <- gsub('\\[', '(', names(color_pal_tmp))
names(color_pal_tmp) <- gsub('\\]', ')', names(color_pal_tmp))
# Reorder factor levels
cwc.basin.precip$range <- factor(cwc.basin.precip$range, levels=names(color_pal_tmp))
print(table(cwc.basin.precip$range))
# Only keep streamlines for main rivers
maj_streamlines <- na.omit(streamlines)
# Drop unused levels
cwc.basins.hydroshed <- droplevels(cwc.basins.hydroshed)
# --- Make spatial plot of mean annual precipitation ---
# Change directory for saving plot
setwd(filePath)
# Delete existing file
fileName <- 'CWC_basins_mean_precip.png'
unlink(fileName)
# Start pdf device driver for saving plots
png(fileName, width = 700, height = 800)
p1 <- ggplot() +
geom_map(data=cwc.basin.precip[,c('CWC_basin','range')],
aes(map_id=CWC_basin, fill=range), map=cwc.basins.hydroshed) +
geom_line(data=maj_streamlines, aes(x=long, y=lat, group=group), color='white', size=1.2) +
# geom_line(data=streamlines, aes(x=long,y=lat,group=group),color='dodgerblue', size=0.3) +
geom_path(data=cwc.basins.hydroshed, aes(x=long, y=lat, group=group), color='black', size=0.75) +
geom_path(data=india.map, aes(x=long, y=lat, group=group), color='grey15', size=1.2) +
geom_text_repel(data=subset(cwc.basins.centroids, CWC_basin %in% c('Indus (Up to border)','Godavari','Krishna','Cauvery','Narmada','Sabarmati','Mahi','Tapi','Pennar')),
aes(x=long, y=lat, label=CWC_basin),
nudge_x = 70-subset(cwc.basins.centroids, CWC_basin %in% c('Godavari','Krishna','Cauvery','Narmada','Sabarmati','Mahi','Tapi','Pennar'))$long,
nudge_y = -0.5,
size = 6,
segment.size = 0.75,
segment.color = 'black',
direction = 'y',
hjust = 1) +
geom_text_repel(data=subset(cwc.basins.centroids, CWC_basin %in% c('Brahmani and Baitarni','Subernarekha','Mahanadi')),
aes(x=long, y=lat, label=CWC_basin),
nudge_x = 90-subset(cwc.basins.centroids, CWC_basin %in% c('Brahmani and Baitarni','Subernarekha','Mahanadi'))$long,
nudge_y = -1.5,
size = 6,
segment.size = 0.75,
segment.color = 'black',
direction = 'y',
hjust = 0) +
geom_text_repel(data=subset(cwc.basins.centroids, CWC_basin %in% c('Ganga','Brahmaputra')),
aes(x=long, y=lat, label=CWC_basin),
nudge_x = 1.0,
nudge_y = 31.0-subset(cwc.basins.centroids, CWC_basin %in% c('Ganga','Brahmaputra'))$lat,
size = 6,
segment.size = 0.75,
segment.color = 'black',
direction = 'x',
vjust = 0) +
scale_fill_manual(values=color_pal_tmp) +
scale_x_continuous(limits=c(min(cwc.basins.hydroshed$long),max(cwc.basins.hydroshed$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(cwc.basins.hydroshed$lat), max(cwc.basins.hydroshed$lat)), expand = c(0, 0)) +
guides(fill=guide_legend(ncol=1, title=expression(paste(Precipitation,' [',mm~yr^-1,']')))) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=18,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = c(0.85,0.25),
legend.justification = 'center',
legend.text = element_text(size=18,family='Helvetica'))
print(p1)
dev.off()
# ---
# Delete existing file
fileName <- 'CWC_basins_mean_precip_2.png'
unlink(fileName)
# Start pdf device driver for saving plots
png(fileName, width = 750, height = 800)
# Keep major streamlines and station locations for the final basins only
final_basins <- c('Brahmani and Baitarni','Subernarekha','Narmada','Mahanadi','Godavari','Cauvery','Krishna')
maj_streamlines <- maj_streamlines[which(maj_streamlines$CWC_basin %in% final_basins),]
plot.select.sta.loc <- select.sta.loc[which(select.sta.loc$CWC_basin %in% final_basins),]
p2 <- ggplot() +
geom_map(data=cwc.basin.precip[,c('CWC_basin','range')],
aes(map_id=CWC_basin, fill=range), map=cwc.basins.hydroshed) +
geom_line(data=maj_streamlines, aes(x=long, y=lat, group=group), color='white', size=1.2) +
# geom_line(data=streamlines, aes(x=long, y=lat, group=group), color='dodgerblue', size=0.3) +
geom_path(data=subset(cwc.basins.hydroshed, !CWC_basin %in% final_basins), aes(x=long, y=lat, group=group), color='black', size=0.75) +
geom_path(data=subset(cwc.basins.hydroshed, CWC_basin %in% final_basins), aes(x=long, y=lat, group=group), color='#ff9000',size=0.75) +
geom_path(data=india.map, aes(x=long, y=lat, group=group), color='grey15', size=1.2) +
geom_point(data=plot.select.sta.loc, aes(x=Longitude, y=Latitude), fill='red', color='black', shape=21, size=4) +
geom_text_repel(data=subset(plot.select.sta.loc, !Station %in% c('Mandleshwar')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = 2.6,
size = 6.5,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
direction = 'y',
hjust = 0) +
geom_text_repel(data=subset(plot.select.sta.loc, Station %in% c('Mandleshwar')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = -3.5,
nudge_y = -1.8,
size = 6.5,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
hjust = 1) +
scale_fill_manual(values=color_pal_tmp) +
scale_x_continuous(limits=c(min(cwc.basins.hydroshed$long),max(cwc.basins.hydroshed$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(cwc.basins.hydroshed$lat), max(cwc.basins.hydroshed$lat)), expand = c(0, 0)) +
# guides(fill=guide_legend(ncol=1, title=expression(paste(Mean~precipitation,' [',mm~yr^-1,']')))) +
guides(fill=guide_legend(ncol=1, title=expression(atop(Mean~precipitation, paste('[',mm~yr^-1,']'))))) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=20,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = c(0.84,0.25),
legend.justification = 'center',
legend.title.align = 1,
legend.text = element_text(size=20,family='Helvetica'))
print(p2)
dev.off()
# --- Make spatial plot of interannual variability of annual precipitation ---
# Breaks for annual precipitation
breaks <- c(50, 100, 150, 200, 250, 500)
color_pal_tmp <- c('[50,100]' = '#e7f7fa',
'(100,150]' = '#bfeaf1',
'(150,200]' = '#7ed6e7',
'(200,250]' = '#2f99bc',
'(250,500]' = '#256da7')
# Add range for annual precipitation
cwc.basin.precip$range <- cut(cwc.basin.precip$sd_precip_mm_yr, dig.lab=5, breaks=breaks, include.lowest=TRUE)
# Update maximum range and color-pal for maximum range
max_precip <- round(max(cwc.basin.precip$sd_precip_mm_yr, na.rm=TRUE),0)
cwc.basin.precip$range <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), cwc.basin.precip$range)
names(color_pal_tmp) <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), names(color_pal_tmp))
# Update minimum range and color-pal for minimum range
min_precip <- round(min(cwc.basin.precip$sd_precip_mm_yr, na.rm=TRUE),0)
cwc.basin.precip$range <- gsub(paste('\\[',breaks[max(which(breaks < min_precip))],',',sep=''), paste('\\[',min_precip,',',sep=''), cwc.basin.precip$range)
names(color_pal_tmp) <- gsub(paste('\\[',breaks[max(which(breaks < min_precip))],',',sep=''), paste('\\[',min_precip,',',sep=''), names(color_pal_tmp))
# Reorder factor levels
cwc.basin.precip$range <- factor(cwc.basin.precip$range, levels=names(color_pal_tmp))
# Replace square brackets with round brackets
cwc.basin.precip$range <- gsub('\\[', '(', cwc.basin.precip$range)
cwc.basin.precip$range <- gsub('\\]', ')', cwc.basin.precip$range)
names(color_pal_tmp) <- gsub('\\[', '(', names(color_pal_tmp))
names(color_pal_tmp) <- gsub('\\]', ')', names(color_pal_tmp))
# Delete existing file
fileName <- 'CWC_basins_mean_sd_2.png'
unlink(fileName)
# Start pdf device driver for saving plots
png(fileName, width = 750, height = 850)
p3 <- ggplot() +
geom_map(data=cwc.basin.precip[,c('CWC_basin','range')],
aes(map_id=CWC_basin, fill=range), map=cwc.basins.hydroshed) +
geom_line(data=maj_streamlines, aes(x=long, y=lat, group=group), color='white', size=1.2) +
# geom_line(data=streamlines, aes(x=long, y=lat, group=group), color='dodgerblue', size=0.3) +
geom_path(data=subset(cwc.basins.hydroshed, !CWC_basin %in% final_basins), aes(x=long, y=lat, group=group), color='black', size=0.75) +
geom_path(data=subset(cwc.basins.hydroshed, CWC_basin %in% final_basins), aes(x=long, y=lat, group=group), color='#ff9000',size=0.75) +
geom_path(data=india.map, aes(x=long, y=lat, group=group), color='grey15', size=1.2) +
geom_point(data=plot.select.sta.loc, aes(x=Longitude, y=Latitude),fill='red',color='black',shape=21,size=4) +
geom_text_repel(data=subset(plot.select.sta.loc, !Station %in% c('Mandleshwar')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = 2.6,
size = 6,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
direction = 'y',
hjust = 0) +
geom_text_repel(data=subset(plot.select.sta.loc, Station %in% c('Mandleshwar')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = -4,
nudge_y = -1.8,
size = 6,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
hjust = 1) +
scale_fill_manual(values=color_pal_tmp) +
scale_x_continuous(limits=c(min(cwc.basins.hydroshed$long),max(cwc.basins.hydroshed$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(cwc.basins.hydroshed$lat), max(cwc.basins.hydroshed$lat)), expand = c(0, 0)) +
guides(fill=guide_legend(ncol=1, title=expression(paste(Interannual~variability,' [',mm~yr^-1,']')))) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=20,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = c(0.835,0.25),
legend.justification = 'center',
legend.text = element_text(size=20,family='Helvetica'))
print(p3)
dev.off()
# --- Make spatial plot of coefficient of variation of annual precipitation ---
# Add coefficient of variation
cwc.basin.precip$cv_precip <- cwc.basin.precip$sd_precip_mm_yr/cwc.basin.precip$Avg_precip_mm_yr
# Breaks for annual precipitation
breaks <- c(0, 0.15, 0.2, 0.25, 0.3, 0.4)
color_pal_tmp <- c('[0,0.15]' = '#ffffd4', # 'gray90',
'(0.15,0.2]' = '#fed98e', # 'gray70',
'(0.2,0.25]' = '#fe9929', # 'gray50',
'(0.25,0.3]' = '#d95f0e', # 'gray30',
'(0.3,0.4]' = '#993404') # 'gray20')
# Add range for annual precipitation
cwc.basin.precip$range <- cut(cwc.basin.precip$cv_precip, dig.lab=5, breaks=breaks, include.lowest=TRUE)
# Update maximum range and color-pal for maximum range
max_precip <- round(max(cwc.basin.precip$cv_precip, na.rm=TRUE),2)
cwc.basin.precip$range <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), cwc.basin.precip$range)
names(color_pal_tmp) <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), names(color_pal_tmp))
# Update minimum range and color-pal for minimum range
min_precip <- round(min(cwc.basin.precip$cv_precip, na.rm=TRUE),2)
cwc.basin.precip$range <- gsub(paste('\\[',breaks[max(which(breaks < min_precip))],',',sep=''), paste('\\[',min_precip,',',sep=''), cwc.basin.precip$range)
names(color_pal_tmp) <- gsub(paste('\\[',breaks[max(which(breaks < min_precip))],',',sep=''), paste('\\[',min_precip,',',sep=''), names(color_pal_tmp))
# Reorder factor levels
cwc.basin.precip$range <- factor(cwc.basin.precip$range, levels=names(color_pal_tmp))
# Replace square brackets with round brackets
cwc.basin.precip$range <- gsub('\\[', '(', cwc.basin.precip$range)
cwc.basin.precip$range <- gsub('\\]', ')', cwc.basin.precip$range)
names(color_pal_tmp) <- gsub('\\[', '(', names(color_pal_tmp))
names(color_pal_tmp) <- gsub('\\]', ')', names(color_pal_tmp))
# Delete existing file
fileName <- 'CWC_basins_cv_2.png'
unlink(fileName)
# Start pdf device driver for saving plots
png(fileName, width = 750, height = 800)
p4 <- ggplot() +
geom_map(data=cwc.basin.precip[,c('CWC_basin','range')],
aes(map_id=CWC_basin, fill=range), map=cwc.basins.hydroshed) +
geom_line(data=maj_streamlines, aes(x=long, y=lat, group=group), color='blue', size=1.2) +
# geom_line(data=streamlines, aes(x=long, y=lat, group=group), color='dodgerblue', size=0.3) +
geom_path(data=subset(cwc.basins.hydroshed, !CWC_basin %in% final_basins), aes(x=long, y=lat, group=group), color='black', size=0.75) +
geom_path(data=subset(cwc.basins.hydroshed, CWC_basin %in% final_basins), aes(x=long, y=lat, group=group), color='#ff9000',size=0.75) +
geom_path(data=india.map, aes(x=long, y=lat, group=group), color='grey15', size=1.2) +
geom_point(data=plot.select.sta.loc, aes(x=Longitude, y=Latitude),fill='red',color='black',shape=21,size=4) +
geom_text_repel(data=subset(plot.select.sta.loc, !Station %in% c('Mandleshwar')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = 2.6,
size = 6.5,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
direction = 'y',
hjust = 0) +
geom_text_repel(data=subset(plot.select.sta.loc, Station %in% c('Mandleshwar')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = -3.0,
nudge_y = -1.8,
size = 6.5,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
hjust = 1) +
scale_fill_manual(values=color_pal_tmp) +
scale_x_continuous(limits=c(min(cwc.basins.hydroshed$long),max(cwc.basins.hydroshed$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(cwc.basins.hydroshed$lat), max(cwc.basins.hydroshed$lat)), expand = c(0, 0)) +
guides(fill=guide_legend(ncol=1, title=expression(paste(Coefficient~of~variation)))) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=20,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = c(0.84,0.25),
legend.justification = 'center',
legend.text = element_text(size=20,family='Helvetica'))
print(p4)
dev.off()
# --- Make spatial plot of fertilizer application rates ---
# Breaks for annual precipitation
breaks <- seq(1500, 8500, 500)
color_pal_tmp <- c('[1500,2000]' = '#edf8e9',
'(2000,2500]' = '#c7e9c0',
'(3500,4000]' = '#a1d99b',
'(4500,5000]' = '#74c476',
'(5000,5500]' = '#31a354',
'(8000,8500]' = '#006d2c')
# Add range for annual precipitation
cwc.basin.fert$range <- cut(cwc.basin.fert$fert_kgN_km2, dig.lab=5, breaks=breaks, include.lowest=TRUE)
print(table(cwc.basin.fert$range))
# Update maximum range and color-pal for maximum range
max_fert <- round(max(cwc.basin.fert$fert_kgN_km2, na.rm=TRUE),0)
cwc.basin.fert$range <- gsub(paste(',',breaks[min(which(breaks > max_fert))],sep=''), paste(',',max_fert,sep=''), cwc.basin.fert$range)
names(color_pal_tmp) <- gsub(paste(',',breaks[min(which(breaks > max_fert))],sep=''), paste(',',max_fert,sep=''), names(color_pal_tmp))
# Update minimum range and color-pal for minimum range
min_fert <- round(min(cwc.basin.fert$fert_kgN_km2, na.rm=TRUE),0)
cwc.basin.fert$range <- gsub(paste('\\[',breaks[max(which(breaks < min_fert))],',',sep=''), paste('\\[',min_fert,',',sep=''), cwc.basin.fert$range)
names(color_pal_tmp) <- gsub(paste('\\[',breaks[max(which(breaks < min_fert))],',',sep=''), paste('\\[',min_fert,',',sep=''), names(color_pal_tmp))
# Reorder factor levels
cwc.basin.fert$range <- factor(cwc.basin.fert$range, levels=names(color_pal_tmp))
# Replace square brackets with round brackets
cwc.basin.fert$range <- gsub('\\[', '(', cwc.basin.fert$range)
cwc.basin.fert$range <- gsub('\\]', ')', cwc.basin.fert$range)
names(color_pal_tmp) <- gsub('\\[', '(', names(color_pal_tmp))
names(color_pal_tmp) <- gsub('\\]', ')', names(color_pal_tmp))
# Reorder factor levels
cwc.basin.fert$range <- factor(cwc.basin.fert$range, levels=names(color_pal_tmp))
print(table(cwc.basin.fert$range))
# Delete existing file
fileName <- 'CWC_basins_fert_2.png'
unlink(fileName)
# Start pdf device driver for saving plots
png(fileName, width = 750, height = 800)
# Keep major streamlines and station locations for the final basins only
final_basins <- c('Brahmani and Baitarni','Subernarekha','Narmada','Mahanadi','Godavari','Cauvery','Krishna')
maj_streamlines <- maj_streamlines[which(maj_streamlines$CWC_basin %in% final_basins),]
plot.select.sta.loc <- select.sta.loc[which(select.sta.loc$CWC_basin %in% final_basins),]
p5 <- ggplot() +
geom_map(data=cwc.basin.fert[,c('CWC_basin','range')],
aes(map_id=CWC_basin, fill=range), map=cwc.basins.hydroshed) +
geom_line(data=maj_streamlines, aes(x=long, y=lat, group=group), color='white', size=1.2) +
# geom_line(data=streamlines, aes(x=long, y=lat, group=group), color='dodgerblue', size=0.3) +
geom_path(data=subset(cwc.basins.hydroshed, !CWC_basin %in% final_basins), aes(x=long, y=lat, group=group), color='black', size=0.75) +
geom_path(data=subset(cwc.basins.hydroshed, CWC_basin %in% final_basins), aes(x=long, y=lat, group=group), color='#ff9000',size=0.75) +
geom_path(data=india.map, aes(x=long, y=lat, group=group), color='grey15', size=1.2) +
geom_point(data=plot.select.sta.loc, aes(x=Longitude, y=Latitude), fill='red', color='black', shape=21, size=4) +
geom_text_repel(data=subset(plot.select.sta.loc, !Station %in% c('Mandleshwar')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = 2.6,
size = 6.5,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
direction = 'y',
hjust = 0) +
geom_text_repel(data=subset(plot.select.sta.loc, Station %in% c('Mandleshwar')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = -3.5,
nudge_y = -1.8,
size = 6.5,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
hjust = 1) +
scale_fill_manual(values=color_pal_tmp) +
scale_x_continuous(limits=c(min(cwc.basins.hydroshed$long),max(cwc.basins.hydroshed$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(cwc.basins.hydroshed$lat), max(cwc.basins.hydroshed$lat)), expand = c(0, 0)) +
# guides(fill=guide_legend(ncol=1, title=expression(paste(Mean~precipitation,' [',mm~yr^-1,']')))) +
guides(fill=guide_legend(ncol=1, title=expression(atop(Fert~application~rate, paste('[',kgN~km^-2,']'))))) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=20,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = c(0.84,0.25),
legend.justification = 'center',
legend.title.align = 1,
legend.text = element_text(size=20,family='Helvetica'))
print(p5)
dev.off()
# ---
# Delete existing file
unlink('CWC_basins_3.png')
# Start pdf device driver for saving plots
png('CWC_basins_3.png', width = 1000, height = 800)
plot.cwc.basins <- cwc.basins.hydro1k[which(cwc.basins.hydro1k$CWC_basin %in% wq.basins$CWC_basin),]
p3 <- ggplot() +
geom_polygon(data=plot.cwc.basins, aes(x=long, y=lat, group=group, fill=CWC_basin, color='Hydro1K basins'), size=0.5) +
geom_polygon(data=wq.basins, aes(x=long, y=lat, group=group, color='Watershed upstream of station'), fill='transparent',size=1.05) +
geom_path(data=india.map, aes(x=long, y=lat, group=group),color='grey50',size=0.5) +
geom_point(data=select.sta.loc, aes(x=Longitude, y=Latitude), fill='red', color='black', shape=21, size=4) +
geom_text(data=select.sta.loc, aes(x=Longitude, y=Latitude, label=Station), color='black', size=6, nudge_y=0.5) +
scale_fill_manual(values=color_pal) +
scale_color_manual(name='',values=c('Hydro1K basins'='grey50','Watershed upstream of station'='black')) +
scale_x_continuous(limits=c(min(india.map$long),max(india.map$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(india.map$lat), max(india.map$lat)), expand = c(0, 0)) +
guides(fill =guide_legend(ncol=1, title='CWC basins'),
color=guide_legend(nrow=2, title=NULL, override.aes=list(fill=NA))) + # Override.aes for cleaning legends
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=15,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = 'right',
legend.justification = 'center')
print(p3)
dev.off()
# ---
# Delete existing file
unlink('CWC_basins_4.png')
# Start pdf device driver for saving plots
png('CWC_basins_4.png', width = 1000, height = 800)
final_basins <- c('Brahmani and Baitarni','Subernarekha','Narmada',
'Mahanadi','Godavari','Cauvery','Krishna')
plot.cwc.basins <- plot.cwc.basins[which(plot.cwc.basins$CWC_basin %in% final_basins),]
plot.wq.basins <- wq.basins[which(wq.basins$CWC_basin %in% final_basins),]
plot.select.sta.loc <- select.sta.loc[which(select.sta.loc$CWC_basin %in% final_basins),]
# Reorder factor levels
plot.cwc.basins$CWC_basin <- factor(plot.cwc.basins$CWC_basin, levels=final_basins)
plot.wq.basins$CWC_basin <- factor(plot.wq.basins$CWC_basin, levels=final_basins)
p4 <- ggplot() +
geom_polygon(data=plot.cwc.basins, aes(x=long,y=lat,fill=CWC_basin,color='Hydro1K basins',group=group), size=0.5) +
geom_polygon(data=plot.wq.basins, aes(x=long,y=lat,color='Watershed upstream of station',group=group),fill='transparent',size=1.05) +
geom_path(data=india.map, aes(x=long,y=lat,group=group),color='grey50',size=0.5) +
geom_point(data=plot.select.sta.loc, aes(x=Longitude, y=Latitude),fill='red',color='black',shape=21,size=4) +
geom_text(data=plot.select.sta.loc, aes(x=Longitude, y=Latitude,label=Station),color='black',size=6,nudge_y = 0.5) +
scale_fill_manual(name='CWC basin', values=color_pal) +
scale_color_manual(name='',values=c('Hydro1K basins'='grey50','Watershed upstream of station'='black')) +
scale_x_continuous(limits=c(min(india.map$long),max(india.map$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(india.map$lat), max(india.map$lat)), expand = c(0, 0)) +
guides(fill =guide_legend(ncol=1, title='CWC basins'),
color=guide_legend(nrow=2, title=NULL, override.aes=list(fill=NA))) + # Override.aes for cleaning legends
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=15,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = 'right',
legend.justification = 'center')
print(p4)
dev.off()
# ------- Make individual plot for each station ---
for (ind in 1:nrow(select.sta.loc)){
plot.station <- select.sta.loc[ind,]
plot.cwc.basins <- cwc.basins.hydro1k[which(cwc.basins.hydro1k$BasinId == as.character(plot.station$BasinId)),]
# Delete existing file
fileName <- paste(plot.station$Station, '.png', sep='')
unlink(fileName)
# Start pdf device driver for saving plots
png(fileName, width = 700, height = 800)
p5 <- ggplot() +
geom_polygon(data=plot.cwc.basins, aes(x=long,y=lat,group=group, fill=CWC_basin)) +
geom_path(data=india.map, aes(x=long,y=lat,group=group),color='grey15',size=1.2) +
geom_point(data=plot.station, aes(x=Longitude, y=Latitude),fill='red',color='black',shape=21,size=8) +
geom_text(data=plot.station, aes(x=Longitude, y=Latitude,label=Station),color='black',size=8,nudge_y = 0.6) +
scale_fill_manual(values=color_pal) +
scale_x_continuous(limits=c(min(india.map$long),max(india.map$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(india.map$lat), max(india.map$lat)), expand = c(0, 0)) +
guides(fill=guide_legend(ncol=1, title='CWC basins')) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=20,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.text = element_text(size=20,family='Helvetica'),
legend.direction ='vertical',
legend.position = c(0.5,0.2))
print(p5)
dev.off()
}
}
# ______________________________________________________________________________
# Plot CWC basins in Southern India and locations of selected stations
plot_southern_CWC_basins <- function(filePath, fileName) {
filepath <- '~/Documents/repos/india_wq/India_WRIS/Hydro_ShinyApp/'
f.name <- 'CWC_select_stations.txt'
select.sta.loc <- read.csv(paste(filepath,f.name,sep=''), header=T, sep='\t')
# Read CWC shapefile
setwd('~/Documents/repos/india_wq/Shapefiles/Hydro1K/')
cwc.basins <- readOGR(dsn=getwd(),layer='hydro1K_as_basins')
wq.basins <- readOGR(dsn=getwd(),layer='basin_wq_stations')
streamlines <- readOGR(dsn=getwd(),layer='hydro1K_as_streamlines')
# Remove Musiri station basin
wq.basins <- wq.basins[which(wq.basins@data$WQ_station != 'Musiri'),]
select.sta.loc <- select.sta.loc[which(select.sta.loc$Station != 'Musiri'),]
# Only keep watersheds with CWC basin name
cwc.basins <- cwc.basins[which(!is.na(cwc.basins@data$CWC_basin)),]
streamlines <- streamlines[which(!is.na(streamlines@data$CWC_basin)),]
# Remove northen basins and smaller east and west flowing basins
north_basins <- c('Brahmaputra',
'Barak and others',
'Ganga',
'Indus (Up to border)',
'Area of inland drainage in Rajasthan',
'Minor rivers draining into Myanmar and Bangladesh',
'West flowing rivers of Kutch & Saurashtra in. Luni')
small_basins <- c('East flowing rivers between Mahanadi and Pennar',
'East flowing rivers between Pennar and Kanyakumari',
'West flowing rivers from Tadri to Kanyakumari',
'West flowing rivers from Tapi to Tadri')
cwc.basins <- cwc.basins[-which(cwc.basins@data$CWC_basin %in% c(north_basins,small_basins)),]
streamlines <- streamlines[-which(streamlines@data$CWC_basin %in% c(north_basins,small_basins)),]
# Read shapefile for Indian states
setwd('~/Documents/repos/india_wq/Shapefiles/GADM/gadm36_IND_shp/')
india.states <- readOGR(dsn=getwd(),layer='gadm36_IND_1')
# Modify projection to WGS84
proj.cwc.basins <- CRS('+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0')
cwc.basins <- spTransform(cwc.basins, proj.cwc.basins)
wq.basins <- spTransform(wq.basins, proj.cwc.basins)
streamlines <- spTransform(streamlines, proj.cwc.basins)
india.states <- spTransform(india.states, proj.cwc.basins)
# Aggregate smaller watersheds within CWC basins
# https://rud.is/projects/dissolving_polygons.html
cwc.basins <- unionSpatialPolygons(cwc.basins, cwc.basins@data$CWC_basin)
# Convert back to SpatialPolygonsDataFrame
cwc.basins <- SpatialPolygonsDataFrame(spTransform(cwc.basins,CRS('+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0')),
data.frame(CWC_basin=names(cwc.basins),
row.names=names(cwc.basins),
stringsAsFactors=FALSE))
# CWC basins polygon centroids for placing labels
cwc.basins.centroids <- data.frame(coordinates(cwc.basins), CWC_basin=cwc.basins@data$CWC_basin)
names(cwc.basins.centroids) <- c('long','lat','CWC_basin')
#prepare shapefiles for ploting in ggplot2
cwc.basins@data$id <- rownames(cwc.basins@data)
cwc.basins.points <- fortify(cwc.basins, region='id')
cwc.basins <- merge(cwc.basins.points, cwc.basins@data, by='id')
wq.basins@data$id <- rownames(wq.basins@data)
wq.basins.points <- fortify(wq.basins, region='id')
wq.basins <- merge(wq.basins.points, wq.basins@data, by='id')
streamlines@data$id <- rownames(streamlines@data)
streamlines.points <- fortify(streamlines, region='id')
streamlines <- merge(streamlines.points, streamlines@data, by='id')
india.states@data$id <- rownames(india.states@data)
india.states.points <- fortify(india.states, region='id')
india.states <- merge(india.states.points, india.states@data, by='id')
# India boundary map
world.map <- map_data('world')
india.map <- world.map[which(world.map$region == 'India'),]
# Breaks for annual precipitation
breaks <- c(500, 750, 1000, 1250, 1500, 3000)
color_pal_tmp <- c('[500,750]' = '#bfeaf1',
'(750,1000]' = '#7ed6e7',
'(1000,1250]' = '#2f99bc',
'(1250,1500]' = '#2f5d9e',
'(1500,3000]' = '#1d3a63')
# Add range for annual precipitation
select.sta.loc$range <- cut(select.sta.loc$Avg_precip_mm_yr, dig.lab=5, breaks=breaks, include.lowest=TRUE)
# Update maximum range and color-pal for maximum range
max_precip <- round(max(select.sta.loc$Avg_precip_mm_yr, na.rm=TRUE),0)
select.sta.loc$range <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), select.sta.loc$range)
names(color_pal_tmp) <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), names(color_pal_tmp))
# Update minimum range and color-pal for minimum range
min_precip <- round(min(select.sta.loc$Avg_precip_mm_yr, na.rm=TRUE),0)
select.sta.loc$range <- gsub(paste(breaks[max(which(breaks < min_precip))],',',sep=''), paste(min_precip,',',sep=''), select.sta.loc$range)
names(color_pal_tmp) <- gsub(paste(breaks[max(which(breaks < min_precip))],',',sep=''), paste(min_precip,',',sep=''), names(color_pal_tmp))
# Reorder factor levels
select.sta.loc$range <- factor(select.sta.loc$range, levels=names(color_pal_tmp))
# Only keep streamlines for main rivers
maj_streamlines <- na.omit(streamlines)
# ---
# Change directory for saving plot
setwd(filePath)
# Delete existing file
unlink(fileName)
# Start pdf device driver for saving plots
png(fileName, width = 700, height = 800)
p1 <- ggplot() +
geom_map(data=select.sta.loc[,c('CWC_basin','range')],
aes(map_id=CWC_basin, fill=range), map=cwc.basins) +
geom_line(data=streamlines, aes(x=long,y=lat,group=group),color='dodgerblue', size=0.3) +
geom_line(data=maj_streamlines, aes(x=long,y=lat,group=group),color='blueviolet', size=1.2) +
geom_path(data=cwc.basins, aes(x=long,y=lat,group=group),color='black', size=0.75) +
geom_path(data=india.map, aes(x=long,y=lat,group=group),color='grey15', size=1.2) +
geom_point(data=select.sta.loc, aes(x=Longitude, y=Latitude),fill='red',color='black',shape=21,size=4) +
geom_text_repel(data=subset(select.sta.loc, !Station %in% c('Mandleshwar','Khanpur','Vautha','Sarangkheda')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = 2.6,
size = 6,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
direction = 'y',
hjust = 0) +
geom_text_repel(data=subset(select.sta.loc, Station %in% c('Mandleshwar','Khanpur','Vautha','Sarangkheda')),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = -4.0,
nudge_y = -1.8,
size = 6,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
hjust = 1) +
scale_fill_manual(values=color_pal_tmp) +
scale_x_continuous(limits=c(min(india.map$long),max(india.map$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(india.map$lat), max(india.map$lat)), expand = c(0, 0)) +
guides(fill=guide_legend(ncol=1, title=expression(paste(Mean~precipitation,' [',mm~yr^-1,']')))) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=18,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = c(0.84,0.25),
legend.justification = 'center',
legend.text = element_text(size=18,family='Helvetica'))
print(p1)
dev.off()
# ---
final_basins <- c('Brahmani and Baitarni','Subernarekha','Narmada','Mahanadi','Godavari','Cauvery','Krishna')
plot.select.sta.loc <- select.sta.loc[which(select.sta.loc$CWC_basin %in% final_basins),]
cwc.basins <- cwc.basins[which(cwc.basins$CWC_basin %in% final_basins),]
cwc.basins.centroids <- cwc.basins.centroids[which(cwc.basins.centroids$CWC_basin %in% final_basins),]
streamlines <- streamlines[which(streamlines$CWC_basin %in% final_basins),]
# Reorder factor levels
plot.select.sta.loc$CWC_basin <- factor(plot.select.sta.loc$CWC_basin, levels=final_basins)
cwc.basins$CWC_basin <- factor(cwc.basins$CWC_basin, levels=final_basins)
cwc.basins.centroids$CWC_basin <- factor(cwc.basins.centroids$CWC_basin, levels=final_basins)
streamlines$CWC_basin <- factor(streamlines$CWC_basin, levels=final_basins)
# Only keep streamlines for main rivers
maj_streamlines <- na.omit(streamlines)
# Breaks for annual precipitation
breaks <- c(500, 750, 1000, 1250, 1500, 3000)
color_pal_tmp <- c('[500,750]' = '#e7f7fa',
'(750,1000]' = '#bfeaf1', # '#d7f2f6',
'(1000,1250]' = '#7ed6e7', # '#8fdbea',
'(1250,1500]' = '#2f99bc', # '#2f5d9e'
'(1500,3000]' = '#2f5d9e')
# color_pal_tmp <- c('[750,900]' = '#d7f2f6',
# '(900,1050]' = '#afe5ed',
# '(1050,1200]' = '#8fdbea',
# '(1200,1350]' = '#43add0',
# '(1350,1500]' = '#2f5d9e')
# Add range for annual precipitation
plot.select.sta.loc$range <- cut(plot.select.sta.loc$Avg_precip_mm_yr, dig.lab=5, breaks=breaks, include.lowest=TRUE)
# Update maximum range and color-pal for maximum range
max_precip <- round(max(plot.select.sta.loc$Avg_precip_mm_yr),0)
plot.select.sta.loc$range <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), plot.select.sta.loc$range)
names(color_pal_tmp) <- gsub(paste(',',breaks[min(which(breaks > max_precip))],sep=''), paste(',',max_precip,sep=''), names(color_pal_tmp))
# Update minimum range and color-pal for minimum range
min_precip <- round(min(plot.select.sta.loc$Avg_precip_mm_yr),0)
plot.select.sta.loc$range <- gsub(paste(breaks[max(which(breaks < min_precip))],',',sep=''), paste(min_precip,',',sep=''), plot.select.sta.loc$range)
names(color_pal_tmp) <- gsub(paste(breaks[max(which(breaks < min_precip))],',',sep=''), paste(min_precip,',',sep=''), names(color_pal_tmp))
# Reorder factor levels
plot.select.sta.loc$range <- factor(plot.select.sta.loc$range, levels=names(color_pal_tmp))
# ---------- Delete existing file ---
unlink('CWC_basins_South_India_2.png')
# Start pdf device driver for saving plots
png('CWC_basins_South_India_2.png', width = 700, height = 800)
p2 <- ggplot() +
geom_map(data=plot.select.sta.loc[,c('CWC_basin','range')],
aes(map_id=CWC_basin, fill=range), map=cwc.basins) +
geom_line(data=streamlines, aes(x=long,y=lat,group=group),color='dodgerblue', size=0.3) +
geom_line(data=maj_streamlines, aes(x=long,y=lat,group=group),color='blueviolet', size=1.2) +
geom_path(data=cwc.basins, aes(x=long,y=lat,group=group),color='black', size=0.75) +
geom_path(data=india.map, aes(x=long,y=lat,group=group),color='grey15', size=1.2) +
geom_point(data=plot.select.sta.loc, aes(x=Longitude, y=Latitude),fill='red',color='black',shape=21,size=4) +
geom_text_repel(data=subset(cwc.basins.centroids, CWC_basin %in% c('Godavari','Krishna','Cauvery')),
aes(x=long, y=lat, label=CWC_basin),
nudge_x = 70-subset(cwc.basins.centroids, CWC_basin %in% c('Godavari','Krishna','Cauvery'))$long,
nudge_y = -0.5,
size = 6,
segment.size = 0.75,
segment.color = 'black',
direction = 'y',
hjust = 1) +
geom_text_repel(data=subset(cwc.basins.centroids, CWC_basin %in% c('Subernarekha','Narmada','Mahanadi')),
aes(x=long, y=lat, label=CWC_basin),
nudge_x = -1.5,
nudge_y = 24.5-subset(cwc.basins.centroids, CWC_basin %in% c('Subernarekha','Narmada','Mahanadi'))$lat,
size = 6,
segment.size = 0.75,
segment.color = 'black',
direction = 'x',
box.padding = 1.0,
vjust = 0) +
geom_text_repel(data=subset(cwc.basins.centroids, CWC_basin %in% c('Brahmani and Baitarni')),
aes(x=long, y=lat, label=CWC_basin),
nudge_x = -3.3,
nudge_y = 25.5-subset(cwc.basins.centroids, CWC_basin %in% c('Brahmani and Baitarni'))$lat,
size = 6,
segment.size = 0.75,
segment.color = 'black',
direction = 'x',
box.padding = 1.0,
vjust = 0) +
geom_text_repel(data=subset(plot.select.sta.loc, Station != 'Mandleshwar'),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = 2.6,
size = 6,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
direction = 'y',
hjust = 0) +
geom_text_repel(data=subset(plot.select.sta.loc, Station == 'Mandleshwar'),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = -4.0,
nudge_y = -1.8,
size = 6,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
hjust = 1) +
scale_fill_manual(values=color_pal_tmp) +
scale_x_continuous(limits=c(min(india.map$long),max(india.map$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(india.map$lat), max(india.map$lat)), expand = c(0, 0)) +
guides(fill=guide_legend(ncol=1, title=expression(paste(Mean~precipitation,' [',mm~yr^-1,']')))) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=18,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = c(0.84,0.25),
legend.justification = 'center',
legend.text = element_text(size=18,family='Helvetica'))
print(p2)
dev.off()
# ---------- Delete existing file ---
unlink('CWC_basins_South_India_3.png')
# Start pdf device driver for saving plots
png('CWC_basins_South_India_3.png', width = 700, height = 800)
p3 <- ggplot() +
geom_map(data=plot.select.sta.loc[,c('CWC_basin','range')],
aes(map_id=CWC_basin, fill=range), map=cwc.basins) +
geom_line(data=streamlines, aes(x=long,y=lat,group=group),color='dodgerblue', size=0.3) +
geom_line(data=maj_streamlines, aes(x=long,y=lat,group=group),color='blueviolet', size=1.2) +
geom_path(data=cwc.basins, aes(x=long,y=lat,group=group),color='black', size=0.75) +
geom_path(data=india.map, aes(x=long,y=lat,group=group),color='grey15', size=1.2) +
geom_point(data=plot.select.sta.loc, aes(x=Longitude, y=Latitude),fill='red',color='black',shape=21,size=4) +
geom_text_repel(data=subset(plot.select.sta.loc, Station != 'Mandleshwar'),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = 2.6,
size = 6,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
direction = 'y',
hjust = 0) +
geom_text_repel(data=subset(plot.select.sta.loc, Station == 'Mandleshwar'),
aes(x=Longitude, y=Latitude, label=Station),
nudge_x = -4.0,
nudge_y = -1.8,
size = 6,
color = 'red',
segment.size = 0.75,
segment.color = 'red',
hjust = 1) +
scale_fill_manual(values=color_pal_tmp) +
scale_x_continuous(limits=c(min(india.map$long),max(india.map$long)), expand = c(0, 0)) +
scale_y_continuous(limits=c(min(india.map$lat), max(india.map$lat)), expand = c(0, 0)) +
guides(fill=guide_legend(ncol=1, title=expression(paste(Mean~precipitation,' [',mm~yr^-1,']')))) +
theme_map() + # clean theme for displaying maps
theme(text = element_text(size=18,family='Helvetica'),
legend.key = element_blank(),
legend.key.size = unit(2, 'lines'),
legend.position = c(0.84,0.25),
legend.justification = 'center',
legend.text = element_text(size=18,family='Helvetica'))
print(p3)