-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path01_Descriptive figures.R
324 lines (258 loc) · 15.4 KB
/
01_Descriptive figures.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
###############################!
#1: Descriptive figures #######!
###############################!
remove(list=ls())
#Load libraries and functions
source("./Code/Functions.R")
source("./Code/Libraries.R")
# Fig. 2: Description maps ----
# NOTE: The stations map (panel A) was saved in vector format and then the names of the regions were added manually in Adobe Illustrator.
# The metrics shown in the other panels (B-E) were obtained later in the process, the full process is detailed in the script "03_Distinctiveness_local"
# Fig. 2A: Stations map ----
data<-read.csv("./Data/Raw_data/species_AFDW_2005_2020.txt",header=T,dec=".",sep="\t", check.names = FALSE)
data$year <- as.factor(data$year)
p <- ggplot(data, aes(x = lon,y = lat)) +
geom_point(size = 1.5, color = "darkgreen") +
borders(fill="grey55", colour = "grey5") +
theme(panel.background = element_rect(fill='lightsteelblue1', colour = 'black')) +
coord_quickmap(xlim=c(min(data$lon), max(data$lon)),ylim= c(min(data$lat),max(data$lat)))+
labs(x = "Lon",y="Lat")+
theme(legend.position = "none")+
theme(legend.title = element_text(size = 11),legend.text = element_text(size = 10))+
guides(shape = guide_legend(override.aes = list(size = 1))); p
#ggsave(file="map_stations.svg", plot=p, width=8, height=10)
# Fig. 2B-E: Richness, Shannon, relative biomass of NIS and % of NIS in local communities ----
data<-read.csv("./Data/Di_metrics_station.txt",header=T,dec=".",sep="\t", check.names = FALSE)
datat <- data
#Linear interpolation for all the variables
metrics <- c("richness", "Shannon", "ratio_NISvsNAT", "ratio_sp"); plots = list()
#NOTE: This process might not work some times, if it does not work, run the iterations manually
for (i in 1:length(metrics)){
temp <- datat; colnames(temp)[which(colnames(temp) == metrics[i])] <- "var"
coords <- cbind(lon = temp$lon, lat = temp$lat)
#Cut a polygon to crop the interpolated data
myShape <- getDynamicAlphaHull(coords, partCount = 1, buff=500, clipToCoast = FALSE) #create a polygon to define the training area
# plot(myShape[[1]])
try <- temp
#Krigging: linear interpolation
coordinates(temp) <- ~ lon+lat
kriging_result= autoKrige(var~1, temp)
Kriging=as.data.frame(kriging_result$krige_output); Kriging<-fortify(Kriging)
#Rasterize the linear interpolation to crop it for our interest area
coords <- cbind(Kriging$x1, Kriging$x2)
crs<- CRS("+proj=longlat")# proj4string of coords
kd<-SpatialPointsDataFrame(coords = coords, data = Kriging,proj4string = crs)
r<-raster(ncol=90, nrow=90,
xmn = min(Kriging$x1), ymn = min(Kriging$x2), xmx = max(Kriging$x1), ymx = max(Kriging$x2))
krig <- rasterize(kd,r,field = kd$var1.pred, fun = mean, update=TRUE, updatevalue = "NA"); #plot(krig)
shape_Di <- raster::crop(krig, myShape[[1]], snap = "out") #get the polygon to crop in, crop the rectangle
train <- mask(x=krig, mask=myShape[[1]]); #plot(train)
Kriging <- data.frame(rasterToPoints(train)); data <-try
#Plot the results
colpal<- c("midnightblue", "ghostwhite", "coral4") # Color palette for the map
map <- ggplot(data, aes(x= lon, y = lat)) +
geom_tile(data=Kriging,aes(x,y,fill=layer))+
scale_fill_gradientn(name = metrics[i], colours=colpal, na.value = 'white')+
borders(fill="grey55", colour = "grey5") +
theme(panel.background = element_rect(fill='lightsteelblue1', colour = 'black')) +
coord_quickmap(xlim=c(min(data$lon)-1, max(data$lon)+1),ylim= c(min(data$lat)-1,max(data$lat)+1))+
labs(x = "Lon",y="Lat")+
theme(legend.position = c(1.2,0.2))+
theme(legend.title = element_text(size = 11),legend.text = element_text(size = 10))+
guides(shape = guide_legend(override.aes = list(size = 1))); map
p <- list(map); names(p) <- metrics[i]
name <- metrics[i]
plots <- append(plots, p);
#ggsave(file=paste(name, ".svg", sep = ""), plot = p[[1]], width=10, height=10)
}
#plots
crossPlot <- ggarrange(plots[[1]], plots[[2]], plots[[3]], plots[[4]],
ncol=2, nrow=2, common.legend = F, legend = "right"); crossPlot
#ggsave(file="description_metrics.svg", plot = crossPlot, width=20, height=20)
# Fig. S1: Species biomass ----
data<-read.csv("./Data/Raw_data/species_AFDW_2005_2020.txt",header=T,dec=".",sep="\t", check.names = FALSE)
status<-read.csv("./Data/Raw_data/sp_status.txt",header=T,dec=".",sep="\t", check.names = FALSE)
# Biomass (AFDW) of all the species
biomass_cum <- as.data.frame(data %>%
group_by(taxon) %>%
summarise_at(.vars = "AFDW", sum, na.rm = TRUE))
biomass <- merge(biomass_cum, status, by="taxon"); biomass$rel_abun <- (biomass$AFDW/sum(biomass$AFDW))*100
# Donut plot for the relative abundances - Natives vs NIS ----
#Compute the cumulative percentages (top of each rectangle)
biom <- as.data.frame(biomass %>%
group_by(status) %>%
summarise_at(.vars = c("rel_abun","AFDW"), sum, na.rm = TRUE)); biom$ymax = cumsum(biom$rel_abun)
# Compute the rectangle bottom
biom$ymin = c(0, head(biom$ymax, n=-1))
#Comptue label position
biom$labelPosition <- (biom$ymax + biom$ymin) / 2
biom$label1 <- paste0(round(biom$rel_abun, digits = 2), "%")
# Make the plot-NIS VS NATIVE
natnis <- ggplot(biom, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=status)) +
geom_rect(size = 1.5, aes(color = status, alpha = 0.5,
fill = after_scale(desaturate(lighten(color, .5), .5)))) +
geom_text(x=2.2, aes(y = labelPosition,label=status, color=status, fontface = "bold"),size=11, check_overlap = TRUE) + # x here controls label position (inner / outer)
geom_label(x=3.5, aes(y = labelPosition,label=label1, color = status,
fill = after_scale(desaturate(lighten(color, .9), .9))),size=12) +
scale_fill_aaas() +
scale_color_aaas() +
coord_polar(theta="y") +
theme_void() +
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank(),
panel.border = element_blank()) + theme(legend.position = "none"); natnis
#ggsave(file="NATNIS_AFDW.svg", plot=natnis, width=10, height=8)
# Donut plot for the relative abundances - NATIVES ----
nat <- biomass[which(biomass$status == "Native"),] #Ratio groups of native organisms
nat <- as.data.frame(biomass %>%
group_by(phylum) %>%
summarise_at(.vars = c("rel_abun","AFDW"), sum, na.rm = TRUE))
nat$rel_abun <- (nat$AFDW/sum(nat$AFDW))*100;
nat_other <- nat[which(nat$rel_abun <=1),]; nat <- nat %>% add_row(phylum = "Other", AFDW = sum(nat_other$AFDW),
rel_abun = sum(nat_other$rel_abun))
nat <- anti_join(nat, nat_other); nat$ymax = cumsum(nat$rel_abun)
# Compute the rectangle bottom
nat$ymin = c(0, head(nat$ymax, n=-1))
#Comptue label position
nat$labelPosition <- (nat$ymax + nat$ymin) / 2
nat$label1 <- paste0(round(nat$rel_abun, digits = 2), "%")
#Make the plot biomass NATIVES
NAT = ggplot(nat, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=phylum)) +
geom_rect(size = 1.5, aes(color = phylum, alpha = 0.6, fill = after_scale(desaturate(lighten(color, .4), .4)))) +
scale_fill_uchicago(palette = "light") +
# x here controls label position (inner / outer)
geom_text_repel(min.segment.length = 0,
aes(x = 3, y = labelPosition,label=phylum, colour = factor(phylum), fontface = "bold.italic",
segment.size = 0.8, segment.linetype = 3), size=14, max.overlaps = Inf)+
geom_label_repel(x=3.7, aes(y = labelPosition, label=label1, color = phylum, size = 12,
fill = after_scale(desaturate(lighten(color, .9), .9))),
size=5, fontface = "bold")+
scale_color_uchicago(palette = "dark") +
coord_polar(theta="y", start = 0) +
theme_void() +
theme(panel.grid=element_blank()) +
theme(axis.text=element_blank()) +
theme(axis.ticks=element_blank(),
panel.border = element_blank()) + theme(legend.position = "none"); NAT
#ggsave(file="NAT_AFDW.svg", plot=NAT, width=10, height=8)
# Donut plot for the relative abundances - NIS ----
nis <- biomass[which(biomass$status == "Non-indigenous"),] #Ratio groups of native organisms
nis$rel_abun <- (nis$AFDW/sum(nis$AFDW))*100; nis$ymax = cumsum(nis$rel_abun)
# Compute the rectangle bottom
nis$ymin = c(0, head(nis$ymax, n=-1))
#Comptue label position
nis$labelPosition <- (nis$ymax + nis$ymin) / 2
nis$label1 <- paste0(round(nis$rel_abun, digits = 2), "%")
#Make the plot biomass NIS
NIS <- ggplot(nis, aes(ymax=ymax, ymin=ymin, xmax=4, xmin=3, fill=taxon)) +
geom_rect(size = 1.5, aes(color = taxon, alpha = 0.5, fill = after_scale(desaturate(lighten(color, .4), .4)))) +
scale_fill_uchicago(palette = "light") +
# x here controls label position (inner / outer)
geom_text_repel(min.segment.length = 0,
aes(x = 3, y = labelPosition,label=taxon, colour = factor(taxon), fontface = "bold",
segment.size = 0.8, segment.linetype = 3),
size=7, max.overlaps = Inf)+
geom_label_repel(x=3.7, aes(y = labelPosition, label=label1, color = taxon,
fill = after_scale(desaturate(lighten(color, .9), .9))),
size=6, fontface = "bold")+
scale_color_uchicago(palette = "dark") +
coord_polar(theta="y") +
theme_void() +- theme(panel.grid=element_blank()) +vtheme(axis.text=element_blank()) +
theme(axis.ticks=element_blank(), panel.border = element_blank()) + theme(legend.position = "none"); NIS
#ggsave(file="NIS_AFDW.svg", plot=NIS, width=10, height=8)
# Fig. S4; Maps of environmental variables ----
data<-read.csv("./Data/Di_metrics_station.txt",header=T,dec=".",sep="\t", check.names = FALSE); coln <- c(colnames(data))
NIS <- c("Marenzelleria", "Mya.arenaria", "Potamopyrgus.antipodarum","Streblospio.benedicti", "Polydora.cornuta")#put the names of your NIS here
colnames(data)
Di_unified <- data.frame()
# Put the species as a factor rather than separate columns -- if necessary
for(i in 1:length(NIS)){
temp <- data %>% dplyr::select(c(1:21), mean_NIS_Di, NIS[i], paste(NIS[i],"relab", sep = "_"), paste(NIS[i],"nw", sep = "_"));
temp <- subset(temp, !is.na(temp[,NIS[i]]))
colnames(temp)[c(23,24,25)] <- c("NIS_Di", "NIS_relab", "NIS_Di_nw"); temp$species <- NIS[i]
Di_unified <- rbind(Di_unified, temp)
}
#Some data manipulation, join the Di in one column and add species as a factor
data <- Di_unified; data$species <- as.factor(data$species); coln <- c(colnames(data))
#Join the environmental data with the metrics
env <- read.csv("./Data/Raw_data/env_data.txt",header=T,dec=".",sep="\t", check.names = FALSE)
head(env); env <- env %>% dplyr::select(station, year, lon, lat, month, bot_oxy, bot_sal, bot_T, depth)
data <- merge(data, env, by = c("station", "year", "lon", "lat")); #data$Sediment <- as.factor(data$Sediment)
data$station <- as.factor(data$station)
data <- data[which(!duplicated(data[,coln])),]
#Loop for the linear interpolation of each variable
metrics <- c("bot_sal", "bot_T", "depth", "bot_oxy"); plots = list(); datat = data
#NOTE: This process might not work some times, if it does not work, run the iterations manually
for (i in 1:length(metrics)){
temp <- datat; colnames(temp)[which(colnames(temp) == metrics[i])] <- "var"
temp <- temp[which(!is.na(temp$var)),]
coords <- cbind(lon = temp$lon, lat = temp$lat)
#Cut a polygon to crop the interpolated data
myShape <- getDynamicAlphaHull(coords, partCount = 1, buff=500, clipToCoast = FALSE) #create a polygon to define the training area
#plot(myShape[[1]])
try <- temp
#Krigging: linear interpolation
coordinates(temp) <- ~ lon+lat
kriging_result= autoKrige(var~1, temp)
Kriging=as.data.frame(kriging_result$krige_output); Kriging<-fortify(Kriging)
#Rasterize the linear interpolation to crop it for our interest area
coords <- cbind(Kriging$x1, Kriging$x2)
crs<- CRS("+proj=longlat")# proj4string of coords
kd<-SpatialPointsDataFrame(coords = coords,data = Kriging,proj4string = crs)
r<-raster(ncol=90, nrow=90,
xmn = min(Kriging$x1), ymn = min(Kriging$x2), xmx = max(Kriging$x1), ymx = max(Kriging$x2))
krig <- rasterize(kd,r,field = kd$var1.pred, fun = mean, update=TRUE, updatevalue = "NA"); #plot(krig)
shape_Di <- raster::crop(krig, myShape[[1]], snap = "out") #get the polygon to crop in, crop the rectangle
train <- mask(x=krig, mask=myShape[[1]]); #plot(train)
Kriging <- data.frame(rasterToPoints(train)); temp <-try
#Plot the results
if(metrics[i] == "bot_sal"){colpal<- c("ghostwhite","cornflowerblue", "midnightblue") }
if(metrics[i] == "bot_oxy"){colpal<- c("ghostwhite","cornflowerblue", "midnightblue") }
if(metrics[i] == "bot_T"){colpal<- c("cornflowerblue","ghostwhite", "red") }
if(metrics[i] == "depth"){colpal<- c("ghostwhite", "plum1", "darkorchid4") }
env_map <- ggplot(temp, aes(x= lon, y = lat)) +
geom_tile(data=Kriging,aes(x,y,fill=layer))+
scale_fill_gradientn(name = metrics[i], colours=colpal, na.value = 'white')+
borders(fill="grey55", colour = "grey5") +
theme(panel.background = element_rect(fill='ghostwhite', colour = 'black')) +
coord_quickmap(xlim=c(min(data$lon)-1, max(data$lon)+1),ylim= c(min(data$lat)-1,max(data$lat)+1))+
labs(x = "Lon",y="Lat")+
theme(legend.position = "right")+
theme(legend.title = element_text(size = 11),legend.text = element_text(size = 10))+
guides(shape = guide_legend(override.aes = list(size = 1))); env_map
p <- list(env_map); names(p) <- metrics[i]
name <- metrics[i]
plots <- append(plots, p)
}
crossPlot <- ggarrange(plots[[1]],plots[[2]],plots[[3]],plots[[4]], ncol=2, nrow=2, common.legend = F, legend = "right"); crossPlot
#ggsave(file="maps_env.svg", plot=crossPlot, width=10, height=11)
legend <- cowplot::get_legend(bs)
grid.draw(legend)
#ggsave(file="legendbotT.svg", plot=legend, width=10, height=8)
# Fig. S6; Depth distribution for all NIS ----
plots = list()
for(i in 1:length(NIS)){
temp <- data[which(data$species == NIS[i]),]
if(NIS[i] == "Marenzelleria"){fill = "rosybrown1"; col = "darkred"}
if(NIS[i] == "Mya.arenaria"){fill = "lightgoldenrod" ; col = "darkgoldenrod"}
if(NIS[i] == "Potamopyrgus.antipodarum"){fill = "skyblue1" ; col = "navy" }
if(NIS[i] == "Streblospio.benedicti"){fill<- "orchid1"; col = "darkorchid" }
if(NIS[i] == "Polydora.cornuta"){fill<- "lightgreen"; col = "darkgreen" }
depth_NIS <- ggplot(temp, aes(x=depth)) +
geom_bar(stat="bin", bins = 15, fill = fill, color = col) +
labs(x = "Depth (m)", y = "Occurrences") + ggtitle(NIS[i]) +
theme_minimal() + theme(legend.position = "right", text = element_text(size = 15), axis.text.y = element_text(size = 15),
axis.text.x = element_text(size = 15)); depth_NIS
p <- list(depth_NIS); names(p) <- NIS[i]
name <- NIS[i]
plots <- append(plots, p)
}
depth_all <- ggplot(data, aes(x=depth)) +
geom_bar(stat="bin", bins = 15, fill ="gray90", color = "black" ) +
labs(x = "Depth (m)", y = "Occurrences") + ggtitle("All NIS") +
theme_minimal() + theme(legend.position = "right", text = element_text(size = 15), axis.text.y = element_text(size = 15),
axis.text.x = element_text(size = 15)); depth_all
plots = append(list(depth_all), plots)
d <- grid.arrange(grobs = plots, nrow = 2); d
#ggsave(file="depth_NIS.svg", plot=d, width=10, height=8)