################################################################
# Fig. S5 flow velocity patterns within step-pool systems
################################################################

# Creates Figure S5 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/04/30
###########################################################################################

## u is upstream (negative = flow downstream)
## v is cross-stream  (negative = towards right)
## w is vertical  (negative = downwards)



windows(width = 12, height = 8)
layout(matrix(1:6, 2, 3, byrow = TRUE))
par(mar=c(4.5,5.5,0.5,0.5),las=1,cex=1,mgp=c(3.6, 1, 0))



y <- -log10(DataSpot$l)
x <- log10(DataSpot$d)
# Remove NA pairs
ok <- !is.na(x) & !is.na(y)
x_clean <- x[ok]
y_clean <- y[ok]
# Sort by x
ord <- order(x_clean)
x_sorted <- x_clean[ord]
y_sorted <- y_clean[ord]
# Base scatter plot
plot(x_sorted, y_sorted, pch=1, col=adjustcolor("black",alpha.f=0.2), main="",xaxt="n",yaxt="n", ylab="l (cm)",xlab="d (cm)")
abline(h=0,lty=3)
# Fit loess model
loess_fit <- loess(y_sorted ~ x_sorted)
# Predictions with standard errors
pred <- predict(loess_fit, se=TRUE)
# Add LOESS line
lines(x_sorted, pred$fit, col="blue", lwd=2)
# Add 95% confidence interval
lines(x_sorted, pred$fit + 1.96*pred$se.fit, col="blue", lty=2)
lines(x_sorted, pred$fit - 1.96*pred$se.fit, col="blue", lty=2)
original_ticks <- c(0.1,1,10,100,1000)
axis(1, at=log10(original_ticks), labels=original_ticks)
minor_ticks <- c(2:9, 20,30,40,50,60,70,80,90, 200,300,400,500,600,700,800,900, 2000:9000)  # extend as needed
axis(1, at=log10(minor_ticks), labels=FALSE, tck=-0.03)  # tck sets tick length (negative for outside)

original_ticks <- c(0.1,1,10,20,100,1000)
axis(2, at=-log10(original_ticks), labels=-original_ticks)
minor_ticks <- c(2:9/10, 2:9, 20,30,40,50,60,70,80,90, 200,300,400,500,600,700,800,900, 2000:9000)  # extend as needed
axis(2, at=-log10(minor_ticks), labels=FALSE, tck=-0.03)  # tck sets tick length (negative for outside)

text(min(x_sorted)-1,max(y_sorted)-0.01,"A)",font=2,xpd=TRUE)



y <- log10(DataSpot$Width)
x <- log10(DataSpot$d)
# Remove NA pairs
ok <- !is.na(x) & !is.na(y)
x_clean <- x[ok]
y_clean <- y[ok]
# Sort by x
ord <- order(x_clean)
x_sorted <- x_clean[ord]
y_sorted <- y_clean[ord]
# Base scatter plot
plot(x_sorted, y_sorted, pch=1, col=adjustcolor("black",alpha.f=0.2), main="",xaxt="n",yaxt="n", ylab="Pool width (m)",xlab="d (cm)")
abline(h=0,lty=3)
# Fit loess model
loess_fit <- loess(y_sorted ~ x_sorted)
# Predictions with standard errors
pred <- predict(loess_fit, se=TRUE)
# Add LOESS line
lines(x_sorted, pred$fit, col="blue", lwd=2)
# Add 95% confidence interval
lines(x_sorted, pred$fit + 1.96*pred$se.fit, col="blue", lty=2)
lines(x_sorted, pred$fit - 1.96*pred$se.fit, col="blue", lty=2)
original_ticks <- c(0.1,1,10,100,1000)
axis(1, at=log10(original_ticks), labels=original_ticks)
minor_ticks <- c(2:9, 20,30,40,50,60,70,80,90, 200,300,400,500,600,700,800,900, 2000:9000)  # extend as needed
axis(1, at=log10(minor_ticks), labels=FALSE, tck=-0.03)  # tck sets tick length (negative for outside)

axis(2, at=log10(original_ticks), labels=original_ticks)
minor_ticks <- c(2:9, 20,30,40,50,60,70,80,90, 200,300,400,500,600,700,800,900, 2000:9000)  # extend as needed
axis(2, at=log10(minor_ticks), labels=FALSE, tck=-0.03)  # tck sets tick length (negative for outside)

text(min(x_sorted)-1,max(y_sorted)-0.01,"B)",font=2,xpd=TRUE)


y <- DataSpot$Vadv
# Remove NA pairs
ok <- !is.na(x) & !is.na(y)
x_clean <- x[ok]
y_clean <- y[ok]
# Sort by x
ord <- order(x_clean)
x_sorted <- x_clean[ord]
y_sorted <- y_clean[ord]
# Base scatter plot
plot(x_sorted, y_sorted, pch=1, col=adjustcolor("black",alpha.f=0.2), main="",xaxt="n", ylab=expression(paste(V[adv]," (m ", s^-1,")")),xlab="d (cm)")
abline(h=0,lty=3)
# Fit loess model
loess_fit <- loess(y_sorted ~ x_sorted)
# Predictions with standard errors
pred <- predict(loess_fit, se=TRUE)
# Add LOESS line
lines(x_sorted, pred$fit, col="blue", lwd=2)
# Add 95% confidence interval
lines(x_sorted, pred$fit + 1.96*pred$se.fit, col="blue", lty=2)
lines(x_sorted, pred$fit - 1.96*pred$se.fit, col="blue", lty=2)
original_ticks <- c(0.1,1,10,100,1000)
axis(1, at=log10(original_ticks), labels=original_ticks)
minor_ticks <- c(2:9, 20,30,40,50,60,70,80,90, 200,300,400,500,600,700,800,900, 2000:9000)  # extend as needed
axis(1, at=log10(minor_ticks), labels=FALSE, tck=-0.03)  # tck sets tick length (negative for outside)

