plotLR=function(df, right=F, intervals=unique(df$interval), start=min(df$n), end=max(df$n)) {
	right0=right
	ggplot(data=subset(df, right == right0 & interval %in% intervals & n >= start & n<=end),
		aes(x=n, y=alpha, group=Intervalle, col=Intervalle, shape=Intervalle))+
		geom_point(cex=2)+
		geom_hline(yintercept=0.025*c(1/1.5,1.5),size=0.5)+
		geom_hline(yintercept=0.025,size=1)+
		xlab("Espérance du nombre d'événements")+
		ylab(paste0("Alpha unilatéral (IC95 ",ifelse(right,"trop haut","trop bas"),")"))+
		geom_line()
}
plotLRx=function(df, right=F, intervals=unique(df$interval), start=min(df$n), end=max(df$n),conf.levels=0.95) {
	right0=right
	df$Interval=df$Intervalle
	df$Interval[df$Interval == "Mod. Wilson"]="My mod. Wilson";
	df$Nname=paste0("Sample size N=",df$N)
	ggplot(data=subset(df, right == right0 & interval %in% intervals & n >= start & n<=end & conf.level %in% conf.levels),
		aes(x=n/N, y=1-alpha, group=Interval, col=Interval, shape=Intervalle))+
		geom_hline(yintercept=0.95,size=1)+
		xlab("Theoretical proportion")+
		ylab("Coverage probability")+
		geom_line(aes(linetype=Interval))+
		ylim(c(0.84,1))+
		facet_wrap(~ Nname,nrow=1,ncol=2,scales="free")
}


#font_import()
loadfonts(device = "win")

fontMath="Cambria"
fontMath="sans"
mytheme=theme_bw()+
		theme(panel.grid.major = element_blank(),
  			panel.grid.minor = element_blank())+
#		theme(axis.title = element_text(face="italic", size = 12 ))+
		theme(legend.title = element_text(size = 11, face="bold"))+
		theme(legend.text = element_text(size = 12, family=fontMath),
			legend.key.height = unit(1,"cm"),
			legend.text.align = 0)+
		theme(legend.position = "bottom")

subLR=function(df, intervals, start, end, conf.levels) {
	if (nrow(df)==0) {
		stop("subLR attend un data.frame non vide !")
	}
	df=subset(df, !is.na(right) & conf.level %in% conf.levels & interval %in% intervals);
	if (nrow(df)==0) {
		tempdf <<- df;
		tempintervals <<- intervals
		tempconf.levels <<- conf.levels
		if (nrow(subset(df, !is.na(right)))==0) {
			stop("subLR renvoie un ensemble vide parce que la variable 'right' manque dans le df");
		} else if (nrow(subset(df, conf.level %in% conf.levels))==0) {
			stop("subLR renvoie un ensemble vide à cause de conf.levels");
		} else if (nrow(subset(df, interval %in% intervals))==0) {
			stop("subLR renvoie un ensemble vide à cause des intervalles");
		} else {
			stop("subLR renvoie un ensemble vide à cause de la combinaison de 'right', conf.levels et des intervalles");
		}
	}
	dftemp<<-df
	endup = max(na.omit(sapply(unique(df$N), function (N) {
		v = df$n[df$N == N & df$n >= end] 
		if (length(v)==0) {end} else {min (v)}
	})))
	subset(df, n >= start & n <=endup)
}


scale_xy=function(obj,xlim, ylim, xstep, ystep) {
	obj+
		scale_x_continuous(
					minor_breaks=seq(xlim[1],xlim[2],xstep/2),
					breaks=seq(xlim[1],xlim[2],xstep),
					expand=c(0,0))+
		scale_y_continuous(limits=c(min(ylim),max(ylim)+ylimoff),
					minor_breaks=seq(ylim[1],ylim[2],ystep/2),
					breaks=seq(ylim[1],ylim[2],ystep),
					expand=c(0,0))+
		coord_cartesian(xlim=xlim, ylim=ylim)+
		mytheme
}

