Figures
Dendrogram nutrients
download
set.seed(1234)
# cluster with euclidean dissim, ward grouping
dend <- dat_frm %>%
vegdist(method = 'euclidean') %>%
hclust(method = 'ward.D2')
# grouping vector
ngrps <- 4
grps <- cutree(dend, k = ngrps)
# adonis(dat_frm ~ grps, method = 'eu', permutations = 1000)
# color vector for groups
cols <- mapviewGetOption("vector.palette")(ngrps)
cols[4] <- 'orange'
colsd <- cols[c(1, 3, 4, 2)]
p1 <- dend %>%
as.dendrogram %>%
set("labels_cex", NA) %>%
color_branches(k = ngrps, col = cols) #%>%
# color_labels(k = ngrps, col = colsd)
jpeg(here('figs', 'siteclsts.jpg'), height = 5.5, width = 7, units = 'in', res = 500, family = 'serif')
par(mar = c(2.5, 4.5, 0.25, 0))
plot(p1, ylab = 'Dissimilarity')
legend('topright', legend = c('1', '2', '3', '4'), fill = colsd, title = 'Groups', bty = 'n')
title(xlab = 'Monitoring stations (153)', line = 0)
dev.off()
knitr::include_graphics(here('figs', 'siteclsts.jpg'))
Pairs plot, nutrients
download
cols <- cols[c(1, 3, 4, 2)]
toplo <- dat_frm %>%
mutate(Groups = as.character(grps))
p <- ggpairs(toplo, mapping = ggplot2::aes(fill = Groups, colour = Groups))
for (row in seq_len(p$nrow))
for (col in seq_len(p$ncol))
p[row, col] <- p[row, col] + scale_colour_manual(values = cols) + scale_fill_manual(values = cols)
# circlize_dendrogram(p1)
jpeg(here('figs', 'prplts.jpg'), height = 7, width = 7, units = 'in', res = 500, family = 'serif')
p
dev.off()
knitr::include_graphics(here('figs', 'prplts.jpg'))
PCA nutrients
download
ppp <- PCA(dat_frm, scale.unit = F, graph = F)
p1 <- ggord(ppp, grp_in = as.character(grps), vec_ext = 3, alpha = 0.8, size = 2, txt = 4, arrow = 0.2, cols = cols, repel = T, coord_fix = F) +
mythm2 +
ggtitle('(a)')
p2 <- ggord(ppp, grp_in = as.character(grps), axes = c('2', '3'), vec_ext = 3, alpha = 0.8, size = 2, txt = 4, arrow = 0.2, cols = cols, repel = T, coord_fix = F) +
mythm2 +
ggtitle('(b)')
p3 <- ggord(ppp, grp_in = as.character(grps), axes = c('1', '3'), vec_ext = 3, alpha = 0.8, size = 2, txt = 4, arrow = 0.2, cols = cols, repel = T, coord_fix = F) +
theme(legend.position = 'top') +
ggtitle('(c)')
pleg <- g_legend(p3)
p3 <- p3 + mythm2
jpeg(here('figs', 'pcanuts.jpg'), height = 4.7, width = 11, units = 'in', res = 500, family = 'serif')
# {p1 + p2 + p3 + plot_layout(ncol = 1)} -
# pleg + plot_layout(ncol = 2, widths = c(1, 0.2))
wrap_elements(pleg) + {p1 + p2 + p3 + plot_layout(ncol = 3)} +
plot_layout(ncol = 1, heights = c(0.2, 1))
dev.off()
knitr::include_graphics(here('figs', 'pcanuts.jpg'))
PCA metals
download
ppp <- PCA(dat_frm_mets, scale.unit = F, graph = F)
p1 <- ggord(ppp, grp_in = as.character(grps), vec_ext = 9, alpha = 0.8, size = 2, txt = 4, arrow = 0.2, cols = cols, coord_fix = F) +
mythm2 +
ggtitle('(a)')
p2 <- ggord(ppp, grp_in = as.character(grps), axes = c('2', '3'), vec_ext = 9, alpha = 0.8, size = 2, txt = 4, arrow = 0.2, cols = cols, coord_fix = F) +
mythm2 +
ggtitle('(b)')
p3 <- ggord(ppp, grp_in = as.character(grps), axes = c('1', '3'), vec_ext = 9, alpha = 0.8, size = 2, txt = 4, arrow = 0.2, cols = cols, coord_fix = F) +
theme(legend.position = 'top') +
ggtitle('(c)')
pleg <- g_legend(p3)
p3 <- p3 + mythm2
jpeg(here('figs', 'pcamets.jpg'), height = 4.7, width = 11, units = 'in', res = 500, family = 'serif')
# {p1 + p2 + p3 + plot_layout(ncol = 1)} -
# pleg + plot_layout(ncol = 2, widths = c(1, 0.2))
wrap_elements(pleg) + {p1 + p2 + p3 + plot_layout(ncol = 3)} +
plot_layout(ncol = 1, heights = c(0.2, 1))
dev.off()
knitr::include_graphics(here('figs', 'pcamets.jpg'))
Metals variable importance
download
tomod <- dat_frm_mets %>%
mutate(grps = factor(grps)) %>%
rename(
amph_surv = `Amph. surv.`,
embr_surv = `Embr. surv.`
)
set.seed(123)
tmp <- randomForest(grps ~ .,
data = tomod,
importance = TRUE,
ntree = 1000, na.action = na.omit)
# plos for variable importance groupings
plos <- tmp$importance %>%
as.data.frame %>%
rownames_to_column('var') %>%
dplyr::select(-MeanDecreaseAccuracy, -MeanDecreaseGini) %>%
gather('grp', 'importance', -var) %>%
group_by(grp) %>%
nest %>%
mutate(
implo = pmap(list(as.character(grp), data), function(grp, data){
colfl <- colgrp %>%
filter(grps %in% grp) %>%
pull(cols)
toplo <- data %>%
arrange(-importance) %>%
.[1:10, ] %>%
mutate(var = factor(var, levels = var))
p <- ggplot(toplo, aes(x = var, y = importance)) +
geom_segment(aes(xend = var, yend = 0)) +
geom_point(fill = colfl, pch = 21, size = 3) +
theme_bw() +
theme(
axis.title.x = element_blank(),
# axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
axis.title.y = element_blank()
) +
ggtitle(grp) +
scale_y_continuous(limits = c(0, 0.26), labels = scales::percent) +
coord_flip()
return(p)
})
)
# variable importance for toxicity
plos2 <- dat_frm_mets %>%
gather('var', 'val', `Amph. surv.`, `Embr. surv.`) %>%
group_by(var) %>%
nest %>%
mutate(
implo = pmap(list(var, data), function(var, data){
tmp <- randomForest(val ~ .,
data = data,
importance = TRUE,
ntree = 1000, na.action = na.omit)
toplo <- tmp$importance %>%
as.data.frame %>%
rownames_to_column('var') %>%
dplyr::select(-IncNodePurity) %>%
rename(importance = `%IncMSE`) %>%
arrange(-importance) %>%
.[1:10, ] %>%
mutate(var = factor(var, levels = var))
p <- ggplot(toplo, aes(x = var, y = importance)) +
geom_segment(aes(xend = var, yend = 0)) +
geom_point(fill = 'grey', pch = 21, size = 3) +
theme_bw() +
theme(
axis.title.x = element_blank(),
# axis.text.x = element_text(angle = 45, hjust = 1, vjust = 1),
axis.title.y = element_blank()
) +
ggtitle(var) +
scale_y_continuous(limits = c(0, 0.41), labels = scales::percent) +
coord_flip()
return(p)
})
)
p1 <- plos$implo[[1]]
p2 <- plos$implo[[2]]
p3 <- plos$implo[[3]]
p4 <- plos$implo[[4]]
p5 <- plos2$implo[[1]]
p6 <- plos2$implo[[2]]
jpeg(here('figs', 'grpimp.jpg'), height = 7.5, width = 8.5, units = 'in', res = 500, family = 'serif')
grid.arrange(
arrangeGrob(p1, p2, p3, p4, ncol = 4, top = '(a) Group variable importance'),
arrangeGrob(p5, p6, ncol = 2, top = '(b) Toxicity variable importance'),
left = grid::textGrob('Importance (% increase MSE)', rot = 90),
ncol = 1, heights = c(1, 1)
)
dev.off()
knitr::include_graphics(here('figs', 'grpimp.jpg'))
Map
download
locs <- dat %>%
select(station, lon, lat) %>%
mutate(station = as.character(station))
prstr <- "+proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0"
toplo <- dat_frm_mets %>%
rownames_to_column('station') %>%
left_join(locs, by = 'station') %>%
mutate(Groups = grps) %>%
filter(!is.na(lon)) %>%
st_as_sf(coords = c('lon', 'lat'), crs = prstr)
mapview(toplo, zcol = 'Groups', col.regions = cols, layer.name = 'Groups')
st_write(toplo, here('data', 'sitegrps.shp'), delete_layer = T)
knitr::include_graphics(here('figs', 'sitemap.jpg'))