-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathf_traits.R
146 lines (121 loc) · 5.22 KB
/
f_traits.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
# load packages
library(neonUtilities)
library(neonOS)
library(ggplot2)
library(maps)
# download data
cfc <- loadByProduct(dpID='DP1.10026.001',
package='expanded',
include.provisional=F,
check.size=F)
# do this the first time you run the code, to download the data
# change file path to something on your computer
saveRDS(cfc, '/Users/clunch/Desktop/cfc_rel_2024.rds')
# after the first time, run this line instead of lines 6-13 - load already-downloaded data
# change file path
cfc <- readRDS('/Users/clunch/Desktop/cfc_rel_2024.rds')
# make species list for each site
splist <- vector('list', length(unique(cfc$cfc_fieldData$siteID)))
names(splist) <- unique(cfc$cfc_fieldData$siteID)
ind <- 0
for(i in unique(cfc$cfc_fieldData$siteID)) {
ind <- ind+1
itax <- unique(cfc$cfc_fieldData$taxonID[which(cfc$cfc_fieldData$siteID==i)])
splist[[ind]] <- itax
}
# get frequency table of all species
ft <- table(unlist(splist))
# find most frequent species
ft[which(ft==max(ft))]
# get the 13 sites where ACRU (Acer rubrum) appears
# actually this step is unnecessary
aspl <- splist[unlist(lapply(splist, FUN=function(x) {'ACRU' %in% x}))]
acru.sites <- names(aspl)
# join chemistry data to species data, subset to only ACRU data
lma <- joinTableNEON(cfc$cfc_fieldData,
cfc$cfc_LMA,
'cfc_fieldData',
'cfc_LMA')
lmaa <- lma[which(lma$taxonID=='ACRU'),]
# load site metadata table to get location & climate of each site
fsm <- read.csv('/Users/clunch/Desktop/NEON_Field_Site_Metadata_20231026.csv')
# join to trait data
lmaall <- merge(lmaa, fsm, by.x='siteID', by.y='field_site_id', all.x=T)
# calculate mean values per site
lmamean <- aggregate(lmaall, by=list(lmaall$siteID), FUN=mean, na.rm=T)
colnames(lmamean)[1] <- 'Site'
# get US map data from maps package
mpus <- map_data('world')[which(map_data('world')$region=='USA'),]
# bubble map of ACRU LMA
gg <- ggplot() +
geom_polygon(data=mpus, aes(x=long, y=lat, group=group), fill="grey", alpha=0.3) +
geom_point(data=lmamean, aes(x=field_longitude, y=field_latitude,
size=leafMassPerArea, color=Site,
alpha=0.5)) +
theme_void() + xlim(-130,-60) + ylim(25,50) + coord_map()
gg
# plot against temperature/rainfall instead of map
gg <- ggplot(data=lmamean, aes(x=field_mean_annual_precipitation_mm,
y=field_mean_annual_temperature_C,
color=Site, alpha=0.5,
size=leafMassPerArea)) +
geom_point()
gg
# do the same thing with the minor elements table, check out foliar Mn
ele <- joinTableNEON(cfc$cfc_fieldData,
cfc$cfc_elements,
'cfc_fieldData',
'cfc_elements')
elea <- ele[which(ele$taxonID=='ACRU'),]
eleall <- merge(elea, fsm, by.x='siteID', by.y='field_site_id', all.x=T)
elemean <- aggregate(eleall, by=list(eleall$siteID), FUN=mean, na.rm=T)
colnames(elemean)[1] <- 'Site'
gg <- ggplot() +
geom_polygon(data=mpus, aes(x=long, y=lat, group=group), fill="grey", alpha=0.3) +
geom_point(data=elemean, aes(x=field_longitude, y=field_latitude,
size=foliarManganeseConc, color=Site,
alpha=0.5)) +
theme_void() + xlim(-130,-60) + ylim(25,50) + coord_map()
gg
gg <- ggplot(data=elemean, aes(x=field_mean_annual_precipitation_mm,
y=field_mean_annual_temperature_C,
color=Site, alpha=0.5,
size=foliarManganeseConc)) +
geom_point()
gg
# do the same thing with the C/N table, check out foliar C:N ratio
cn <- joinTableNEON(cfc$cfc_fieldData,
cfc$cfc_carbonNitrogen,
'cfc_fieldData',
'cfc_carbonNitrogen')
cna <- cn[which(cn$taxonID=='ACRU'),]
cnall <- merge(cna, fsm, by.x='siteID', by.y='field_site_id', all.x=T)
cnmean <- aggregate(cnall, by=list(cnall$siteID), FUN=mean, na.rm=T)
colnames(cnmean)[1] <- 'Site'
gg <- ggplot() +
geom_polygon(data=mpus, aes(x=long, y=lat, group=group), fill="grey", alpha=0.3) +
geom_point(data=cnmean, aes(x=field_longitude, y=field_latitude,
size=CNratio, color=Site,
alpha=0.5)) +
theme_void() + xlim(-130,-60) + ylim(25,50) + coord_map()
gg
gg <- ggplot(data=cnmean, aes(x=field_mean_annual_precipitation_mm,
y=field_mean_annual_temperature_C,
color=Site, alpha=0.5,
size=CNratio)) +
geom_point()
gg
# 13C
gg <- ggplot() +
geom_polygon(data=mpus, aes(x=long, y=lat, group=group), fill="grey", alpha=0.3) +
geom_point(data=cnmean, aes(x=field_longitude, y=field_latitude,
size=d13C, color=Site,
alpha=0.5)) +
theme_void() + xlim(-130,-60) + ylim(25,50) + coord_map()
gg
gg <- ggplot(data=cnmean, aes(x=field_mean_annual_precipitation_mm,
y=field_mean_annual_temperature_C,
color=Site, alpha=0.5,
size=d13C)) +
geom_point()
gg