hline25=function(obj,yvar, conf.level=0.95) {
	if (length(conf.level)>1) {conf.level=0.95;}
	halpha = (1-conf.level)/2
	if (grepl("alpha", yvar)) {
		obj = obj +
		geom_hline(yintercept=halpha*c(1/1.5,1.5),size=0.5,col="gray")+
		geom_hline(yintercept=halpha,size=1)
	} else if (yvar == "width2") {
		obj = obj +
		geom_hline(yintercept=1,size=0.5, col="gray")
	}
	obj = obj + geom_line()
	obj
}

prepareDFplot=function(df, intervals, start, end, conf.levels=0.95, bilat=F) {
	if (nrow(df)==0) {
		stop("prepareDFplot attend un df non vide")
	}
	df=subLR(df, intervals, start, end, conf.levels)
	if (length(conf.levels)>1) {df=subset(df, right == 1);right=F;}
	if (!bilat) {df=subset(df, right!=2)}
	df$Dénominateur=factor(paste0("n = ",df$N), levels=paste0("n = ",sort(unique(df$N))))
	if (bilat) {
		df$Erreur=factor(df$right, levels=0:2, labels=c("lower", "upper", "two-sided"))
	} else {
		df$Erreur=factor(df$right, levels=0:1, labels=c("lower", "upper"))
	}
	if (length(conf.levels) > 1) {
		df$color = factor(df$conf.level);
	} else {
		df$color = df$Erreur;
	}
	df$ColN=paste(df$color, df$N, df$interval)
	#df$ErreurN=paste(df$Erreur, df$N)
	#df$Intervalle=factor(df$Intervalle, levels=sapply(intervals, function(interval) {df$Intervalle[df$interval == interval][1]}))
	intervals = unique(intervals)
	intervalnames = sapply(intervals, function (x) {
		if (is.null(cinames0[[x]])) {return(x)}
		return(cinames0[[x]])
	})
	if (any(duplicated(intervalnames))) {
		stop("Doublon dans les noms d'intervalle : ", intervalnames[duplicated(intervalnames)]);
	}
	df$Intervalle = factor(df$interval, levels=intervals, labels=intervalnames);
	df
}