text(min(x_sorted)-1,max(y_sorted)-0.01,"C)",font=2,xpd=TRUE)



x <- log10(DataSpot$d)
y <- DataSpot$Vu
# Remove NA pairs
ok <- !is.na(x) & !is.na(y)
x_clean <- x[ok]
y_clean <- y[ok]
# Sort by x
ord <- order(x_clean)
x_sorted <- x_clean[ord]
y_sorted <- y_clean[ord]
# Base scatter plot
plot(x_sorted, y_sorted, pch=1, col=adjustcolor("black",alpha.f=0.2), main="",xaxt="n", ylab=expression(paste("u (m ", s^-1,")")),xlab="d (cm)")
abline(h=0,lty=3)
# Fit loess model
loess_fit <- loess(y_sorted ~ x_sorted)
# Predictions with standard errors
pred <- predict(loess_fit, se=TRUE)
# Add LOESS line
lines(x_sorted, pred$fit, col="blue", lwd=2)
# Add 95% confidence interval
lines(x_sorted, pred$fit + 1.96*pred$se.fit, col="blue", lty=2)
lines(x_sorted, pred$fit - 1.96*pred$se.fit, col="blue", lty=2)
original_ticks <- c(0.1,1,10,100,1000)
axis(1, at=log10(original_ticks), labels=original_ticks)
minor_ticks <- c(2:9, 20,30,40,50,60,70,80,90, 200,300,400,500,600,700,800,900, 2000:9000)  # extend as needed
axis(1, at=log10(minor_ticks), labels=FALSE, tck=-0.03)  # tck sets tick length (negative for outside)

text(log10(230),-1.05,"downstream",cex=0.9)
text(log10(230),0.4,"upstream",cex=0.9)

text(min(x_sorted)-1,max(y_sorted)-0.01,"D)",font=2,xpd=TRUE)


y <- DataSpot$Vv
# Remove NA pairs
ok <- !is.na(x) & !is.na(y)
x_clean <- x[ok]
y_clean <- y[ok]
# Sort by x
ord <- order(x_clean)
x_sorted <- x_clean[ord]
y_sorted <- y_clean[ord]
# Base scatter plot
plot(x_sorted, y_sorted, pch=1, col=adjustcolor("black",alpha.f=0.2), main="",xaxt="n", ylab=expression(paste("v (m ", s^-1,")")),xlab="d (cm)")
abline(h=0,lty=3)
# Fit loess model
loess_fit <- loess(y_sorted ~ x_sorted)
# Predictions with standard errors
pred <- predict(loess_fit, se=TRUE)
# Add LOESS line
lines(x_sorted, pred$fit, col="blue", lwd=2)
# Add 95% confidence interval
lines(x_sorted, pred$fit + 1.96*pred$se.fit, col="blue", lty=2)
lines(x_sorted, pred$fit - 1.96*pred$se.fit, col="blue", lty=2)
original_ticks <- c(0.1,1,10,100,1000)
axis(1, at=log10(original_ticks), labels=original_ticks)
minor_ticks <- c(2:9, 20,30,40,50,60,70,80,90, 200,300,400,500,600,700,800,900, 2000:9000)  # extend as needed
axis(1, at=log10(minor_ticks), labels=FALSE, tck=-0.03)  # tck sets tick length (negative for outside)

text(log10(230),-0.28,"rightward",cex=0.9)
text(log10(250),0.8,"leftward",cex=0.9)

text(min(x_sorted)-1,max(y_sorted)-0.01,"E)",font=2,xpd=TRUE)

y <- DataSpot$Vw
# Remove NA pairs
ok <- !is.na(x) & !is.na(y)
x_clean <- x[ok]
y_clean <- y[ok]
# Sort by x
ord <- order(x_clean)
x_sorted <- x_clean[ord]
y_sorted <- y_clean[ord]
# Base scatter plot
plot(x_sorted, y_sorted, pch=1, col=adjustcolor("black",alpha.f=0.2), main="",xaxt="n", ylab=expression(paste("w (m ", s^-1,")")),xlab="d (cm)")
abline(h=0,lty=3)
# Fit loess model
loess_fit <- loess(y_sorted ~ x_sorted)
# Predictions with standard errors
pred <- predict(loess_fit, se=TRUE)
# Add LOESS line
lines(x_sorted, pred$fit, col="blue", lwd=2)
# Add 95% confidence interval
lines(x_sorted, pred$fit + 1.96*pred$se.fit, col="blue", lty=2)
lines(x_sorted, pred$fit - 1.96*pred$se.fit, col="blue", lty=2)
original_ticks <- c(0.1,1,10,100,1000)
axis(1, at=log10(original_ticks), labels=original_ticks)
minor_ticks <- c(2:9, 20,30,40,50,60,70,80,90, 200,300,400,500,600,700,800,900, 2000:9000)  # extend as needed
axis(1, at=log10(minor_ticks), labels=FALSE, tck=-0.03)  # tck sets tick length (negative for outside)

text(log10(240),-0.7,"downward",cex=0.9)
text(log10(250),0.7,"upward",cex=0.9)

text(min(x_sorted)-1,max(y_sorted)-0.01,"F)",font=2,xpd=TRUE)




