-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcode.Rmd
1657 lines (1379 loc) · 70 KB
/
code.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
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
--
title: Report on data in AusTraits
output:
html_document:
df_print: kable
highlight: tango
keep_md: no
smart: no
theme: yeti
toc: yes
toc_depth: 2
toc_float:
collapsed: false
smooth_scroll: true
editor_options:
chunk_output_type: console
---
# Setup
Create output directories if necessary. If exists just ignore.
```{r}
dir.create("output/manuscript/figures", recursive = TRUE)
dir.create("output/manuscript/supps_figures", recursive = TRUE)
dir.create("output/manuscript/tables", recursive = TRUE)
dir.create("output/manuscript/supps_tables", recursive = TRUE)
dir.create("output/traits_by_dataset", recursive = TRUE)
dir.create("data/climate_data/Envirem", recursive = TRUE)
dir.create("data/climate_data/VPD_Chelsa", recursive = TRUE)
dir.create("data/climate_data/wc2.1_30s_bio", recursive = TRUE)
```
Load packages
```{r, warning=FALSE, message=FALSE}
# remotes::install_github("valentinitnelav/plotbiomes")
library(plotbiomes)
# remotes::install_github("eliocamp/tagger")
library(tagger)
# remotes::install_github("traitecoevo/austraits", dependencies = TRUE, upgrade = "ask")
library(austraits)
library(xtable)
library(lubridate)
library(R.utils)
library(tidyverse)
library(corrplot)
library(ggpointdensity)
library(relaimpo)
library(terra)
library(GGally)
library(effects)
library(mgcv)
library(tidymv)
library(ggnewscale)
library(ggrepel)
library(raster)
library(tidyterra)
```
Load data and user-defined functions from files
```{r, warning=FALSE, message=FALSE}
#functions to run assessments of data entry (austraits already goes through cleaning protocols)
source("R/error_checking_functions/error_checking_function.R")
#climate extraction functions
source("R/climate_extraction_functions/load_climate_data.R")
#plotting functions
source("R/plotting_functions/plot_whittaker_biomes.R")
source("R/plotting_functions/plot_climate.R")
source("R/plotting_functions/plot_trait_by_dataset.R")
source("R/plotting_functions/ggpairs_modified.R")
```
# Data processing
Load in trait data from Austraits (using version 4.1.0 at the time of writing)
```{r, warning=FALSE, message=FALSE}
#austraits::get_versions()
#austraits <- austraits::load_austraits(version = "4.1.0")
austraits <- readRDS("data/austraits/austraits-4.1.0.rds")
```
Filter Austraits by the focal traits including some accessory traits like "leaflet_area" and "leaf_N_per_dry_mass" which will eventually become "leaf_area" and "leaf_N_per_area" at a later point and convert to numeric values (necessary because there are also character-based traits in AusTraits).
```{r}
traits <- austraits$traits %>%
filter(trait_name %in% c("leaflet_area","leaf_area","leaf_mass_per_area","leaf_N_per_dry_mass","leaf_N_per_area","wood_density","plant_height", "seed_dry_mass", "leaf_delta13C","huber_value")) %>%
mutate(value = as.numeric(value))
```
Remove some unnecessary columns which will improve simplicity of the data and help in the long run with respect to pivoting
```{r}
traits %>%
dplyr::select(-collection_date, -measurement_remarks, -original_name, -unit) -> traits
```
Inspect the basis of record options. We only want data from "field" experiments as these represent naturally-occurring organisms without experimental treatments. We can also extract from "field, field_experiment", which combines both, provided that the method_id allows us to determine which data were not affected by a treatment.
```{r}
traits %>% pull(basis_of_record) %>% unique()
```
Filter as above. Cheesman_2020 is the only noted field, field experiment. Removing Cheesman obs in experimental contects removes 122 obs.
```{r}
austraits$contexts %>% filter(dataset_id == "Cheesman_2020")
traits %>%
filter(basis_of_record %in% c("field", "field, field_experiment")) %>%
filter(!(dataset_id == "Cheesman_2020" & (method_id %in% c("03","04") | treatment_id %in% c("03")))) -> field_traits
```
We also remove all literature based values and also values which are recorded as either metapopulations or species. Except for plant height, which we consider to be appropraite to use maximum, species-level values. Removing observations matching these conditions removes 805 obs.
```{r}
field_traits$basis_of_value %>% table()
field_traits$entity_type %>% table()
field_traits %>%
filter(basis_of_value %in% c("measurement")) %>%
filter(entity_type %in% c("individual","population")|trait_name == "plant_height")-> field_traits
```
For leaf area, we focus on analysis the lamina area of leaflets in the case of compound species. Thus, we must filter the leaf area data on the basis that IF a compound species was analysed, it must have been measured at the leaflet scale. The protocol is as follows: 1) determine if a study includes compound species, 2) if the study includes compound species, determine if that study measured on compound species leaves or leaflet area, 3) if compound and leaf, remove just those observations, or if compound and leaflet, retain those observations. We identified whether leaf or leaflet was measured on compound species by either reading the methods included in Austraits, contacting the authors personally, or if this information was not available, assessing the leaf area of compound taxa from online sources and comparing to the Austraits data. Our decisions and supporting evidence are included in "data/leaf_compound_filter/datasets_with_leaf_area_edited.csv" which is created below.
First, determine which taxa are compound using the "leaf_compoundness" dataset in Austraits
```{r}
austraits$traits %>%
filter(trait_name == "leaf_compoundness") %>%
group_by(taxon_name) %>%
mutate(num_types = n_distinct(value)) %>%
ungroup() %>%
mutate(compoundness = if_else(num_types > 1 | value =="compound" | value == "compound simple", "compound", "simple")) %>%
dplyr::select(-value) %>%
distinct(taxon_name, .keep_all = T) %>%
dplyr::select(compoundness, taxon_name) -> compound_taxa
```
Create a dataframe to fill in where each row is a different study and, for the studies which have either compound taxa or taxa which the compoundness is unknown, identify the method used to measure leaf area on compound taxa.
```{r}
field_traits %>%
filter(trait_name == "leaf_area") %>%
left_join(compound_taxa) %>%
group_by(dataset_id) %>%
mutate(compound_species = if_else(any(is.na(compoundness)) |any(compoundness == "compound") , "compound_present","no_compound")) %>%
distinct(dataset_id, .keep_all = TRUE) %>%
dplyr::select(dataset_id, compound_species) %>%
ungroup() %>%
mutate(leaf_unit_measured = NA,
evidence = NA,
notes = NA) %>%
write_csv("data/leaf_compound_filter/datasets_with_leaf_area.csv")
```
Read in the now filled-in "data/leaf_compound_filter/datasets_with_leaf_area_edited.csv". There are four studies out of 71 for which it was unclear whether the study assessed compound species at the leaflet or leaf scale and thus we conservatively assume that compound species were measured at leaf scale. Importantly, this means that observations emerging from only compound or unknown species are removed from just these datasets (so not many at all)
```{r}
datasets_with_leaf_area <- read_csv("data/leaf_compound_filter/datasets_with_leaf_area_edited.csv")
```
Filter the traits data down to just leaf area and left join the leaf area simple/compound assessment.
```{r}
field_traits %>%
filter(trait_name %in% c("leaf_area")) %>%
left_join(datasets_with_leaf_area) -> field_traits_leaf_area
```
Identify compound taxa using Austraits on the basis that taxa must be ONLY simple.
Left join the extracted compoundness information to the leaf area dataset and remove all obs that are in datasets which had compound species recorded, measured leaf area on compound taxa at the leaf scale, and were either compound or unknown. It is important to retain the compound_species == "compound_present" filter here, because we can keep observations on taxa with unknown compoundness provided that we know that the study was recording at the leaflet scale. For studies which the scale at which measurement occurred is unknown, all compound or unknown leaf type observations are removed. The process below removes 2,016 observations out of 17,444 (approx 10%).
```{r}
field_traits_leaf_area %>%
left_join(compound_taxa) %>%
filter(!(compound_species == "compound_present" & leaf_unit_measured == "leaf" & compoundness %in% c(NA,"compound"))) %>%
dplyr::select(-compound_species, -leaf_unit_measured, -evidence, -notes, -compoundness) -> field_traits_leaf_area_compound_removed
```
Remove leaf area observations from the original data set and add on the filtered leaf area dataset
```{r}
field_traits %>%
filter(!trait_name %in% c("leaf_area")) %>%
bind_rows(field_traits_leaf_area_compound_removed) -> field_traits
```
Also account for studies which reported both leaflet and leaf area for the same taxa. For these taxa, we must remove the leaf area observations for compound species and replace with leaflet areas.
```{r}
field_traits %>%
filter(trait_name == "leaflet_area") %>%
filter(dataset_id == "Wells_2012") %>%
distinct(taxon_name) -> compound_wells
field_traits %>%
filter(!(trait_name == "leaf_area" & dataset_id == "Wells_2012" & taxon_name %in% compound_wells$taxon_name)) %>%
mutate(trait_name = ifelse(trait_name == "leaflet_area", "leaf_area",trait_name))-> field_traits
```
Now onto the woodiness categories. We use the woodiness detailed trait collated by Wenk_2022 for most taxa in Austraits, to determine woody or non-woody tax as well as woody-like taxa.
```{r}
austraits$traits %>%
filter(trait_name %in% c("woodiness_detailed")) %>%
filter(dataset_id == "Wenk_2022") %>%
filter(value %in% c("woody")) %>%
pull(taxon_name) %>%
unique() -> woody_taxa
austraits$traits %>%
filter(trait_name %in% c("woodiness_detailed")) %>%
filter(dataset_id == "Wenk_2022") %>%
filter(!value %in% c("woody")) %>%
pull(taxon_name) %>%
unique() -> non_woody_taxa
austraits$traits %>%
filter(trait_name %in% c("woodiness_detailed")) %>%
filter(dataset_id == "Wenk_2022") %>%
filter(value %in% c("woody_like_stem")) %>%
pull(taxon_name) %>%
unique() -> woody_like_taxa
austraits$traits %>%
filter(trait_name %in% c("woodiness_detailed")) %>%
filter(dataset_id == "Wenk_2022") %>%
filter(value != c("woody_like_stem")) %>%
pull(taxon_name) %>%
unique() -> non_woody_like_taxa
```
Make growth form data a nested dataframe based on above info. Taxa without woodiness information get NA for both growth_form and woody_like taxa.
```{r}
field_traits %>%
mutate(growth_form = case_when(taxon_name %in% woody_taxa ~ "Woody",
taxon_name %in% non_woody_taxa ~ "Non-woody"),
woody_like_taxa = case_when(taxon_name %in% woody_like_taxa ~ "Woody",
taxon_name %in% non_woody_like_taxa ~ "Non-woody")) %>%
group_by(taxon_name, growth_form, woody_like_taxa) %>%
nest() %>%
group_by(taxon_name) %>%
nest(growth_form_data = c(growth_form, woody_like_taxa)) %>%
unnest(data) -> woody_field_traits
```
Now that we are finished filtering by different aspects of entity, we can further remove some columns to improve simplicity of data. We need all _id to remain however, as these are important for grouping data in terms of observations.
```{r}
woody_field_traits %>%
dplyr::select(-value_type, -basis_of_value, -replicates, -basis_of_record, -life_stage) -> woody_field_traits
```
We can also extend the Narea dataset by combining reported balues of Leaf N per dry mass and leaf mass per area from the same populations or individuals (in cases where Narea was not already reported.).
```{r}
woody_field_traits %>%
filter(trait_name %in% c("leaf_N_per_dry_mass", "leaf_mass_per_area", "leaf_N_per_area")) %>%
mutate(value = as.numeric(value)) %>%
# dplyr::select(-unit) %>%
#this grouping is equivalent to number of rows
# group_by(observation_id, taxon_name, trait_name, dataset_id, growth_form_data, location_id, temporal_id, method_id)%>%
# nest(.key = "value") %>%
# #find mean just in case, but should be the same values as above
# mutate(value = map_dbl(value, ~mean(.x$value))) %>%
ungroup() %>%
#wright_2019 has two seperate collection methods for N mass and lma, unify methods so that LMA and Nmass can be combined
mutate(method_id = if_else(dataset_id == "Wright_2019" & trait_name == "leaf_N_per_dry_mass" & method_id == "03", "02", method_id)) %>%
mutate(method_id = if_else(dataset_id == "Wright_2019" & trait_name == "leaf_N_per_dry_mass" & method_id == "04", "01", method_id)) %>%
pivot_wider(names_from = "trait_name", values_from = "value") %>%
mutate(leaf_N_per_dry_mass = leaf_N_per_dry_mass/1000) %>%
#If Narea does not exist, add a value based on Nmass*lma
mutate(leaf_N_per_area= if_else(!is.na(leaf_N_per_area),
leaf_N_per_area,
leaf_N_per_dry_mass * leaf_mass_per_area)) %>%
dplyr::select(-leaf_N_per_dry_mass, -leaf_mass_per_area) %>%
drop_na(leaf_N_per_area) %>%
pivot_longer(cols = leaf_N_per_area, names_to = "trait_name") %>%
mutate(trait_name = "leaf_N_per_area_calc") -> calculated_leaf_N_data
#bind new values onto the original dataset
woody_field_traits %>%
bind_rows(calculated_leaf_N_data) -> woody_field_traits
```
Do some error checking by comparing observations from different studies of the same taxa against each other in terms of data variance or min/max.
Error checking for leaf mass per area
```{r}
error_check(woody_field_traits, "leaf_mass_per_area") -> error_check_lma
```
Error checking for huber value
```{r}
error_check(woody_field_traits, "huber_value") -> error_check_huber
```
Error checking for leaf N per area
```{r}
error_check(woody_field_traits, "leaf_N_per_area_calc") -> error_check_leaf_N
```
Error checking for wood density - no problematic data points detected
```{r}
error_check(woody_field_traits, "wood_density") -> error_check_wood_density
```
Error checking for plant height - no problematic data points detected
```{r}
error_check(woody_field_traits, "plant_height") -> error_check_plant_height
```
Error checking for leaf_delta13C - no problematic points detected
```{r}
error_check(woody_field_traits, "leaf_delta13C") -> error_check_13C
```
Error checking for seed mass
```{r}
error_check(woody_field_traits, "seed_dry_mass") -> error_check_seed_mass
```
Error checking for leaf area
```{r}
error_check(woody_field_traits, "leaf_area") -> error_check_leaf_area
```
To see if there any instances where data is shared between studies, we inspect occurrences of identical trait values for each trait~species group. Although there are a number of cases where this occurs, most seem to be coincidental. Jurada_1991 and Leishman_1992 appear to be related but are not entirely duplicated. In any case, duplicates have been carefully checked as indicated on the github for Austraits: https://github.com/traitecoevo/austraits.build/issues/156
```{r}
woody_field_traits %>%
group_by(value, taxon_name, trait_name) %>%
filter(n()>1) %>%
filter(n_distinct(dataset_id)>1) -> identical_values
```
Based on error checking above, we can now remove erroneous values, which have been recorded in "erroneous_values.csv".
```{r}
erroneous_values <- read_csv("data/erroneous_values/erroneous_values.csv",trim_ws = TRUE)
woody_field_traits <- left_join(woody_field_traits, erroneous_values) %>%
filter(is.na(erroneous)) %>%
dplyr::select(-reason, -erroneous)
```
error_check_plant_height also reveals a number of implausibly low plant heights in the dataset ID "Thomas_2017" of 0.001. These appear to represent resprouting individuals according to the methods available in Austraits. These are also removed.
```{r}
woody_field_traits %>%
ungroup() %>%
filter(!(value == 0.001 & dataset_id == "Thomas_2017" & trait_name == "plant_height")) -> woody_field_traits
```
Convert $\delta^{13}C$ values to $\Delta^{13}C$ values (Farquhar et al. 1989).
```{r}
woody_field_traits %>%
filter(trait_name == "leaf_delta13C") %>%
mutate(trait_name = "leaf_capital_delta13C") %>%
mutate(value = (-8/1000 - value/1000)/(1 + value/1000)*1000) -> leaf_Delta13C_calculated
```
Bind the newly calculated $\Delta^{13}C$ to the original dataframe
```{r}
woody_field_traits %>%
bind_rows(leaf_Delta13C_calculated) -> woody_field_traits
```
Find C3 species to filter the delta13c values (as mentioned in MS)
```{r}
austraits$traits %>%
filter(trait_name == "photosynthetic_pathway") %>%
dplyr::select(taxon_name, value) %>%
group_by(taxon_name) %>%
nest(photosynthetic_pathway = -taxon_name) %>%
mutate(photosynthetic_pathway = map(photosynthetic_pathway, ~unique(.x$value))) %>%
mutate(length_pathways = map_dbl(photosynthetic_pathway, ~length(.x))) %>%
filter(length_pathways == 1) %>%
unnest(photosynthetic_pathway) %>%
filter(photosynthetic_pathway == "c3") %>%
dplyr::select(-length_pathways) -> austraits_photosythnetic_pathway_C3_taxa
```
Remove leaf capital delta13c values if they belong to a taxa which is either not c3 or is unknown. Removes 952 delta13c obs.
```{r}
woody_field_traits %>%
left_join(austraits_photosythnetic_pathway_C3_taxa) %>%
filter(!(trait_name == "leaf_capital_delta13C" & is.na(photosynthetic_pathway))) -> woody_field_traits
```
Now, onto the climate-based extraction. First extract all sites from Austraits regardless of whether they are currently included in study.
```{r}
#where are sites located?
location_of_sites <-
austraits$locations %>%
filter(location_property %in% c("longitude (deg)", "latitude (deg)")) %>%
spread(location_property, value) %>%
rowid_to_column("ID") %>%
rename(latitude = `latitude (deg)`, longitude = `longitude (deg)`) %>%
#some sites do not have location info if they are not `field` studies like ANBG (botanical garden) but have some other title like "unknown" so get NA warning
dplyr::mutate_at(c("latitude", "longitude"), ~as.numeric(.x)) %>%
drop_na(longitude, latitude)
```
Create a new trait dataframe to only include observations with associated latitude and longitude. Indeed, many field studies do not have lat/long data and thus cannot be used for site-based climate analyses. 94,452 obs versus 78,128 after filtering.
```{r}
woody_field_traits_georef <- woody_field_traits %>%
left_join(location_of_sites) %>%
drop_na(latitude,longitude)
```
Define the core traits to focus on
```{r}
core_traits <- c("huber_value","leaf_capital_delta13C","leaf_N_per_area","leaf_N_per_area_calc","leaf_mass_per_area","wood_density","plant_height","leaf_area","seed_dry_mass")
```
Plot the whole trait dataset over the Australian continent (Figures S1-S3).
```{r}
woody_field_traits_georef %>%
filter(entity_type %in% c("individual","population")) %>%
unnest(growth_form_data) %>%
drop_na(growth_form) %>%
filter(trait_name %in% core_traits) %>%
filter(!trait_name %in% c("huber_value","wood_density")) %>%
plot_site_map_by_trait_dataset() -> overall_distribution_dataset
woody_field_traits_georef %>%
filter(entity_type %in% c("individual","population")) %>%
ungroup() %>%
unnest(growth_form_data) %>%
drop_na(growth_form) %>%
filter(growth_form == "Woody" | woody_like_taxa == "Woody") %>%
filter(trait_name %in% core_traits) %>%
plot_site_map_by_trait_dataset() -> woody_distribution_dataset
woody_field_traits_georef %>%
filter(entity_type %in% c("individual","population")) %>%
unnest(growth_form_data) %>%
drop_na(growth_form) %>%
filter(growth_form == "Non-woody" & woody_like_taxa != "Woody") %>%
filter(trait_name %in% core_traits) %>%
filter(!trait_name %in% c("huber_value","wood_density")) %>%
plot_site_map_by_trait_dataset() -> non_woody_distribution_datatset
overall_distribution_dataset +
labs(title = "All trait data") -> a
woody_distribution_dataset +
labs(title = "Woody trait data") -> b
non_woody_distribution_datatset +
labs(title = "Non-woody trait data") -> c_plot
png("output/manuscript/supps_figures/overall_distribution.png", height = 6000, width = 5500, res = 300)
cowplot::plot_grid(plotlist = list(a,b,c_plot), ncol = 1, labels = c("a)","b)","c)"), align = "v")
dev.off()
```
Now to load the climate data. The function load_climate_data is designed to minimise load time for analysis by interpreting whether a climate.RDS is available which is appropriate for the current state of woody_field_traits_georef. The function only extracts climate data for the sites in woody_field_traits_georef, so if the identity of the sites in woody_field_traits_georef changes, then load_climate_data will need to be rerun.
```{r}
climate_data <- load_climate_data(woody_field_traits_georef)%>%
group_by(ID, cell) %>%
nest(.key = "climate_data")
```
Read in climate data and join to traits
```{r}
woody_field_traits_georef %>%
left_join(climate_data) -> woody_field_traits_georef_climate
```
We will now inspect distribution of data in each study relative to total distribution to see if there is any erroneous data. If no folders currently exist in traits_by_dataset, this function will create directories with the name of each trait, then populate them with .pngs of scatterplots of each dataset in red relative to the remaining data of tha trait.
```{r eval=FALSE, include=FALSE}
trait_by_dataset_core_traits<-function(trait, data){
data %>%
distinct(dataset_id) -> dataset_index
purrr::map(dataset_index$dataset_id, plot_trait_by_dataset, trait, data)
}
woody_field_traits_georef_climate %>%
unnest(climate_data) %>%
group_by(trait_name) %>%
nest() %>%
map2(.x = .$trait_name, .y = .$data, .f = ~trait_by_dataset_core_traits(.x, .y))
```
This is important. Converts observation level data in species x site-mean level observations. This is achieved by grouping by cell, which is obtained from the climate extraction above. Importantly, we group by cell here, rather than location_id and dataset_id to minimise sampling bias associated with repeated sampling of a given site. Thus, two different studies on the same taxa at the same site would have trait observations averaged. Thus, species x site-mean level observatiosn are conducted at the same resolution as the climate-data (~1km at the equator). In the majority of cases (>50%), most species x site-means are are represented by a single observation.
```{r}
woody_field_traits_georef_climate %>%
dplyr::select(cell, taxon_name, trait_name, value, climate_data, growth_form_data) %>%
group_by(cell, taxon_name, trait_name, climate_data, growth_form_data) %>%
nest(.key = "value") %>%
ungroup() %>%
mutate(n = map_dbl(value, ~length(.x$value))) %>%
pull(n) %>%
table()
woody_field_traits_georef_climate %>%
dplyr::select(cell, taxon_name, trait_name, value, climate_data, growth_form_data) %>%
group_by(cell, taxon_name, trait_name, climate_data, growth_form_data) %>%
nest(.key = "value") %>%
mutate(value = ifelse(trait_name == "plant_height",
map_dbl(value, ~max(.x$value)),
map_dbl(value, ~mean(.x$value)))) %>% ungroup() -> woody_field_traits_georef_mean_climate_postpop_included
```
Make another version of woody_field_traits_georef_climate with species/metapopulation heights removed. We will work primarily with this object, but will bring back the one above to compare height relationships with MAP when this data is included.
```{r}
woody_field_traits_georef_climate %>%
filter(entity_type %in% c("individual","population")) %>%
dplyr::select(cell, taxon_name, trait_name, value, climate_data, growth_form_data) %>%
group_by(cell, taxon_name, trait_name, climate_data, growth_form_data) %>%
nest(.key = "value") %>%
mutate(value = ifelse(trait_name == "plant_height",
map_dbl(value, ~max(.x$value)),
map_dbl(value, ~mean(.x$value)))) %>%
ungroup() -> woody_field_traits_georef_mean_climate
```
Inspecting possibility of intra-specific variation analysis. Far too much data would be removed. 1324/1932 taxa would be removed for LMA
```{r}
woody_field_traits_georef_mean_climate %>%
filter(trait_name == "leaf_mass_per_area") %>%
unnest(growth_form_data) %>%
filter(growth_form == "Woody") %>%
group_by(taxon_name) %>%
mutate(n = n()) %>%
ungroup() %>%
filter(n < 3) %>%
distinct(taxon_name)
woody_field_traits_georef_mean_climate %>%
filter(trait_name == "leaf_mass_per_area") %>%
unnest(growth_form_data) %>%
filter(growth_form == "Woody") %>%
group_by(taxon_name) %>%
mutate(n = n()) %>%
ungroup() %>%
distinct(taxon_name)
```
Starting preparing the new species x site means for analysis. Firslty, convert to a wide format, so that we can assess which traits and environmental variables are not normally distributed.
```{r}
woody_field_traits_georef_mean_climate %>%
filter(trait_name %in% core_traits) %>%
pivot_wider(values_from = value, names_from = trait_name) %>%
unnest(climate_data) %>%
dplyr::rename(Temp = temp,
PET = annualPET,
Prec = prec,
VPD = vpd,
prec_cv = prec.cv,
temp_cv = temp.cv,
prec_wq = prec.wq,
prec_dq = prec.dq,
prec_hq = prec.hq,
prec_cq = prec.cq) -> woody_field_traits_georef_mean_climate_wide
woody_field_traits_georef_mean_climate_wide %>%
mutate(MI = Prec/PET) %>%
mutate(log_MI = log(MI)) %>%
mutate(log_leaf_capital_delta13C = log(leaf_capital_delta13C),
log_leaf_area = log(leaf_area),
log_seed_mass = log(seed_dry_mass),
log_LMA = log(leaf_mass_per_area),
log_wood_dens = log(wood_density),
log_leaf_N_calc = log(leaf_N_per_area_calc),
log_leaf_N = log(leaf_N_per_area),
log_height = log(plant_height),
log_huber = log(huber_value),
log_VPD = log(VPD),
log_Prec = log(Prec),
log_prec_hq = log(prec_hq),
log_prec_wq = log(prec_wq),
sqrt_prec_dq = sqrt(prec_dq),
sqrt_prec_cq = sqrt(prec_cq),
log_Prec_CV = log(prec_cv)) -> woody_field_traits_georef_mean_climate_wide
#environmental variables
#normal
hist(woody_field_traits_georef_mean_climate_wide$Temp)
#log
hist(log(woody_field_traits_georef_mean_climate_wide$VPD))
hist(log(woody_field_traits_georef_mean_climate_wide$Prec))
#sqrt
hist(sqrt(woody_field_traits_georef_mean_climate_wide$prec_dq))
hist(sqrt(woody_field_traits_georef_mean_climate_wide$prec_cq))
#log
hist(log(woody_field_traits_georef_mean_climate_wide$prec_wq))
hist(log(woody_field_traits_georef_mean_climate_wide$prec_hq))
hist(log(woody_field_traits_georef_mean_climate_wide$MI))
hist(log(woody_field_traits_georef_mean_climate_wide$prec_cv))
#traits - all logged
hist(log(woody_field_traits_georef_mean_climate_wide$leaf_area))
hist(log(woody_field_traits_georef_mean_climate_wide$leaf_mass_per_area))
hist(log(woody_field_traits_georef_mean_climate_wide$wood_density))
hist(log(woody_field_traits_georef_mean_climate_wide$leaf_N_per_area_calc))
hist(log(woody_field_traits_georef_mean_climate_wide$plant_height))
hist(log(woody_field_traits_georef_mean_climate_wide$huber_value))
hist(log(woody_field_traits_georef_mean_climate_wide$seed_dry_mass))
hist(log(woody_field_traits_georef_mean_climate_wide$leaf_capital_delta13C))
```
Assess correlations amonst trait-pairs. This is the correlation between species-site means, so this will apply where trait observations where made on different traits in the same cell, so potentially from different dataset contributors. Due to the nature of the Austraits, some trait pairs are more sparse than others if they are infrequently measured together. This is most apparent for the non-woody taxa, for which some traits pairs are NA because there are less than 3 sets of observations for some trait pairs. Supps Figure (XX - XX)
```{r}
png("output/manuscript/supps_figures/trait_correlation_woody.png", height=2000, width=2000, res=200)
woody_field_traits_georef_mean_climate_wide %>%
unnest(growth_form_data) %>%
filter(growth_form == "Woody") %>%
dplyr::select(growth_form, log_leaf_capital_delta13C, log_height, log_leaf_area, log_LMA, log_seed_mass, log_leaf_N_calc, log_huber, log_wood_dens) -> for_corrplot
ggpairs(for_corrplot,columns = (2:9), upper = list(continuous = GGally::wrap("cor", stars = FALSE)), columnLabels = c("Delta**{13}*C","MH","LA", "LMA","SM","N[area]", "SA:LA","WD"),
labeller = "label_parsed",
title = "Woody trait data")
dev.off()
png("output/manuscript/supps_figures/trait_correlation_non_woody.png", height=1600, width=1600, res=200)
woody_field_traits_georef_mean_climate_wide %>%
unnest(growth_form_data) %>%
filter(growth_form != "Woody") %>%
# mutate(na_col = rep(NA, 2927)) %>%
# mutate(na_col2 = rep(NA, 2927)) %>%
dplyr::select(log_leaf_capital_delta13C, log_height, log_leaf_area, log_LMA, log_seed_mass, log_leaf_N_calc) -> for_corrplot
ggpairs_modified(for_corrplot, upper = list(continuous = GGally::wrap("cor", stars = FALSE))
, columnLabels = c("Delta**{13}*C", "MH", "LA", "LMA", "SM", "N[area]"),
labeller = "label_parsed",
title = "Non-woody trait data") -> b
b
dev.off()
png("output/manuscript/supps_figures/trait_correlation_overall.png", height=1600, width=1600, res=200)
woody_field_traits_georef_mean_climate_wide %>%
unnest(growth_form_data) %>%
# filter(growth_form != "Woody") %>%
# mutate(na_col = rep(NA, 2927)) %>%
# mutate(na_col2 = rep(NA, 2927)) %>%
dplyr::select(log_leaf_capital_delta13C, log_height, log_leaf_area, log_LMA, log_seed_mass, log_leaf_N_calc) -> for_corrplot
ggpairs(for_corrplot, upper = list(continuous = GGally::wrap("cor", stars = FALSE)), columnLabels = c("Delta**{13}*C", "MH","LA", "LMA", "SM", "N[area]"),
labeller = "label_parsed",
title = "All trait data") -> c
c
dev.off()
```
##Main analysis
First, create a function to fit a linear model, and then a quadratic model, then extract the linear term coefficient and the r.squared values for the linear and quadratic model.
```{r}
fit_models <- function(...){
data <- tibble(...) %>%
unnest(data)
fit_lm <- lm(trait_value~env_value, data)
data$env_value2 <- data$env_value^2
fit_quad <- lm(trait_value ~ env_value + env_value2, data)
return(tibble(p_lm = summary(fit_lm)$coef[2,4],
fit_lm = summary(fit_lm)$r.squared,
fit_quad = summary(fit_quad)$r.squared))
}
```
Start by assessing correlations for the `overall` data. This means that we drop any data which has NA for growth form, but otherwise all data is included.
```{r}
woody_field_traits_georef_mean_climate_wide %>%
unnest(growth_form_data) %>%
drop_na(growth_form) %>%
# drop_na(AI_thorn) %>%
dplyr::select(log_leaf_capital_delta13C, log_height, log_leaf_N_calc, log_LMA, log_seed_mass, log_leaf_area, Temp,log_Prec, log_MI, log_VPD, log_Prec_CV, log_prec_wq, sqrt_prec_dq, log_prec_hq, cell) %>%
pivot_longer(cols = c(log_leaf_capital_delta13C,
log_leaf_area,
log_seed_mass,
log_LMA,
log_leaf_N_calc,
log_height),
names_to = "trait_name", values_to = "trait_value") %>%
pivot_longer(cols = c(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,Temp,log_prec_hq), names_to = "env_name", values_to = "env_value") %>%
group_by(trait_name, env_name) %>%
nest() %>%
ungroup %>%
mutate(r2 = pmap(., fit_models)) %>%
unnest(r2) -> overall_relationships
overall_relationships %>%
dplyr::select(fit_lm, trait_name, env_name) %>%
pivot_wider(names_from = "env_name", values_from ="fit_lm") -> fit_lm_overall
overall_relationships %>%
dplyr::select(fit_quad, trait_name, env_name) %>%
pivot_wider(names_from = "env_name", values_from ="fit_quad") -> fit_quad_overall
```
Print linear and quadratic output table in .tex format for overleaf
```{r}
print(xtable(fit_lm_overall %>%
dplyr::select(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq,Temp, trait_name) %>%
rename("log(MAP)" = "log_Prec",
"log(VPD)" = "log_VPD",
"log(Precipitation seasonality)" = "log_Prec_CV",
"log(Wettest quarter precipitation)" = "log_prec_wq",
"sqrt(Driest quarter precipitation)" = "sqrt_prec_dq",
"log(Warmest quarter precipitation)" = "log_prec_hq",
"MAT" = "Temp",
"log(Moisture index)" = "log_MI") %>% pivot_longer(cols = -(trait_name)) %>% pivot_wider(names_from = "trait_name", values_from = "value")), include.rownames=FALSE, file = "output/manuscript/supps_tables/fit_lm_overall_prec.tex")
print(xtable(fit_quad_overall %>%
dplyr::select(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq,Temp, trait_name) %>%
rename("log(MAP)" = "log_Prec",
"log(VPD)" = "log_VPD",
"log(Precipitation seasonality)" = "log_Prec_CV",
"log(Wettest quarter precipitation)" = "log_prec_wq",
"sqrt(Driest quarter precipitation)" = "sqrt_prec_dq",
"log(Warmest quarter precipitation)" = "log_prec_hq",
"MAT" = "Temp",
"log(Moisture index)" = "log_MI") %>% pivot_longer(cols = -(trait_name)) %>% pivot_wider(names_from = "trait_name", values_from = "value")), include.rownames=FALSE, file = "output/manuscript/supps_tables/fit_quad_overall_prec.tex")
```
Assessing correlations for the `woody` data. This analysis therefore does NOT include non-woody woody-like taxa.
```{r}
woody_field_traits_georef_mean_climate_wide %>%
unnest(growth_form_data) %>%
drop_na(growth_form) %>%
filter(growth_form == "Woody") %>%
# drop_na(AI_thorn) %>%
dplyr::select(log_leaf_capital_delta13C, log_height, log_leaf_N_calc, log_LMA, log_seed_mass, log_leaf_area,
log_huber,
log_wood_dens, Temp, log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_prec_hq,log_MI, cell) %>%
pivot_longer(cols = c(log_leaf_capital_delta13C,
log_leaf_area,
log_seed_mass,
log_LMA,
log_leaf_N_calc,
log_height,
log_huber,
log_wood_dens),
names_to = "trait_name", values_to = "trait_value") %>%
pivot_longer(cols = c(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_prec_hq,log_MI, Temp), names_to = "env_name", values_to = "env_value") %>%
group_by(trait_name, env_name) %>%
nest() %>%
ungroup %>%
mutate(r2 = pmap(., fit_models)) %>%
unnest(r2)-> woody_relationships
woody_relationships %>%
dplyr::select(fit_lm, trait_name, env_name) %>%
pivot_wider(names_from = "env_name", values_from ="fit_lm") -> fit_lm_woody
```
Print linear output table in .tex format for overleaf
```{r}
print(xtable(fit_lm_woody %>%
dplyr::select(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq,Temp, trait_name) %>%
rename("log(MAP)" = "log_Prec",
"log(VPD)" = "log_VPD",
"log(Precipitation seasonality)" = "log_Prec_CV",
"log(Wettest quarter precipitation)" = "log_prec_wq",
"sqrt(Driest quarter precipitation)" = "sqrt_prec_dq",
"log(Warmest quarter precipitation)" = "log_prec_hq",
"MAT" = "Temp",
"log(Moisture index)" = "log_MI") %>% pivot_longer(cols = -(trait_name)) %>% pivot_wider(names_from = "trait_name", values_from = "value")), include.rownames=FALSE, file = "output/manuscript/supps_tables/fit_lm_woody_prec.tex")
```
Assessing correlations for the `woody` data and woody-like taxa.
```{r}
woody_field_traits_georef_mean_climate_wide %>%
unnest(growth_form_data) %>%
drop_na(growth_form) %>%
filter(growth_form == "Woody" | woody_like_taxa == "Woody") %>%
dplyr::select(log_leaf_capital_delta13C, log_height, log_leaf_N_calc, log_LMA, log_seed_mass, log_leaf_area,
log_huber,
log_wood_dens, log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_prec_hq,Temp,log_MI, cell) %>%
pivot_longer(cols = c(log_leaf_capital_delta13C,
log_leaf_area,
log_seed_mass,
log_LMA,
log_leaf_N_calc,
log_height,
log_huber,
log_wood_dens),
names_to = "trait_name", values_to = "trait_value") %>%
pivot_longer(cols = c(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_prec_hq,log_MI,Temp), names_to = "env_name", values_to = "env_value") %>%
group_by(trait_name, env_name) %>%
nest() %>%
ungroup %>%
mutate(r2 = pmap(., fit_models)) %>%
unnest(r2)-> woody_relationships
woody_relationships %>%
dplyr::select(fit_lm, trait_name, env_name) %>%
pivot_wider(names_from = "env_name", values_from ="fit_lm") -> fit_lm_woody
woody_relationships %>%
dplyr::select(fit_quad, trait_name, env_name) %>%
pivot_wider(names_from = "env_name", values_from ="fit_quad") -> fit_quad_woody
```
Print linear and quadratic output table in .tex format for overleaf
```{r}
print(xtable(fit_lm_woody %>%
dplyr::select(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq,Temp, trait_name) %>%
rename("log(MAP)" = "log_Prec",
"log(VPD)" = "log_VPD",
"log(Precipitation seasonality)" = "log_Prec_CV",
"log(Wettest quarter precipitation)" = "log_prec_wq",
"sqrt(Driest quarter precipitation)" = "sqrt_prec_dq",
"log(Warmest quarter precipitation)" = "log_prec_hq",
"MAT" = "Temp",
"log(Moisture index)" = "log_MI")%>% pivot_longer(cols = -(trait_name)) %>% pivot_wider(names_from = "trait_name", values_from = "value")), include.rownames=FALSE, file = "output/manuscript/supps_tables/fit_lm_woody_prec_woody_like.tex")
print(xtable(fit_quad_woody %>%
dplyr::select(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq,Temp, trait_name) %>%
rename("log(MAP)" = "log_Prec",
"log(VPD)" = "log_VPD",
"log(Precipitation seasonality)" = "log_Prec_CV",
"log(Wettest quarter precipitation)" = "log_prec_wq",
"sqrt(Driest quarter precipitation)" = "sqrt_prec_dq",
"log(Warmest quarter precipitation)" = "log_prec_hq",
"MAT" = "Temp",
"log(Moisture index)" = "log_MI")%>% pivot_longer(cols = -(trait_name)) %>% pivot_wider(names_from = "trait_name", values_from = "value")), include.rownames=FALSE, file = "output/manuscript/supps_tables/fit_quad_woody_prec_woody_like.tex")
```
Assessing correlations for the `non-woody` data and woody-like taxa.
```{r}
woody_field_traits_georef_mean_climate_wide %>%
unnest(growth_form_data) %>%
drop_na(growth_form) %>%
filter(growth_form != "Woody") %>%
# drop_na(AI_thorn) %>%
dplyr::select(log_leaf_capital_delta13C, log_height, log_leaf_N_calc, log_LMA, log_seed_mass, log_leaf_area,
log_Prec, log_VPD,log_Prec_CV, Temp, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq, cell) %>%
pivot_longer(cols = c(log_leaf_capital_delta13C,
log_leaf_area,
log_seed_mass,
log_LMA,
log_leaf_N_calc,
log_height),
names_to = "trait_name", values_to = "trait_value") %>%
pivot_longer(cols = c(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, Temp,sqrt_prec_dq,log_MI,log_prec_hq), names_to = "env_name", values_to = "env_value") %>%
group_by(trait_name, env_name) %>%
nest() %>%
ungroup %>%
mutate(r2 = pmap(., fit_models)) %>%
unnest(r2)-> non_woody_relationships
non_woody_relationships %>%
dplyr::select(fit_lm, trait_name, env_name) %>%
pivot_wider(names_from = "env_name", values_from ="fit_lm") -> fit_lm_non_woody
```
Print linear output table in .tex format for overleaf
```{r}
print(xtable(fit_lm_non_woody %>%
dplyr::select(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq,Temp, trait_name) %>%
rename("log(MAP)" = "log_Prec",
"log(VPD)" = "log_VPD",
"log(Precipitation seasonality)" = "log_Prec_CV",
"log(Wettest quarter precipitation)" = "log_prec_wq",
"sqrt(Driest quarter precipitation)" = "sqrt_prec_dq",
"log(Warmest quarter precipitation)" = "log_prec_hq",
"MAT" = "Temp",
"log(Moisture index)" = "log_MI")%>% pivot_longer(cols = -(trait_name)) %>% pivot_wider(names_from = "trait_name", values_from = "value")), include.rownames=FALSE, file = "output/manuscript/supps_tables/fit_lm_non_woody_prec.tex")
```
Assessing correlations for the `non-woody` data and NO woody-like taxa.
```{r}
woody_field_traits_georef_mean_climate_wide %>%
unnest(growth_form_data) %>%
drop_na(growth_form) %>%
filter(growth_form != "Woody" & woody_like_taxa != "Woody") %>%
dplyr::select(log_leaf_capital_delta13C, log_height, log_leaf_N_calc, log_LMA, log_seed_mass, log_leaf_area, log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,Temp,log_prec_hq, cell) %>%
pivot_longer(cols = c(log_leaf_capital_delta13C,
log_leaf_area,
log_seed_mass,
log_LMA,
log_leaf_N_calc,
log_height),
names_to = "trait_name", values_to = "trait_value") %>%
pivot_longer(cols = c(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,Temp,log_prec_hq,log_MI), names_to = "env_name", values_to = "env_value") %>%
group_by(trait_name, env_name) %>%
nest() %>%
ungroup %>%
mutate(r2 = pmap(., fit_models)) %>%
unnest(r2)-> non_woody_relationships
non_woody_relationships %>%
dplyr::select(fit_lm, trait_name, env_name) %>%
pivot_wider(names_from = "env_name", values_from ="fit_lm") -> fit_lm_non_woody
non_woody_relationships %>%
dplyr::select(fit_quad, trait_name, env_name) %>%
pivot_wider(names_from = "env_name", values_from ="fit_quad") -> fit_quad_non_woody
```
Print linear and quadratic output table in .tex format for overleaf
```{r}
print(xtable(fit_lm_non_woody %>%
dplyr::select(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq,Temp, trait_name) %>%
rename("log(MAP)" = "log_Prec",
"log(VPD)" = "log_VPD",
"log(Precipitation seasonality)" = "log_Prec_CV",
"log(Wettest quarter precipitation)" = "log_prec_wq",
"sqrt(Driest quarter precipitation)" = "sqrt_prec_dq",
"log(Warmest quarter precipitation)" = "log_prec_hq",
"MAT" = "Temp",
"log(Moisture index)" = "log_MI")%>% pivot_longer(cols = -(trait_name)) %>% pivot_wider(names_from = "trait_name", values_from = "value")), include.rownames=FALSE, file = "output/manuscript/supps_tables/fit_lm_non_woody_prec_woody_like.tex")
print(xtable(fit_quad_non_woody %>%
dplyr::select(log_Prec, log_VPD,log_Prec_CV, log_prec_wq, sqrt_prec_dq,log_MI,log_prec_hq,Temp, trait_name) %>%
rename("log(MAP)" = "log_Prec",
"log(VPD)" = "log_VPD",
"log(Precipitation seasonality)" = "log_Prec_CV",
"log(Wettest quarter precipitation)" = "log_prec_wq",
"sqrt(Driest quarter precipitation)" = "sqrt_prec_dq",
"log(Warmest quarter precipitation)" = "log_prec_hq",
"MAT" = "Temp",
"log(Moisture index)" = "log_MI")%>% pivot_longer(cols = -(trait_name)) %>% pivot_wider(names_from = "trait_name", values_from = "value")), include.rownames=FALSE, file = "output/manuscript/supps_tables/fit_quad_non_woody_prec_woody_like.tex")
```
Assessing potential interactions between precipitation and temperature. Looking for the plotted effects as well as the variance explained.
```{r}
fit_and_plot_interaction_models <- function(..., shape = "circle"){
#load data and unnest trait data
data <- unnest(...)
#fit basic prec model
res_prec <- lm(trait_value~log_Prec, data)
#fit main effect model
res_main <- lm(trait_value~log_Prec+Temp, data)
#fit interaction model
res <- lm(trait_value~log_Prec*Temp, data)
#create a in-sample dataset to predict from interaction model. Sampling across rainfall gradient for a given trait and the 33rd and 66th quantile of the temperature gradient
newdf = expand_grid(log_Prec = seq(min(data$log_Prec, na.rm = T), max(data$log_Prec, na.rm = T), length.out = 100), Temp = c(quantile(data$Temp, 0.25, na.rm = T), quantile(data$Temp, 0.75, na.rm = T)))