en_environment=function() {
	xlabN<<-expression(paste(lambda," = expected number of successes"))
	lblSampleSize<<-"Sample size"
	lblSampleSizeppp<<-"Expected sample size"
	lblUB <<- " (upper bound)"
	lblLB <<- " (lower bound)"
	lblBB <<- " (two-sided)"
	lblHLR <<- "Half-width ratio"
	lblRISK <<- "Error"
	ylablegend<<-list(
		width2=lblHLR,
		alphap=expression(paste(italic(paste(alpha,"'")), " error")),
		alphapp=expression(paste(italic(paste(alpha,"''")), " error")),
		alphappp=expression(paste(italic(paste(alpha,"'''")), " error"))
	)

	yvarlegend<<-list(
		width2=c(
					expression(paste(italic(v[L]^"''")," (lower bound)")),
					expression(paste(italic(v[U]^"''")," (upper bound)")),
					expression(paste(italic(v^"''")," (two-sided)"))
				),
		alphap=c(
					expression(paste(italic(alpha[L]^"'")," (lower bound)")),
					expression(paste(italic(alpha[U]^"'")," (upper bound)")),
					expression(paste(italic(alpha^"'")," (two-sided)"))
				),
		alphapp=c(
					expression(paste(italic(alpha[L]^"''")," (lower bound)")),
					expression(paste(italic(alpha[U]^"''")," (upper bound)")),
					expression(paste(italic(alpha^"''")," (two-sided)"))
				),
		alphappp=c(
					expression(paste(italic(alpha[L]^"'''")," (lower bound)")),
					expression(paste(italic(alpha[U]^"'''")," (upper bound)")),
					expression(paste(italic(alpha^"'''")," (two-sided)"))
				)
	)

	cinames0 <<- cinamesen0
	complete_environment()
	riskname<<-list(
		width2=lblHLR,
		alphap="Actual conditional error",
		alphapp="Actual local average error",
		alphappp="Actual average error (random sample size)"
	)
	lblInfiniteSampleSize <<- expression(bold(paste("Sample size ", n %->% +infinity, " (Poisson)")))
	langdir <<- "en"
	is_french <<- FALSE
}
fr_environment=function() {
	xlabN<<-expression(paste(lambda," = nombre de succès attendu"))
	lblSampleSize<<-"Taille de l'échantillon"
	lblSampleSizeppp<<-"Taille attendue de l'échantillon"
	lblUB <<- " (borne haute)"
	lblLB <<- " (borne basse)"
	lblBB <<- " (bilatéral)"
	lblHLR <<- "Demi-largeur relative"
	lblRISK <<- "Risque"
	ylablegend<<-list(
		width2=lblHLR,
		alphap=expression(paste("Risque ", italic(paste(alpha,"'")))),
		alphapp=expression(paste("Risque ", italic(paste(alpha,"''")))),
		alphappp=expression(paste("Risque ", italic(paste(alpha,"'''"))))
	)
	cinames0 <<- cinamesfr0
	yvarlegend<<-list(
		width2=c(
					expression(paste(italic(v[l]^"''")," (borne basse)")),
					expression(paste(italic(v[u]^"''")," (borne haute)")),
					expression(paste(italic(v^"''")," (bilatéral)"))
				),
		alphap=c(
					expression(paste(italic(alpha[l]^"'")," (borne basse)")),
					expression(paste(italic(alpha[u]^"'")," (borne haute)")),
					expression(paste(italic(alpha^"'")," (bilatéral)"))
				),
		alphapp=c(
					expression(paste(italic(alpha[l]^"''")," (borne basse)")),
					expression(paste(italic(alpha[u]^"''")," (borne haute)")),
					expression(paste(italic(alpha^"''")," (bilatéral)"))
				),
		alphappp=c(
					expression(paste(italic(alpha[l]^"'''")," (borne basse)")),
					expression(paste(italic(alpha[u]^"'''")," (borne haute)")),
					expression(paste(italic(alpha^"'''")," (bilatéral)"))
				)
	)
	complete_environment()
	riskname<<-list(
		width2=lblHLR,
		alphap="Risque réel conditionnel",
		alphapp="Risque réel local moyen",
		alphappp="Risque réel moyen (taille d'échantillon aléatoire)"
	)
	lblInfiniteSampleSize <<- expression(bold(paste("Taille de l'échantillon ", n %->% +infinity, " (Poisson)")))
	langdir <<- "fr"
	is_french <<- TRUE
}

fr_environment2=function() {
	xlabN<<-"np = Nombre de succès attendu"
	lblSampleSize<<-"Taille de l'échantillon"
	lblSampleSizeppp<<-"Taille attendue de l'échantillon"
	lblUB <<- " (borne haute)"
	lblLB <<- " (borne basse)"
	lblBB <<- " (bilatéral)"
	lblHLR <<- "Demi-largeur relative"
	lblRISK <<- "Risque"
	ylablegend<<-list(
		width2=lblHLR,
		alphap=expression(paste("Risque ", italic(paste(alpha,"'")))),
		alphapp=expression(paste("Risque ", italic(paste(alpha,"''")))),
		alphappp=expression(paste("Risque ", italic(paste(alpha,"'''"))))
	)
	cinames0 <<- cinamesfr0
	yvarlegend<<-list(
		width2=c(
					expression(paste(italic(v[l]^"''")," (borne basse)")),
					expression(paste(italic(v[u]^"''")," (borne haute)")),
					expression(paste(italic(v^"''")," (bilateral)"))
				),
		alphap=c(
					expression(paste(italic(alpha[l]^"'")," (borne basse)")),
					expression(paste(italic(alpha[u]^"'")," (borne haute)")),
					expression(paste(italic(alpha^"'")," (bilateral)"))
				),
		alphapp=c(
					expression(paste(italic(alpha[l]^"''")," (borne basse)")),
					expression(paste(italic(alpha[u]^"''")," (borne haute)")),
					expression(paste(italic(alpha^"''")," (bilateral)"))
				),
		alphappp=c(
					expression(paste(italic(alpha[l]^"'''")," (borne basse)")),
					expression(paste(italic(alpha[u]^"'''")," (borne haute)")),
					expression(paste(italic(alpha^"'''")," (bilateral)"))
				)
	)
	complete_environment()
	riskname<<-list(
		width2=lblHLR,
		alphap="Risque réel conditionnel",
		alphapp="Risque réel local moyen",
		alphappp="Risque réel moyen (taille d'échantillon aléatoire)"
	)
	lblInfiniteSampleSize <<- expression(bold(paste("Taille de l'echantillon ", n %->% +infinity, " (Poisson)")))
	langdir <<- "fr"
	is_french <<- TRUE
}

