Nothing
#----------------------------------------------------------------------------
# plotting SUN densities
#
plot.SUNdistr <- function(x, range, nlevels=8, levels, npt, main, comp,
compLabs, gap = 0.5, ...)
{# plot density of object of class "SUNdistr"
obj <- x
if(slot(obj, "class") != "SUNdistr") stop("object of wrong class")
dp <- slot(obj, "dp")
d <- length(dp$xi)
if(missing(comp)) comp <- seq(1, d)
if(!all(comp %in% seq(1,d))) stop("illegal 'comp' value(s)")
pd <- length(comp) # actual plotting dimension
if(missing(npt)) npt <- if(pd==1) 251 else rep(101, pd)
pobj <- if(pd == d) obj else marginalSUNdistr(obj, comp=comp, drop=FALSE)
name.pobj <- slot(obj, "name")
if(pd < d) name.pobj <- paste(name.pobj,"[", paste(comp, collapse=","), "]", sep="")
if(missing(main)) { main <- if(pd == 1 | pd == 2)
paste("Density function of", name.pobj) else
paste("Bivariate densities of", name.pobj)
}
compNames <- slot(pobj, "compNames")
if(missing(compLabs)) compLabs <- compNames
if(length(compLabs) != pd) stop("wrong length of 'compLabs' vector")
if(missing(range)) {
range <- matrix(NA, 2, pd)
dp.pobj <- slot(pobj, "dp")
m <- sunMean(dp=dp.pobj)
v <- sunVcov(dp=dp.pobj)
s <- sqrt(diag(v))
range <- rbind(m -3*s, m + 3*s)
if(d ==1) range <- range(pretty(range))
else for(j in 1:pd) range[,j] <- range(pretty(range[,j]))
}
dots <- list(...)
nmdots <- names(dots)
col <- if("col" %in% nmdots) dots$col else "blue"
lty <- if("lty" %in% nmdots) dots$lty else par("lty")
lwd <- if("lwd" %in% nmdots) dots$lwd else par("lwd")*0.5
cex <- if("cex" %in% nmdots) dots$cex else par("cex")
# pch <- if("pch" %in% nmdots) dots$cex else par("pch")
if(pd == 1) out <- plot.SUNdistrUv(pobj, range, npt, main, ...)
if(pd == 2) {
gg <- plot.SUNdistrBv(pobj, range, nlevels, levels, npt, compLabs, main, ...)
out <- list(object=pobj, range=range, gg.obj=gg)
}
if(pd > 2) {
out <- list(object=pobj)
count <- 1
for (i in comp)
for (j in comp) {
count <- count + 1
if(i == j) {
span <- range[,i]
variable.df <- data.frame(x=span, y=span, label=compLabs[i])
v.name <- compLabs[i]
# if(!is.null(main)) v.name <- paste(main, v.name, sep ="\n")
gg.i <- (ggplot(data=variable.df, aes(x=x,y=x))
+ xlim(span) + ylim(span)
+ xlab(NULL) + ylab(NULL)
# + coord_cartesian(clip="off")
+ annotate("text", label=v.name, colour='blue', size=5,
x=mean(span), # xmin=min(span), xmax=max(span),
y=mean(span)) # ymin=min(span), ymax=max(span))
+ theme(panel.grid.minor = element_blank())
+ theme(panel.grid.major = element_blank())
# + theme_classic()
)
out[[count]] <- list()
names(out)[count] <- paste("diagonal component", compNames[i])
gg.all <- if(i==1) gg.i else {gg.all + gg.i}
} else {
ji <- c(j,i)
marg <- marginalSUNdistr(pobj, comp=ji, drop=FALSE)
plot.ji <- plot.SUNdistrBv(x=marg, range=range[,ji],
nlevels=nlevels, levels=levels, npt=npt[ji],
compNames= compNames[ji], compLabs=NULL,
main=NULL, plot.it=FALSE, ...)
out[[count]] <- plot.ji
names(out)[count] <- paste("plot of components (", j, ",", i, ")")
gg.all <- (gg.all + plot.ji$gg.obj
# + theme(axis.ticks = element_line(size = 0),
# axis.text=element_text(size=0))
)
}
}
gg.all <- (gg.all + plot_layout(ncol=pd, nrow=pd)
+ plot_annotation(title=main,
theme = theme(plot.title = element_text(size = cex)))
)
print(gg.all)
out$range <- range
out$gg.obj <- gg.all
}
invisible(out)
}
plot.SUNdistrBv <- function(x, range, nlevels=8, levels, npt, compLabs, main,
plot.it=TRUE, ...)
{# plot BiVariate SUN distribution (hence d=2)
y <- NULL ## To please R CMD check
## https://cran.r-project.org/web/packages/future/vignettes/future-4-issues.html
obj <- x
if(slot(obj, "class") != "SUNdistr") stop("object of wrong class")
dp <- slot(obj, "dp")
d <- length(dp[[1]])
if(d != 2) stop("wrong dimensions, d=2 is required")
if(missing(npt)) npt <- rep(51, d)
n1 <- npt[1]
n2 <- npt[2]
x1 <- seq(min(range[,1]), max(range[,1]), length=n1)
x2 <- seq(min(range[,2]), max(range[,2]), length=n2)
x1.x2 <- cbind(rep(x1, n2), as.vector(matrix(x2, n1, n2, byrow=TRUE)))
X <- matrix(x1.x2, n1 * n2, 2, byrow = FALSE)
pdf.col <- dsun(X, dp=dp)
pdf <- matrix(pdf.col, n1, n2)
pdf.df <- data.frame(x=X[,1], y=X[,2], pdf=pdf.col)
# options(warn=-1)
compNames <- slot(obj ,"compNames")
if(missing(levels)) levels <- pretty(range(pdf, finite=TRUE), nlevels)[-1]
if(missing(compLabs)) compLabs <- compNames
dots <- list(...)
nmdots <- names(dots)
col <- if("col" %in% nmdots) dots$col else "blue"
lty <- if("lty" %in% nmdots) dots$lty else 1
lwd <- if("lwd" %in% nmdots) dots$lwd else par("lwd")*0.5
gg <- (ggplot(pdf.df, aes(x = x, y = y, z = pdf))
+ geom_contour(bins=nlevels, breaks=levels, colour=col, linetype=lty,
linewidth=lwd)
+ xlab(compLabs[1]) + ylab(compLabs[2])
+ xlim(range[,1]) + ylim(range[,2])
# + theme(plot.margin=unit(rep(0,4), "pt"))
)
if(missing(main)) main <- paste("Density function of", slot(obj, "name"))
if(!is.null(main)) gg <- gg + labs(title=main)
if(plot.it) print(gg)
cL <- contourLines(x1, x2, pdf, levels=levels)
for(j in 1:length(cL)) cL[[j]]$level <- levels[j]
return(list(x=x1, y=x2, names=compNames, density=pdf, contourLines=cL, gg.obj=gg))
}
plot.SUNdistrUv <- function(x, range, npt=251, main, ...)
{# plot density of SUNdistr object when d=1
obj <- x
if(slot(obj, "class") != "SUNdistr") stop("object of wrong class")
dp <- slot(obj, "dp")
if(length(dp[[1]]) != 1) stop("SUN distribution of wrong dimension")
dots <- list(...)
nmdots <- names(dots)
col.annote <- "gray70"
topline <- if(obj@name == "") "" else
paste("Probability density of ", obj@name, "\n", sep="")
if(missing(main)) main <- paste(topline, "\nunivariate SUN distribution")
x <- seq(min(range), max(range), length=npt)
pdf <- as.vector(dsun(matrix(x, ncol=1), dp=dp))
xLab <- if("xlab" %in% nmdots) dots$xlab else slot(obj, "name")
yLab <- if("ylab" %in% nmdots) dots$ylab else "probability density"
yLim <- if("ylim" %in% nmdots) dots$ylim else c(0, max(pdf))
col <- if("col" %in% nmdots) dots$col else "blue"
lty <- if("lty" %in% nmdots) dots$lty else par("lty")
lwd <- if("lwd" %in% nmdots) dots$lwd else par("lwd")*0.5
pdf.df <- data.frame(x=x, pdf=pdf, zero=0)
gg <- (ggplot(data=pdf.df, aes(x, pdf)) + xlab(xLab) + ylab(yLab)
+ xlim(range[1], range[2])
+ ylim(yLim[1], yLim[2])
+ geom_line(colour=col, linetype=lty, linewidth=lwd)
+ geom_abline(intercept=0, slope=0, colour="gray80")
)
if (!is.null(main)) {
# font.m <- if("font.main" %in% nmdots) dots$font.main else par("font.main")
# cex.m <- if("cex.main" %in% nmdots) dots$cex.main else par("cex.main")
### title(main, line=2, cex.main=cex.m, font.main=font.m)
gg <- gg + labs(title=main)
}
print(gg)
invisible(list(object=obj, x=x, density=pdf, gg.obj=gg))
}
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.