R/plotDistDiag.R

'plotDistDiag' <- function(family = NULL,
						   labels = NULL,
						   scale = 1,
						   color = "skyblue",
						   name = TRUE)
{
	if (is.null(family)) {
		stop("family cannot be NULL")
	}
	x = .LazyEnv$distributions[[family]]$x
	y = do.call(.LazyEnv$distributions[[family]]$ddist, c(list(x=x), .LazyEnv$distributions[[family]]$ddist_params))
	opar = par(mar=c(0.3, 0, 0, 0), xaxt='n', yaxt='n', ann=FALSE, bty="n", xpd=NA)
	plot(c(x[1:2], x), c(0, max(y)/(1-.LazyEnv$distributions[[family]]$top_space), y), type="l", col="transparent")
	points_to_NA = stats::filter(c(0, y, 0), filter=c(1, 1, 1)) == 0
	points_to_NA = points_to_NA[-c(1, length(points_to_NA))]
	y[points_to_NA] = NA
	if ("bar" %in% .LazyEnv$distributions[[family]]$plot_type) {
		lines(x, y, type="h", col=color, lwd=6, lend=1)
		if (name) {
			legend(grconvertX(.LazyEnv$distributions[[family]]$name_pos[1], from="npc"), grconvertY(.LazyEnv$distributions[[family]]$name_pos[2], from="npc"),
				   .LazyEnv$distributions[[family]]$name, cex=1.5*scale, xjust=0.5, yjust=0.5, bty="o", box.lwd = 0, box.col="transparent",
				   bg=rgb(1,1, 1,0.5), x.intersp=-1, y.intersp=0 , text.col="transparent")
		}
	}
	if ("line" %in% .LazyEnv$distributions[[family]]$plot_type) {
		lines(x, y, type="l", col=color, lwd=3*scale)
	}
	lines(grconvertX(c(0.037, (1-0.037)), from="npc"), grconvertY(c(0, 0), from="npc"), lwd=2*scale)
	if (name) {
		text(grconvertX(.LazyEnv$distributions[[family]]$name_pos[1], from="npc"),
			 grconvertY(.LazyEnv$distributions[[family]]$name_pos[2], from="npc"),
			 .LazyEnv$distributions[[family]]$name, cex=1.5*scale)
	}
	if (is.character(names(labels))) {
		for (label_name in names(labels)) {
			xpos = .LazyEnv$distributions[[family]]$labels[[label_name]][1]
			ypos = .LazyEnv$distributions[[family]]$labels[[label_name]][2]
			label = labels[label_name]
			text(grconvertX(xpos, from="npc"), grconvertY(ypos, from="npc"), label, cex=2 * scale)
		}
	} else {
		for (i in seq_along(labels)) {
			xpos = .LazyEnv$distributions[[family]]$labels[[i]][1]
			ypos = .LazyEnv$distributions[[family]]$labels[[i]][2]
			label = labels[i]
			text(grconvertX(xpos, from="npc"), grconvertY(ypos, from="npc"), label, cex=2)
		}
	}
	
	return(invisible(par(opar)))
}
ndbrown6/lazypuppy documentation built on Sept. 14, 2020, 7:33 a.m.