complete_environment=function() {
	ylimoff<<-1
	ylimoffw<<-1
	riskname<<-list(
		width2=lblHLR,
		alphap=lblRISK,
		alphapp=lblRISK,
		alphappp=lblRISK
	)
	lblInfiniteSampleSize = expression(n %->% +infinity)
}

enus=function(s) {
	if (is_french) {gsub("[.]", ",",s)} else {gsub(",", ".",s)}
}

if (is_french) {fr_environment()} else {en_environment()}

rgb255=function(v) {v=v/255;rgb(v[1],v[2],v[3])}
palcols=c(rgb255(c(248,118,109)), rgb255(c(0,186,56)), rgb255(c(97,156,255)))

constrain_ycoords=function(y,x,ymax) { # Plafonne les coordonnées y qui débordent du max
	y=pmin(y,ymax)
	list(y,x)
}

constrain_xcoords=function(x,y,xmax) { # Plafonne les coordonnées x qui débordent du max
	# x est ordonné du plus petit au plus grand
	bad = which(x > xmax)
	deltax = (xmax - x[bad-1])
	pente = (y[bad] - y[bad-1])/(x[bad] - x[bad-1])
	y[bad] = y[bad-1] + pente*deltax
	x[bad] = xmax
	list(x,y)
}

#constrain_xcoords(c(6, 8,10,12), c(32,31,30,29), 10.5)

dfconstrain_coords=function(df, ylim, start, end) {
	l=constrain_ycoords(df$alpha, df$n, ylim[2]*1.01); df$alpha=l[[1]]; df$n=l[[2]]
	l=constrain_xcoords(df$n, df$alpha, end); df$n=l[[1]]; df$alpha=l[[2]]
	df
}

