####################################################################################################
### Code to generate a table with a correlation matrix of hydraulic and physicochemical stream characteristics
####################################################################################################

# Creates Table S4 in
# Klaus, M., Durighetto, N., Khan, A. M., Chatton, E., Öquist, M., Peruzzo, P. and Botter, G. (in revision in "npj Hydrosphere").
# Bubble size spectra reveal distinct scaling patterns and downstream sorting in mountain streams 

###########################################################################################
## created by Marcus Klaus, Swedish University of Agricultural Sciences (SLU), Umeå, Sweden
## contact: marcus.klaus@slu.se
## last update: 2026/08/10
###########################################################################################

lSite <- tapply(log10(DataSpot$l),DataSpot$Site,mean)
QSite <- tapply(DataSpot$Q,DataSpot$Site,max)
hSite <- tapply(DataSpot$h,DataSpot$Site,max)
TempSite <- tapply(DataSpot$Temp,DataSpot$Site,max)
ECSite <- tapply(DataSpot$EC,DataSpot$Site,max)
pHSite <- tapply(DataSpot$pH,DataSpot$Site,max)
WidthSite <- tapply(DataSpot$Width,DataSpot$Site,max)
PoolLengthSite <- tapply(DataSpot$PoolLength,DataSpot$Site,max)

## make Table S4
TableS4 <- cor(cbind(
  log10(QSite),
  log10(hSite),
  log10(WidthSite),
  log10(PoolLengthSite),
  lSite,
  TempSite,
  log10(ECSite),
  pHSite),method = c("pearson")
)

colnames(TableS4) <- c("log10(Q)","log10(h)","log10(Width)","log10(PoolLength)","log10(l)","Temp","log10(EC)","pH")
TableS4 <- data.frame(c("log10(Q)","log10(h)","log10(Width)","log10(PoolLength)","log10(l)","Temp","log10(EC)","pH"),TableS4)
colnames(TableS4)[1] <- ""
rownames(TableS4) <- 1:nrow(TableS4)

print(TableS4)
## additional information to Table S4
print(paste0("correlation d vs l: ",cor(log10(DataSpot$d),log10(DataSpot$l))))
