-
Notifications
You must be signed in to change notification settings - Fork 0
/
ClassificationComparison.Rmd
808 lines (609 loc) · 27.7 KB
/
ClassificationComparison.Rmd
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
---
title: "MapBiomas Classification Comparison"
author: "James D.A. Millington"
date: "Nov 2019"
output: github_document
# html_document:
# keep_md: yes
# code_folding: hide
# df_print: paged
---
This document present code and analyses of pasture areas, meat production and pasture yield for all states 2001-2018, for three different classifications of the MapBiomas data both with and without disaggregation using planted area data.
See accompanying Excel file for the classifications. Input land cover data, including the classification and disaggregation processes, are created using the `ClassifyDisaggregateMap.r` script.
<details><summary>Show/Hide Code</summary>
<p>
```{r warning=F, message=F}
rm(list=ls())
packages <- c(
"tidyverse",
"raster",
"readxl", #for reading Excel sheets
"scales", #useful for ggplotting
"knitr",
"rasterVis", #more useful raster plotting
"cowplot" #useful for ggplotting
)
#use lapply to suppress all wanings: https://stackoverflow.com/a/46685042
invisible(lapply(packages, function(xxx) suppressMessages(require(xxx, character.only = TRUE,quietly=TRUE,warn.conflicts = FALSE))))
```
```{r warning=F, message=F}
#raster to xyz (with help from https://stackoverflow.com/a/19847419)
#sepcify input raster, whether nodata cells should be output, whether a unique cell ID should be added
#return is a matrix. note format is row (Y) then col (X)
extractXYZ <- function(raster, nodata = FALSE, addCellID = TRUE){
vals <- raster::extract(raster, 1:ncell(raster)) #specify raster otherwise dplyr used
xys <- rowColFromCell(raster,1:ncell(raster))
combine <- cbind(xys,vals)
if(addCellID){
combine <- cbind(1:length(combine[,1]), combine)
}
if(!nodata){
combine <- combine[!rowSums(!is.finite(combine)),] #from https://stackoverflow.com/a/15773560
}
return(combine)
}
getLCs <- function(data)
{
#calculates proportion of each LC in the muni (ignoring NAs, help from https://stackoverflow.com/a/44290753)
data %>%
group_by(muniID) %>%
dplyr::summarise(LC1 = round(sum(map == 1, na.rm = T) / sum(!is.na(map)), 3),
LC2 = round(sum(map == 2, na.rm = T) / sum(!is.na(map)), 3),
LC3 = round(sum(map == 3, na.rm = T) / sum(!is.na(map)), 3),
LC4 = round(sum(map == 4, na.rm = T) / sum(!is.na(map)), 3),
LC5 = round(sum(map == 5, na.rm = T) / sum(!is.na(map)), 3),
NonNAs = sum(!is.na(map)),
NAs = sum(is.na(map))
) -> LCs
return(LCs)
}
```
```{r results='hide', warning=F, messages=F}
#unzip(zipfile="Data/sim10_BRmunis_latlon_5km_2018-04-27.zip",files="sim10_BRmunis_latlon_5km_2018-04-27.asc",exdir="ASCII") # unzip file
munis.r <- raster("Data/BaseMaps/sim10_BRmunis_latlon_5km.asc")
#extract cell values to table format
munis.t <- extractXYZ(munis.r, addCellID = F)
munis.t <- as.data.frame(munis.t)
munis.t <- plyr::rename(munis.t, c("vals" = "muniID"))
```
```{r}
#Specify classifications and years to examine. Classifications should be the names of Sheets in the Classifications Excel file. Years should be between 2001 and 2018
#classifications to loop through
cls <- c("PastureA","PastureB","PastureC")
yrls <- seq(2001,2018,1)
```
```{r warning=F, message=F}
#lists to hold data tables
CData_ls <- vector('list', length(cls))
CDataW_ls <- vector('list', length(cls))
SDataW_ls <- vector('list', length(cls))
Stotals_ls <- vector('list', length(cls))
SDataW_Adj_ls <- vector('list', length(cls))
mapStack_ls <- vector('list', length(cls))
names(CData_ls) <- cls
names(CDataW_ls) <- cls
names(SDataW_ls) <- cls
names(Stotals_ls) <- cls
names(SDataW_Adj_ls) <- cls
names(mapStack_ls) <- cls
#i <- 1
#j <- 1
#loop over classifications
for(i in seq_along(cls)){
classification <- read_excel("Data/MapBiomas_CRAFTY_classifications_v4.xlsx", sheet = cls[i], range="B1:C28", col_names=T)
#reset mapStack for this Classification
mapStack <- stack()
#loop over years
for(j in seq_along(yrls)){
#read the classfied map to a raster
map <- raster(paste0("Data/Classified/LandCover",yrls[j],"_",cls[i],".asc"))
#add categories for later plotting etc. (see https://stackoverflow.com/a/37214431)
map <- ratify(map) #tell R that the map raster is categorical
rat <- levels(map)[[1]] #apply the levels (i.e. categories)
#not all classes may be present after classification, so conditionally construct labels
labs <- c()
if(1 %in% levels(map)[[1]]$ID) { labs <- c(labs, "Nature") }
if(2 %in% levels(map)[[1]]$ID) { labs <- c(labs, "OtherAgri") }
if(3 %in% levels(map)[[1]]$ID) { labs <- c(labs, "Agriculture") }
if(4 %in% levels(map)[[1]]$ID) { labs <- c(labs, "Other") }
if(5 %in% levels(map)[[1]]$ID) { labs <- c(labs, "Pasture") }
rat$landcover <- labs
levels(map) <- rat
#add to mapStack for later plotting
mapStack <- stack(map, mapStack)
#extract cell values to table format
map.t <- extractXYZ(map, addCellID = F)
map.t <- as.data.frame(map.t)
map.t <- plyr::rename(map.t, c("vals" = "map"))
#so need to join
map_munis <- left_join(as.data.frame(munis.t), as.data.frame(map.t), by = c("row" = "row", "col" = "col"))
#now summarise by muniID
lcs_map_munis <- getLCs(map_munis)
#convert cell counts to areas (km2) and add state id
map_areas_munis <- lcs_map_munis %>%
mutate(LC1area = round(LC1 * NonNAs) * 25) %>%
mutate(LC2area = round(LC2 * NonNAs) * 25) %>%
mutate(LC3area = round(LC3 * NonNAs) * 25) %>%
mutate(LC4area = round(LC4 * NonNAs) * 25) %>%
mutate(LC5area = round(LC5 * NonNAs) * 25) %>%
mutate(state = substr(muniID, 1, 2))
#drop original cell-count columns (work with area km2 from now on)
map_areas_munis <- map_areas_munis %>% dplyr::select(-LC1, -LC2, -LC3, -LC4, -LC5, -NonNAs, -NAs)
#summarise muni areas to state level
map_areas <- map_areas_munis %>%
group_by(state) %>%
dplyr::summarise_at(vars(LC1area:LC5area),sum, na.rm=T) %>% #use _at so state is not summarised
mutate_if(is.character, as.integer)
#gather to long format for union below
map_areas <- map_areas %>%
gather(key = LCa, value = area, -state)
#recode LCs for union below
map_areas <- map_areas %>%
mutate(LC = if_else(LCa == "LC1area", 1,
if_else(LCa == "LC2area", 2,
if_else(LCa == "LC3area", 3,
if_else(LCa == "LC4area", 4,
if_else(LCa == "LC5area", 5, 0)
)))))
#add source variable for plotting below (re-order to match map table for union below)
map_areas <- map_areas %>%
dplyr::select(-LCa) %>%
mutate(source = "Map") %>%
dplyr::select(state, LC, source, area)
#relabel states to characters
CData_yr <- map_areas %>%
mutate(state = if_else(state == 17, "TO",
if_else(state == 29, "BA",
if_else(state == 31, "MG",
if_else(state == 35, "SP",
if_else(state == 41, "PR",
if_else(state == 42, "SC",
if_else(state == 43, "RS",
if_else(state == 50, "MS",
if_else(state == 51, "MT",
if_else(state == 52, "GO", "NA"
))))))))))
)
#relabel LCs to characters
CData_yr <- CData_yr %>%
mutate(LC = if_else(LC == 1, "Nature",
if_else(LC == 2, "OtherAgri",
if_else(LC == 3, "Agri",
if_else(LC == 4, "Other",
if_else(LC == 5, "Pasture", "NA"
)))))
)
#add year column
CData_yr <- CData_yr %>%
mutate(year = yrls[j])
#union CData for years here.
#if first iteration of classification loop (re)create the tibble
if(j == 1){
CData <- CData_yr
} else {
#else join data to tibble (by creating another tibble, then join (ensure rows are not lost)
CData <- union_all(CData, CData_yr)
}
}
CData_ls[[i]] <- CData
names(mapStack) <- yrls
mapStack_ls[[i]] <- mapStack
}
```
</p>
</details>
# Pre-Disaggregation Classiifcation
## Maps
First, lets look at maps for the different classifications (for the final year) for quick visual comparison.
```{r fig.width=12, warning=F, message=F}
for(i in seq_along(mapStack_ls)){
clabs <- c()
if(1 %in% levels(mapStack_ls[[i]])[[1]]$ID) { clabs <- c(clabs, 'forestgreen') }
if(2 %in% levels(mapStack_ls[[i]])[[1]]$ID) { clabs <- c(clabs, 'darkolivegreen') }
if(3 %in% levels(mapStack_ls[[i]])[[1]]$ID) { clabs <- c(clabs, 'wheat1') }
if(4 %in% levels(mapStack_ls[[i]])[[1]]$ID) { clabs <- c(clabs, 'gray') }
if(5 %in% levels(mapStack_ls[[i]])[[1]]$ID) { clabs <- c(clabs, 'orange2') }
#print(cls[i])
#length(yrls) is the index of the final year
#tail(yrls, n=1) is the 'label' for the final year
print(rasterVis::levelplot(mapStack_ls[[i]][[length(yrls)]], pretty=T,att = 'landcover', col.regions=clabs, main=paste(cls[[i]], tail(yrls, n=1))))
}
```
From these maps, we can immediately see that _PastureA_ has much less Pasture cover than the other two classifications.In particular, in the southern state of Rio Grande do Sul (RS) _PastureA_ has much less Pasture areas as much of the state is classified as Grassland. The Grassland class is significant in Rio Grande do Sul because it is within the Pampa biome and has seen a long history of intense efforts by public and private groups to increase the (pasture) productivity of the grasslands.
As we will see below, this difference in classified land area leads to great differences in calculate pasture yields in RS. Other study area states have relatively small areas of grassland so are less affected by the inclusion, although Santa Catarina (a small state on the edge of the Pampa biome) is next most influenced.
Let's examine these differences between the classifications further quantitatively, looking at both total area and the yield implied when compared to production data.
## Pasture area by state, year and classification
The figure below shows again that generally the _PastureA_ classification produces much less Pasture land area.
<details><summary>Show/Hide Code</summary>
<p>
```{r warning=F, message=F}
#add classification lable to the CData tables
for(i in seq_along(cls)){
CData_ls[[i]] <- CData_ls[[i]] %>%
mutate(classification = cls[i])
}
#union the CData tables for the different classifications
for(i in seq_along(cls)){
if(i == 1) { CDataU <- CData_ls[[i]] }
else { CDataU <- dplyr::union(CDataU, CData_ls[[i]]) }
}
#filter to pasture only
CData_Pas <- CDataU %>%
dplyr::filter(LC == "Pasture")
```
</p>
</details>
```{r fig.height=20, fig.width=12}
CData_Pas %>%
ggplot(aes(x=classification, y=area, fill=classification)) +
geom_bar(stat="identity", colour="white", position = "dodge") +
scale_y_continuous(name=expression(Area~km^{2}), labels = scales::comma) +
facet_grid(year~state) +
xlab("") +
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Area by Classification and State")
```
## Pasture Yield by state, year and classification
This first plot below shows that _PastureA_ for Rio Grande do Sul has vastly greater pasture yield than all other classification-state combinations.
<details><summary>Show/Hide Code</summary>
<p>
```{r warning=F, message=F}
#Load Production Data
meat_prod_Astates <- read_excel("Data/Cattle_meat_production_Kg_2001_2018_all_states.xlsx", sheet = "Plan1", skip = 1) #data for all states Astates
```
```{r echo=F, warning=F, message=F}
#Tidy production data
#tidy meat data
meat_prod_Astates %>%
rename(state = NM_UF_SIGLA) %>%
dplyr::select(-NM_UF, -CD_GCUF) %>% #drop columns
filter(!is.na(state)) %>% #safer way to remove text line at bottom of state column
mutate_at(vars("2001":"2018"), as.numeric) -> meat_prod_Astates
Fstate_vals <- c(17, 29, 31, 35, 41, 42, 43, 50, 51, 52)
Fstate_abbrev <- c("TO", "BA", "MG", "SP", "PR", "SC", "RS", "MS", "MT", "GO")
#Filter meat focal states
meat_prod_Astates %>%
filter(state %in% Fstate_abbrev) -> meat_prod_Fstates
meat_prod_Fstates_long <- meat_prod_Fstates %>%
gather(key = year, value = kg, -state) %>%
mutate_at(vars(year), as.integer)
```
```{r}
#join to pasture areas
meat_areas <- inner_join(CData_Pas, meat_prod_Fstates_long, by = c("year", "state"))
#calculate intensities (yields)
meat_areas <- meat_areas %>%
mutate(intensity = kg / area)
```
</p>
</details>
```{r fig.height=20, fig.width=12}
meat_areas %>%
ggplot(aes(x=classification, y=intensity, fill=classification)) +
geom_bar(stat="identity", colour="white", position = "dodge") +
scale_y_continuous(name=expression(Yield~kg~km^{2})) +
facet_grid(year~state) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield by Classification and State")
```
In fact, values are so large it is difficult to see variation for other states in this plot because of the y-axis scale, so let's repeat this plot but limiting the upper boundary of the yield plotted (to 15,000 kg km-2).
```{r fig.height=20, fig.width=12}
meat_areas %>%
ggplot(aes(x=classification, y=intensity, fill=classification)) +
geom_bar(stat="identity", colour="white", position = "dodge") +
scale_y_continuous(name=expression(Yield~kg~km^{2}),limits = c(0, 15000)) +
facet_grid(year~state) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield by Classification and State (limited at 15,000 kg km-2")
```
Where bars are missing in the plot above, this is because the value for that bar is greater than 15,000. Now we can see more clearly, that in other states _PastureA_ is also generally has the highest yield compared to the other classifications.
## Summary stats over time
Now let's summarise the data over time and examine their means and medians (with variation - error bar is one SE) and maxima during 2001-2018.
<details><summary>Show/Hide Code</summary>
<p>
```{r echo=F}
meat_summary <- meat_areas %>%
group_by(state, classification, source) %>%
summarise(
area_mn = round(mean(area),0),
area_md = round(median(area),0),
area_sd = round(sd(area),0),
prod_mn = round(mean(kg),0),
prod_md = round(median(kg),0),
prod_sd = round(sd(kg),0),
int_mn = round(mean(intensity),0),
int_md = round(median(intensity),0),
int_max = round(max(intensity),0),
int_sd = round(sd(intensity),0),
int_se = round(sd(intensity) / length(intensity), 3)
)
```
</p>
</details>
```{r fig.width=8}
meat_summary %>%
ggplot(aes(x=classification, y=int_mn, fill=classification)) +
geom_errorbar(aes(ymin=int_mn-int_se, ymax=int_mn+int_se), width=.1) +
geom_bar(stat="identity", colour="white", position = "dodge") +
facet_grid(.~state) +
scale_y_continuous(name=expression(Yield~kg~km^{2}), limits = c(0, 10000), labels = scales::comma) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield, Mean")
meat_summary %>%
ggplot(aes(x=classification, y=int_md, fill=classification)) +
geom_errorbar(aes(ymin=int_md-int_se, ymax=int_md+int_se), width=.1) +
geom_bar(stat="identity", colour="white", position = "dodge") +
facet_grid(.~state) +
scale_y_continuous(name=expression(Yield~kg~km^{2}), limits = c(0, 10000), labels = scales::comma) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield, Median")
meat_summary %>%
ggplot(aes(x=classification, y=int_max, fill=classification)) +
geom_bar(stat="identity", colour="white", position = "dodge") +
facet_grid(.~state) +
scale_y_continuous(name=expression(Yield~kg~km^{2}), limits = c(0, 15000), labels = scales::comma) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield, Maxima")
```
In these three plots, we again we see the same pattern; _PastureA_ produces greater yields than _PastureB_ and _PastureC_ (except for Sao Paolo state).
But how do these yields compare to observed and projected yields? The table below presents yields by state and classification (sorted descending on the maximum).
```{r}
int_data <- meat_summary %>%
dplyr::select(-starts_with("prod"), -starts_with("area"))
kable(arrange(int_data, desc(int_max), state, classification), caption="Intensities, sorted on max values descending")
```
We can also calculate the mean state maximum across all years for each of the classifications:
```{r}
meat_summary %>%
dplyr::select(-starts_with("prod"), -int_sd, -int_se) %>%
group_by(classification) %>%
summarise_at(vars(int_max), mean)
```
Largest values for _PastureB_ and _PastureC_ seem reasonable and align well with expected future yields (important to have a max contemporary yield that is feasible in future, to be able to run the model into the future). For example, [this report](http://csr.ufmg.br/pecuaria/portfolio-item/cenarios-para-o-brasil/) expects yields of 8,730kg/km2 in 2030 if the Brazilian sector continues to modernize (in "Produção de @/hectare" @ is equivalent to 15kg). This potential aligns well with the maximum previously observed intensity of 9,100kg/km2 (which is likely an outlier given remaining uncertainty in land cover classification). Ultimately, the _PastureA_ classification produces yield values which are just not feasible relatively to observed values.
# Post-Disaggregation Classiifcation
Now that we have discounted the _PastureA_ classification, let's examine _PastureB_ and _PastureC_ in more detail after they have been disaggregated using planted area data (see `ClassifyDisaggregateMap.r`). We'll repeat much of the above analysis, but using disaggregated maps as input.
<details><summary>Show/Hide Code</summary>
<p>
```{r}
#Specify classifications and years to examine. Classifications should be the names of Sheets in the Classifications Excel file. Years should be between 2001 and 2018
#classifications to loop through
cls <- c("PastureB","PastureC")
yrls <- seq(2001,2018,1)
```
```{r warning=F, message=F}
#lists to hold data tables
CData_ls <- vector('list', length(cls))
CDataW_ls <- vector('list', length(cls))
SDataW_ls <- vector('list', length(cls))
Stotals_ls <- vector('list', length(cls))
SDataW_Adj_ls <- vector('list', length(cls))
mapStack_ls <- vector('list', length(cls))
names(CData_ls) <- cls
names(CDataW_ls) <- cls
names(SDataW_ls) <- cls
names(Stotals_ls) <- cls
names(SDataW_Adj_ls) <- cls
names(mapStack_ls) <- cls
#i <- 1
#j <- 1
#loop over classifications
for(i in seq_along(cls)){
classification <- read_excel("Data/MapBiomas_CRAFTY_classifications_v4.xlsx", sheet = cls[i], range="B1:C28", col_names=T)
#reset mapStack for this Classification
mapStack <- stack()
#loop over years
for(j in seq_along(yrls)){
#read the classfied map to a raster
map <- raster(paste0("Data/Classified/LandCover",yrls[j],"_",cls[i],"_Disagg.asc"))
#add categories for later plotting etc. (see https://stackoverflow.com/a/37214431)
map <- ratify(map) #tell R that the map raster is categorical
rat <- levels(map)[[1]] #apply the levels (i.e. categories)
#not all classes may be present after classification, so conditionally construct labels
labs <- c()
if(1 %in% levels(map)[[1]]$ID) { labs <- c(labs, "Nature") }
if(2 %in% levels(map)[[1]]$ID) { labs <- c(labs, "OtherAgri") }
if(3 %in% levels(map)[[1]]$ID) { labs <- c(labs, "Agriculture") }
if(4 %in% levels(map)[[1]]$ID) { labs <- c(labs, "Other") }
if(5 %in% levels(map)[[1]]$ID) { labs <- c(labs, "Pasture") }
rat$landcover <- labs
levels(map) <- rat
#add to mapStack for later plotting
mapStack <- stack(map, mapStack)
#extract cell values to table format
map.t <- extractXYZ(map, addCellID = F)
map.t <- as.data.frame(map.t)
map.t <- plyr::rename(map.t, c("vals" = "map"))
#so need to join
map_munis <- left_join(as.data.frame(munis.t), as.data.frame(map.t), by = c("row" = "row", "col" = "col"))
#now summarise by muniID
lcs_map_munis <- getLCs(map_munis)
#convert cell counts to areas (km2) and add state id
map_areas_munis <- lcs_map_munis %>%
mutate(LC1area = round(LC1 * NonNAs) * 25) %>%
mutate(LC2area = round(LC2 * NonNAs) * 25) %>%
mutate(LC3area = round(LC3 * NonNAs) * 25) %>%
mutate(LC4area = round(LC4 * NonNAs) * 25) %>%
mutate(LC5area = round(LC5 * NonNAs) * 25) %>%
mutate(state = substr(muniID, 1, 2))
#drop original cell-count columns (work with area km2 from now on)
map_areas_munis <- map_areas_munis %>% dplyr::select(-LC1, -LC2, -LC3, -LC4, -LC5, -NonNAs, -NAs)
#summarise muni areas to state level
map_areas <- map_areas_munis %>%
group_by(state) %>%
dplyr::summarise_at(vars(LC1area:LC5area),sum, na.rm=T) %>% #use _at so state is not summarised
mutate_if(is.character, as.integer)
#gather to long format for union below
map_areas <- map_areas %>%
gather(key = LCa, value = area, -state)
#recode LCs for union below
map_areas <- map_areas %>%
mutate(LC = if_else(LCa == "LC1area", 1,
if_else(LCa == "LC2area", 2,
if_else(LCa == "LC3area", 3,
if_else(LCa == "LC4area", 4,
if_else(LCa == "LC5area", 5, 0)
)))))
#add source variable for plotting below (re-order to match map table for union below)
map_areas <- map_areas %>%
dplyr::select(-LCa) %>%
mutate(source = "Map") %>%
dplyr::select(state, LC, source, area)
#relabel states to characters
CData_yr <- map_areas %>%
mutate(state = if_else(state == 17, "TO",
if_else(state == 29, "BA",
if_else(state == 31, "MG",
if_else(state == 35, "SP",
if_else(state == 41, "PR",
if_else(state == 42, "SC",
if_else(state == 43, "RS",
if_else(state == 50, "MS",
if_else(state == 51, "MT",
if_else(state == 52, "GO", "NA"
))))))))))
)
#relabel LCs to characters
CData_yr <- CData_yr %>%
mutate(LC = if_else(LC == 1, "Nature",
if_else(LC == 2, "OtherAgri",
if_else(LC == 3, "Agri",
if_else(LC == 4, "Other",
if_else(LC == 5, "Pasture", "NA"
)))))
)
#add year column
CData_yr <- CData_yr %>%
mutate(year = yrls[j])
#union CData for years here.
#if first iteration of classification loop (re)create the tibble
if(j == 1){
CData <- CData_yr
} else {
#else join data to tibble (by creating another tibble, then join (ensure rows are not lost)
CData <- union_all(CData, CData_yr)
}
}
CData_ls[[i]] <- CData
names(mapStack) <- yrls
mapStack_ls[[i]] <- mapStack
}
```
```{r warning=F, message=F}
#add classification lable to the CData tables
for(i in seq_along(cls)){
CData_ls[[i]] <- CData_ls[[i]] %>%
mutate(classification = cls[i])
}
#union the CData tables for the different classifications
for(i in seq_along(cls)){
if(i == 1) { CDataU <- CData_ls[[i]] }
else { CDataU <- dplyr::union(CDataU, CData_ls[[i]]) }
}
#filter to pasture only
CData_Pas <- CDataU %>%
dplyr::filter(LC == "Pasture")
```
```{r}
#join to pasture areas
meat_areas <- inner_join(CData_Pas, meat_prod_Fstates_long, by = c("year", "state"))
#calculate intensities (yields)
meat_areas <- meat_areas %>%
mutate(intensity = kg / area)
```
</p>
</details>
## Pasture Yield by state, year and classification
We see that in general there is little difference between the two classifications; Santa Catarina state has possibly the largest differences with _PastureB_ resulting in greater Pasture yields.
```{r fig.height=20, fig.width=12}
meat_areas %>%
#filter(source == "Map") %>%
ggplot(aes(x=classification, y=intensity, fill=classification)) +
geom_bar(stat="identity", colour="white", position = "dodge") +
scale_y_continuous(name=expression(Yield~kg~km^{2})) +
facet_grid(year~state) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield by Classification and State")
```
## Summary stats over time
Now let's summarise the data over time again, and examine their means and medians (with variation - error bar is one SE) and maxima during 2001-2018.
<details><summary>Show/Hide Code</summary>
<p>
```{r echo=F}
meat_summary <- meat_areas %>%
group_by(state, classification, source) %>%
summarise(
area_mn = round(mean(area),0),
area_md = round(median(area),0),
area_sd = round(sd(area),0),
prod_mn = round(mean(kg),0),
prod_md = round(median(kg),0),
prod_sd = round(sd(kg),0),
int_mn = round(mean(intensity),0),
int_md = round(median(intensity),0),
int_max = round(max(intensity),0),
int_sd = round(sd(intensity),0),
int_se = round(sd(intensity) / length(intensity), 3)
)
```
</p>
</details>
```{r fig.width=8}
meat_summary %>%
#filter(source == "MB") %>%
ggplot(aes(x=classification, y=int_mn, fill=classification)) +
geom_errorbar(aes(ymin=int_mn-int_se, ymax=int_mn+int_se), width=.1) +
geom_bar(stat="identity", colour="white", position = "dodge") +
facet_grid(.~state) +
scale_y_continuous(name=expression(Yield~kg~km^{2}), limits = c(0, 10000), labels = scales::comma) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield, Mean")
meat_summary %>%
#filter(source == "Map") %>%
ggplot(aes(x=classification, y=int_md, fill=classification)) +
geom_errorbar(aes(ymin=int_md-int_se, ymax=int_md+int_se), width=.1) +
geom_bar(stat="identity", colour="white", position = "dodge") +
facet_grid(.~state) +
scale_y_continuous(name=expression(Yield~kg~km^{2}), limits = c(0, 10000), labels = scales::comma) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield, Median")
meat_summary %>%
#filter(source == "Map") %>%
ggplot(aes(x=classification, y=int_max, fill=classification)) +
#geom_errorbar(aes(ymin=int_md-int_se, ymax=int_md+int_se), width=.1) +
geom_bar(stat="identity", colour="white", position = "dodge") +
facet_grid(.~state) +
scale_y_continuous(name=expression(Yield~kg~km^{2}), limits = c(0, 10000), labels = scales::comma) +
xlab("")+
theme(axis.text.x = element_blank()) +
ggtitle("Pasture Yield, Maxima")
```
As above, when looking at summaries we see that the two classifications are generally similar, but with _PastureB_ producing more land area, notably for Santa Catarina, Sao Paolo and Parana.
Let's look again at yields by state and classification (sorted descending on the maximum).
```{r}
int_data <- meat_summary %>%
#filter(source == "Map") %>%
dplyr::select(-starts_with("prod"), -starts_with("area")) #%>%
#filter(state != "SC")
kable(arrange(int_data, desc(int_max), state, classification), caption="Intensities, sorted on max values descending")
```
And the mean state maximum across all years for each of the classifications:
```{r}
meat_summary %>%
#filter(source == "Map" & classification == "PastureB") %>%
dplyr::select(-starts_with("prod"), -int_sd, -int_se) %>%
#filter(state != "SC") %>%
group_by(classification) %>%
summarise_at(vars(int_max), mean)
```
Yield values are generally quite similar to those calculated for non-disaggregated maps.
So, overall there is not much difference between the two classifications. We we will chose the _PastureB_ classification as it seems to make more sense to include the MapBiomas _Mosaic of Agriculture and Pasture_ class as Agriculture prior to disaggregation (given the disaggregation process used).
## CRAFTY Service Provision
Having chosem the _PastureB_ classification, we now need to identify the some indication of 'perfect' yield in our data to enable conversion of meat production (in kg) to CRAFTY service production units. We could use the maximum yield observed in our data (~9,100 kg/sq km) but to allow room for future scenarios of extraordinary continued yield improvement, we will use a higher value of 11,000 kg/sq km. In turn, 11,000 kg/sq km == **0.275 gg/25sq km** We will use this value as a single unit of of 'Pasture service' from CRAFTY.