plotLR2=function(df,
			intervals=unique(df$interval),
			start=min(df$n),
			end=min(32,max(df$n)),
			ylim=c(0,0.07),
			step=0.01,
			xstep=4,
			conf.levels=0.95,
			nrow=2,
			ncol=2,
			yvar="alpha"
			) {
	if (nrow(df)==0) {
		stop("plotLR2 attend un df non vide")
	}
	if (!(yvar %in% colnames(df))) {
		stop("La variable ", yvar, " devrait être dans le data.frame passé à plotLR2")
	}
	df=prepareDFplot(df, intervals, start, end, conf.levels, bilat=F)

	df$alpha=df[,yvar]
	df=dfconstrain_coords(df, ylim, start, end)
	yvar = ifelse(yvar == "alpha", "alphapp", yvar)
	df$ltype=ifelse(df$right,"solid","longdash")
	obj = ggplot(data=df,
		aes(x=n, y=alpha, linetype=color, group=ColN, col=Dénominateur))+
		xlab(xlabN)+
		scale_linetype_manual(
			name=riskname[[yvar]],
			values=c("longdash","solid", "dotted"),
			breaks=c("lower", "upper", "two-sided"),
			labels=(yvarlegend[[yvar]])
		)
	if (length(unique(df$N)) == 2 & length(setdiff(unique(df$N), c(2048,Inf)))==0) {
		obj = obj +
			scale_color_manual(
			name=ifelse(yvar=="alphappp",lblSampleSizeppp, lblSampleSize),
			values=c("red","blue"),
			breaks=c("n = 2048", "n = Inf"),
			labels=c(expression("n = 2048"), expression("n" %->% +infinity))
		)
	} else if (!is.null(df$SRs)) {
		obj = obj +
			scale_color_manual(
				name=ifelse(yvar=="alphappp",lblSampleSizeppp, lblSampleSize),
				values=c(palcols[1],palcols[2], palcols[3]),
				breaks=c("n = 32", "n = 64", "n = 2048"),
				labels=c(
					expression(paste(n[0], "= 32")),
					expression(paste(n[0], "= 64")),
					expression(paste(n[0], "= 2048"))
				)
			)
	} else {
		obj = obj +
			scale_colour_discrete  (name=ifelse(yvar=="alphappp",lblSampleSizeppp, lblSampleSize))
	}
	obj = obj + guides(colour = guide_legend(order = 1, ncol=3, direction="horizontal",title.position="top"), linetype = guide_legend(order = 2, ncol=3, direction="horizontal",title.position="top"))
	obj = obj + ylab(ylablegend[[yvar]])
	obj = hline25(obj,yvar,conf.levels)
	obj = scale_xy(obj, c(start, end), ylim, xstep, step)
	obj = obj + facet_wrap(~ Intervalle,nrow=nrow,ncol=ncol,scales="free")
	if (!is.null(df$ORs) && length(unique(df$ORs))==1) {
		ORs = unique(df$ORs)
		if (is_french) {
			if (ORs == 1.20) {expr=expression(paste(OR[S]," = 1,20"))}
			if (ORs == 1.10) {expr=expression(paste(OR[S]," = 1,10"))}
			if (ORs == 1.05) {expr=expression(paste(OR[S]," = 1,05"))}
		} else {
			if (ORs == 1.20) {expr=expression(paste(OR[S]," = 1.20"))}
			if (ORs == 1.10) {expr=expression(paste(OR[S]," = 1.10"))}
			if (ORs == 1.05) {expr=expression(paste(OR[S]," = 1.05"))}
		}
		obj = add_sub(obj, expr,size=14, fontface="bold" )
	}
	if (!is.null(df$SRs) && length(unique(df$SRs))==1) {
		SRs= unique(df$SRs)
		if (is_french) {
			if (SRs == 1.20) {expr=expression(paste(SR[S]," = 1,20"))}
			if (SRs == 1.10) {expr=expression(paste(SR[S]," = 1,10"))}
			if (SRs == 1.05) {expr=expression(paste(SR[S]," = 1,05"))}
		} else {
			if (SRs == 1.20) {expr=expression(paste(SR[S]," = 1.20"))}
			if (SRs == 1.10) {expr=expression(paste(SR[S]," = 1.10"))}
			if (SRs == 1.05) {expr=expression(paste(SR[S]," = 1.05"))}
		}
		obj = add_sub(obj, expr,size=14, fontface="bold" )
	}
	#obj = add_sub(obj, expression(paste(logit(italic(P)),"~N avec ",exp(sqrt(VAR(italic(P)))), "=", OR[S], "=", "1,20")),size=16, fontface="bold" )
	ggdraw(obj)
}
#print(plotLR2(df4N, "ciasinbartlett", start=0, end=32,ylim=c(0,0.06),xstep=2,step=0.005,nrow=1,ncol=1, yvar="alphappp"))

#ggdraw(plotLR2(df4N, "ciasinbartlett", start=0, end=32,ylim=c(0,0.06),xstep=2,step=0.005,nrow=1,ncol=1, yvar="alphappp"))
#plotgroups(groups_article, subset(df0, N %in% c(2048,Inf)), NULL, suffix=".inf")

