# TODO: Add comment
# 
# Author: E.Korsching  2026
###############################################################################



size.sort.panel <- function(x, lead=NULL, sel=NULL, mid="median", ctext=NULL, rtext=NULL, plt="b", title.m="", cex=0.7){
	# find dependencies by ordered (ascending) variable
	# x: get data.frame, variables in columns, at least one column
	#  produce profile graph panels in comparison to ordered master
	# lead: give a column number
	# sel: give a selction of columns up to 10 (or it will become very small)
	# mid: bar graph/ line graph sorted descending around "mean" or "median"
	# ctext: give (row) labels or use the ones in the data.frame
	# rtext: give (col) labels or use the ones in the data.frame
	# plt: plot type bar:"b" or line:"l" or point:"p"
	
	source("../0functions/0general/barplot.ek.R")
	
	if(!is.data.frame(x)){ stop("\n We expect a type  data frame  and variables in columns") }
	if(is.null(lead)|is.null(sel)){ stop("\n We need a master column (lead) and up 9 other columns (sel)") }
	
	# select & arrange columns - lead at first position
	x <- cbind(x[,lead,drop=F],x[,sel,drop=F])
	nr <- nrow(x)
	nc <- ncol(x)
	
	# order according to lead
	x <- x[order(x[,1]),,drop=F]		# info: reorder(Category, Count)
	
	# row names for profiles
	if(is.null(rtext)){
		rtext <- names(x)
	}
	# data point / cases description
	if(is.null(ctext)){
		ctext <- row.names(x)
	}
	
	## start plotting
	layout( matrix(c(1,
					2,
					seq(3,nc+2)),nc+2,1),
			height=c(1,2,rep((20/nc),nc))
	)
#	layout.show(nc+2)
	
	# title
	par(mar=c(0.05,0.05,0.05,0.05))
	plot(0, 0, xlim=c(0, 10), ylim=c(0, 1), type="n", axes=F, xlab="", ylab="" )
	text(x=0.5, y=0.5, labels=title.m, cex=cex+0.2, adj=0)
	
	# top text
	par(mar=c(0.05,0.05,0.05,0.05))
	plot(0, 0, xlim=c(0.4, nr+0.6), ylim=c(0, 1), type="n", axes=F, xlab="", ylab="" )
	for(i in 1:nr){		# top
		text(x=i, y=0.2, ctext[i], cex=cex, srt=45, adj=0)
	}
	
	# profiles
	par(mar=c(0.05,0.05,0.05,0.05))
	
	global.r <- range(x, na.rm=T)
	d.gr <- (global.r[2]-global.r[1])/20
	
	if(plt=="b"){
		for(i in 1:nc){
			if(mid=="median"){
				guide <- median(x[,i], na.rm=T)
			}else{
				guide <- mean(x[,i], na.rm=T)
			}
			plot(0, 0, xlim=c(0.4, nr+0.6), ylim=(global.r-guide), type="n", axes=F, ann=F)		# ann: no xlab ylab
			
			barplot.ek(x=1:nr, y=(x[,i]-guide), col="blue", width=0.8, lwd=0.5, adj=0.5, srt=0, layout="v")
			abline(h=0, col="black")
			text(x=0, y=global.r[2]-guide-d.gr, rtext[i], cex=cex+0.2, srt=0, adj=0)
		}
	}
	if(plt=="l" | plt=="p"){
		for(i in 1:nc){
			if(mid=="median"){
				guide <- median(x[,i], na.rm=T)
			}else{
				guide <- mean(x[,i], na.rm=T)
			}
			plot(0, 0, xlim=c(0.4, nr+0.6), ylim=(global.r-guide), type="n", axes=F, ann=F)		# ann: no xlab ylab
			
			lines(x=1:nr, y=(x[,i]-guide), type=plt, pch=21, col="blue", lwd=0.5) 
			abline(h=0, col="black")
			text(x=0, y=global.r[2]-guide-d.gr, rtext[i], cex=cex+0.2, srt=0, adj=0)
		}
	}
	
	# clean up
	par(mar=c(5,4,4,2))
	par(mfrow=c(1,1))
	return()
}



#aa <- data.frame( matrix(runif(81,0,30),9,9, dimnames=list(c(1:9), c(letters[1:9]))) )
#for(i in 1:ncol(aa)){
#	ac <- c(1:ncol(aa))[-i]
#	cat("\n ac ",ac," i ",i)
#	size.sort.panel(x=aa, lead=i, sel=ac, mid="mean", ctext=NULL, plt="l", title.m="title UU", cex=0.7)
#}