colorset=c("red", rgb(0,0.7,0), "blue")
plotLR3=function(df, intervals=unique(df$interval), start=min(df$n), end=min(32,max(df$n)),ylim=c(0,0.07),step=0.005,bilat=F,xstep=2,conf.levels=0.95,nrow=2,ncol=2,draw_points=F,yvar="alpha") {
	if (nrow(df)==0) {
		stop("plotLR3 attend un df non vide")
	}
	if (!(yvar %in% colnames(df))) {
		stop("La variable ", yvar, " devrait être dans le data.frame passé à plotLR3")
	}
	df = prepareDFplot(df, intervals, start, end, conf.levels=conf.levels, bilat=bilat)
	df = df[order(df$color,decreasing=T),]

	if (yvar == "alpha" & length(unique(df$right))>=3) {
		# On veut supprimer les superpositions de lignes...
		# Lorsqu'un risque alpha unilatéral est nul, il y a superposition du risque non nul avec le risque bilatéral
		# On doit donc supprimer les observations bilatérales
		# En gros, on supprime les right==2 lorsqu'un right %in% c(0,1) est nul
		# Sauf le 1er d'une séquence... Afin que la ligne bleue n'apparaisse pas de nulle part

		df$id = paste(df$conf.level, df$interval, df$N, df$n)
		dfx=df[,c("id","alpha","right")]
		dfy=reshape(dfx,direction="wide",idvar="id", v.names="alpha",timevar="right")
		dfid=t(sapply(strsplit(dfy$id, " "), identity))
		dfid=data.frame(conf.level=as.numeric(dfid[,1]), interval=dfid[,2], N=as.numeric(dfid[,3]), n=as.numeric(dfid[,4]))
		dfy = cbind(dfid, dfy)
		dfy = dfy[order(dfy$conf.level, dfy$interval, dfy$N, dfy$n),]
		coreid = paste(dfy$conf.level, dfy$interval, dfy$N)

		bad = abs(dfy$alpha.0 < 1e-8) | abs(dfy$alpha.1) < 1e-8
		cur =1:(length(bad)-1)
		nxt =2:(length(bad))
		# (coreid[prev] != coreid[cur])
		good = !bad
		good[cur] = good[cur] | (good[nxt] & coreid[nxt] == coreid[cur]) # On conserve le 1er d'une séquence de mauvais (s'il y a des observations juste avant)
		bad = !good
		badid = dfy$id[bad]

		df = df[!(df$id %in% badid & df$right==2),]
		df$id=NULL
		tempdf <<- df
	}

	df$alpha=df[,yvar]
	df=dfconstrain_coords(df, ylim, start, end)
	#df$alpha = pmin(df$alpha, ylim[2]*1.01)
	yvar = ifelse(yvar == "alpha", "alphap", yvar)
	obj = ggplot(data=df,
		aes(x=n, y=alpha, group=paste(color, N, Intervalle), col=color))+
		xlab(xlabN)+
		ylab(ylablegend[[yvar]])
	if (draw_points) {
		obj=obj+geom_point(aes(shape=Erreur))
	}
	# else {
	#	obj=obj+geom_line()
	#}
	obj = obj +
		scale_color_manual(
			name=riskname[[yvar]],
			values=colorset,
			breaks=c("lower", "upper", "two-sided"),
			labels=(yvarlegend[[yvar]])
		)


	obj = obj + guides(colour = guide_legend(order = 1, ncol=3, direction="horizontal",title.position="top"), linetype = guide_legend(order = 2, ncol=3, direction="horizontal",title.position="top"))

	obj = hline25(obj,yvar,conf.levels)
	obj = obj+geom_hline(yintercept=0.05,size=0.5,col="light blue")
	obj = scale_xy(obj, c(start, end), ylim, xstep, step)+
		facet_wrap(~ Intervalle,nrow=nrow,ncol=ncol,scales="free")

	if (!is.null(df$N) && length(unique(df$N))==1) {
		N = unique(df$N)
		if (N == Inf) {expr=lblInfiniteSampleSize;} else {expr=paste0(lblSampleSize, " n = ", N)}
		obj = add_sub(obj, expr,size=12, fontface="bold" )
	}
	ggdraw(obj)
}

#plotgroups(groups_article, subset(df0, is.finite(N)), subset(df3C, N==2048))


#subset(df3C, interval=="ciz" & conf.level==0.95 & N==32 & is.finite(N) & n < 5 & n>4 & right==1)

#print(plotLR2(df0, groups[[grp]],start=0,end=32,ylim=c(0.7,1.4),step=0.1,xstep=4,nrow=nrow,ncol=ncol,yvar="width2"))
#print(plotLR2(df0, groups[[grp]],start=0,end=32,ylim=c(0,0.07),step=0.01,xstep=4,nrow=nrow,ncol=ncol,yvar="alpha"))




langfile=function(file,suffix) {
	paste0(langdir, "/", file, suffix)
}
dirpdf=function(filename,width=10,height=8) {
	pdf(langfile(filename, ".pdf"),width=width, height=height)
}
dirsvg=function(filename,width=10,height=8) {
	svg(langfile(filename, ".svg"),width=width, height=height)
}
diremf=function(filename,width=10,height=8) {
	win.metafile(langfile(filename, ".emf"),width=width, height=height)
}
direps=function(filename,width=10,height=8) {
	setEPS()
	postscript(langfile(filename,".eps"),width=width,height=height)
}

dirfile0=dirsvg

end2xstep=function(end) {
	2^(ceiling(log2(end))-4)
}

plotgroups=function(groups, df0, df3C, suffix="", dirfile=dirfile0, conditional_xstart=0, conditional_xend=10, infile=T, draw_local_width=T, draw_cond_width=F, draw_local_alpha=T, draw_cond_alpha=T, conf.level=0.95, width=10, height=8, ...) {
if (!is.null(df0) && nrow(df0)==0) {
	stop("plotgroups attend un df0 NULL ou avec un nombre de lignes >= 1")
}
if (!is.null(df3C) && nrow(df3C)==0) {
	stop("plotgroups attend un df3C NULL ou avec un nombre de lignes >= 1")
}
for(grp in names(groups)) {
	cat("Groupe :",grp,"\n")
	end = ifelse(grepl("normal",grp), 48, 32)
	#ymax = ifelse(grepl("boot|normal",grp), 0.16, 0.14)
	ymax = 0.14
	ymax = ymax*((1-conf.level)/0.05)
	print(ymax)
	xstep=4
	step=0.02*((1-conf.level)/0.05)
	nintervals = length(groups[[grp]])
	#width=10;height=8;
	if (nintervals == 1) {nrow=1;ncol=1;
	} else if (nintervals == 2) {nrow=1;ncol=2
	} else if (nintervals <= 4) {nrow=2;ncol=2
	} else if (nintervals == 6) {nrow=3;ncol=2
	} else if (nintervals == 9) {nrow=3;ncol=3
	} else {
		ncol=4;
		width=10;height=2*ceiling(nintervals/ncol)
		nrow=ceiling(nintervals/ncol);
	}

	if (draw_local_alpha & !is.null(df0)) {
		if (infile) {dirfile(paste0("plotAlpha/",grp,suffix),width=width,height=height)}
		cat("Going to plot average Alpha\n")
		print(plotLR2(df0, groups[[grp]], start=0, end=end,ylim=c(0,ymax),xstep=xstep,step=step,nrow=nrow,ncol=ncol, conf.levels=conf.level,...))
		if (infile) {dev.off()}
	}
	if (draw_cond_alpha & !is.null(df3C)) {
		cat("Going to plot conditional Alpha\n")
		if (infile) {dirfile(paste0("plotCondAlpha/",grp,suffix),width=width,height=height)}
		print(plotLR3(df3C, groups[[grp]],start=conditional_xstart,end=conditional_xend,ylim=c(0,ymax),step=step,xstep=end2xstep(conditional_xend),nrow=nrow,ncol=ncol,bilat=T,conf.levels=conf.level, ...))
		if (infile) {dev.off()}
	}
	if (draw_local_width & !is.null(df0)) {
		if (infile) {dirfile(paste0("plotWidth/",grp,suffix),width=width,height=height)}
		cat("Going to plot local average Width\n")
		if (is.null(df0$width2)) {
			stop("Problème avec width2 qui devrait être dans df0 mais n'y est pas !");
		}
		print(plotLR2(df0, groups[[grp]],start=0,end=end,ylim=c(0.60,1.60),step=0.1,xstep=xstep,nrow=nrow,ncol=ncol, yvar="width2", conf.levels=conf.level,...))
		if (infile) {dev.off()}
	}
	if (draw_cond_width & !is.null(df3C)) {
		if (infile) {dirfile(paste0("plotCondWidth/",grp,suffix),width=width,height=height)}
		cat("Going to plot conditional expected Width\n")
		if (is.null(df3C$width2)) {
			stop("Problème avec width2 qui devrait être dans df0 mais n'y est pas !");
		}
		print(plotLR2(df3C, groups[[grp]],start=0,end=end,ylim=c(0.60,1.60),step=0.1,xstep=xstep,nrow=nrow,ncol=ncol, yvar="width2", conf.levels=conf.level,...))
		if (infile) {dev.off()}
	}
}
}

