Package 'gg.sn'

Title: Graphical Methods for the 'sn' Package Based on the 'ggplot2' Package
Description: The plotting methods of package 'sn', built on base R graphics, are superseded by alternative methods based on the package 'ggplot2'.
Authors: Adelchi Azzalini [aut, cre] (ORCID: <https://orcid.org/0000-0002-7583-1269>)
Maintainer: Adelchi Azzalini <[email protected]>
License: GPL-2 | GPL-3
Version: 0.1.1
Built: 2026-12-02 20:59:02 UTC
Source: https://github.com/cran/gg.sn

Help Index


gg.sn

Description

The plotting methods of package sn, which are built on base R graphics, are superseded by alternative methods based on the package ggplot2.

Details

This package replaces the plotting methods of the package sn (which works with traditional base-type graphics) with alternative ones built on the package ggplot2. More precisely, the plotting methods of this package take precedence over those of sn, which remain in place. When the present package is launched, sn is loaded first, followed by gg.sn.

The plotting methods of package gg.sn and their arguments match those of sn, but the rendering is different, since the outcome is produced via the underlying package ggplot2. In some cases, arguments are there for compatibility with the original method in sn, but not actually used.

All the plotting methods of sn have their counterpart in gg.sn. Notice, however, that there is no counterpart of the sn function profile.selm, which actually can produce plots, but it does not constitute a plotting S4 method. Also note that sn::profile.selm is still declared to be at experimental stage and any matching function in gg.sn inherits this condition.

Author(s)

Adelchi Azzalini

Examples

# d=1
f1 <- makeSECdistr(dp=c(3,2,5), family="SN", name="First-SN")
plot(f1) # compare the outcome with the output produced by classical 'sn'
#
# d=2
Omega2 <- matrix(c(3, -3, -3, 6), 2, 2)    
dp <- list(c(10,30), Omega=Omega2, alpha=c(-3, 5))
f2 <-  makeSECdistr(dp=dp, family="sn", name="SN-2d", compNames=c("x1","x2"))
show(f2)     
plot(f2)
plot(f2, range=cbind(c(4,16), c(25, 40)), landmarks="")

Plot an object generated by fitdistr.grouped

Description

The object produced by fitdistr.grouped is plotted in the form of an histogram of the input original observed frequencies with superimposed the fitted parametric density function.

Usage

## S3 method for class 'fitdistr.grouped'
plot(x, freq = FALSE, col = "grey70", 
  border = "grey60",  pdfcol = "blue", main, sub = NULL, xlab, ylab, xlim, ylim, 
  axes = TRUE, labels = FALSE, ...)

Arguments

x

a link{fitdistr.grouped} object.

freq

logical; if TRUE, the histogram graphic is to present a representation of frequencies; if FALSE (default value), densities are plotted.

col

a colour to be used to fill the bars.

border

the colour of the border around the bars.

pdfcol

the colour of the fitted parametric distribution.

main, sub, xlab, ylab

these arguments to title an be omitted, in which case default values are constructed.

xlim, ylim

the range of x and y values with sensible defaults.

axes

logical, indicating if axes should be drawn.

labels

logical or character. Additionally draw labels on top of bars, if not FALSE; if TRUE, draw the counts or rounded densities; if labels is a character, draw itself.

...

further graphical parameters to title and axis.

Details

The plotting methods of package sn.gg and their arguments match those of package sn, but the rendering is different, since it is produced via the underlying package ggplot2.

The function builds on plot.histogram, but some lesser features have been dropped.

Value

A list with the following components

hist

an object of class histogram which produces the histogram.

x, y

the values used for plotting the fitted parametric distribution.

Author(s)

Adelchi Azzalini

See Also

fitdistr.grouped for generating a suitable object, plot.histogram for the related more general function.

Examples

data(barolo)
attach(barolo)
A75 <- (reseller=="A" & volume==75)
logPrice <- log(price[A75],10) # as used in selm documentation; see its fitting
detach(barolo)
breaks<- seq(1, 3, by=0.25)
f <- cut(logPrice, breaks = breaks)
freq <- tabulate(f, length(levels(f))) 
logPrice.grouped <- fitdistr.grouped(breaks, freq, family='ST')
plot(logPrice.grouped)
plot(logPrice.grouped, col="grey70", border="lightblue", pdfcol="maroon",
     main="grouped data log(price)", sub="with fitted ST", xlab="log(price)")

Plotting methods for classes SECdistrUv and SECdistrMv

Description

Plotting methods for classes SECdistrUv and SECdistrMv using ggplot2

Usage

## S4 method for signature 'SECdistrUv'
plot(x, range, probs, main, npt = 251, data=NULL, 
  plot.it=TRUE, ...)

## S4 method for signature 'SECdistrMv'
plot(x, range, probs, npt, landmarks = "auto", main, 
  comp, compLabs, data = NULL, data.par = NULL, gap = 0.5, plot.it=TRUE, ...)

Arguments

x

an object of class SECdistrUv or SECdistrMv.

range

in the univariate case, a vector of length 2 which defines the plotting range; in the multivariate case, a matrix with two rows where each column defines the plotting range of the corresponding component variable. If missing, a sensible choice is made.

probs

a vector of probability values. In the univariate case, the corresponding quantiles are plotted on the horizontal axis; it can be skipped by setting probs=NULL. In the multivariate case, each probability value corresponds to a contour level in each bivariate plot; at least one probability value is required. Default value: c(0.05, 0.25, 0.5, 0.75, 0.95) in the univariate case, c(0.25, 0.5, 0.75, 0.95) in the multivariate case. See ‘Details’ for additional information.

npt

a numeric value or vector (in the univariate and in the multivariate case, respectively) to assign the number of evaluation points of the distribution, on an equally-spaced grid over the range defined above. Default value: 251 in the univariate case, a vector of 101's in the multivariate case.

landmarks

a character string which affects the placement of some landmark values in the multivariate case, that is, the origin, the mode and the mean (or its substitute pseudo-mean), which are all aligned. Possible values: "proper", "pseudo", "auto" (default), "". The option "" prevents plotting of the landmarks. With the other options, the landmarks are plotted, with some variation in the last one: "proper" plots the proper mean value, "pseudo" plots the pseudo-mean, useful when the proper mean does not exists, "auto" plots the proper mean if it exists, otherwise it switches automatically to the pseudo-mean. See dp2cp for more information on pseudo-CP parameters, including pseudo-mean.

main

a character string for main title; if missing, one is built from the available ingredients.

comp

a subset of the vector 1:d, if d denotes the dimensionality of the multivariate distribution.

compLabs

a vector of character strings or expressions used to denote the variables in the plot; if missing, slot(object,"compNames") is used.

data

an optional set of data of matching dimensionity of object to be superimposed to the plot. The default value data=NULL produces no effect. In the univariate case, data are plotted using rug at the top horizontal axis, unless if probs=NULL, in which case plotting is at the bottom axis. In the multivariate case, points are plotted in the form of a scatterplot or matrix of scatterplots; this can be regulated by argument data.par.

data.par

an optional list of graphical parameters used for plotting data in the multivariate case, when data is not NULL. Recognized parameters are: col, pch, cex. If missing, the analogous components of par() are used.

gap

unused; only present for compatibility with the sn method.

plot.it

logical value (default: TRUE); should the plot actually be displayed? if not, it is only included in the returned object.

...

additional graphical parameters

Value

An invisible list. In the univariate case, the list has four components: the input object representing the distribution, two numeric vectors with the coordinates of the plotted density values and the ggplot2 plot object, named gg.obj. In the multivariate case, the first element of the list is the input object representing the distribution and all subsequent list elements are lists with components of the panels comprising the matrix plot; the elements of these sub-lists are: the vectors of x and y coordinates, the names of the variables, the density values at the (x,y) points, a vector of the density levels of the curves appearing in each panel plot, with the corresponding approximate probability content as a vector attribute, and the ggplot2 plot object

Details

For univariate density plots where range has not been specified, probs are used to compute quantiles from the appropriate distribution, and these are superimposed to the plot of the density function, unless probs=NULL. In the multivariate case, each bivariate plot is constructed as a collection of contour curves, one curve for each probability level; consequently, probs cannot be NULL and must only include values strictly within the (0, 1) interval. The level of the density contour lines are chosen so that each curve circumscribes a region with the quoted probability, to a good degree of approssimation; for additional information, see Azzalini and Capitanio (2014), specifically Complement 5.2, p. 179, and references therein.

If range has not been specified in the calling statement, a suitable plottiung range is computed from a set of reasonable quantiles for each univariate marginal distribution. Next, if data is not NULL, that plotting range may be expanded to accommodate all the data points.

Methods

signature(x = "SECdistrUv")

Plot an object x of class SECdistrUv.

signature(x = "SECdistrMv")

Plot an object x of class SECdistrMv.

Author(s)

Adelchi Azzalini

References

Azzalini, A. with the collaboration of Capitanio, A. (2014). The Skew-Normal and Related Families. Cambridge University Press, IMS Monographs series.

See Also

makeSECdistr, summary.SECdistr, dp2cp in package sn

Examples

# d=1
f1 <- makeSECdistr(dp=c(3,2,5), family="SC", name="Univariate Skew-Cauchy")
plot(f1)
plot(f1, range=c(-5, 30), probs=NULL, col=4, main="SC(3,2,5) density function")
plot(f1, col=2, lty=2, probs=c(0.25, 0.75), )
#    
# d=2
Omega2 <- matrix(c(3, -3, -3, 5), 2, 2)    
f2 <-  makeSECdistr(dp=list(c(10,30), Omega=Omega2, alpha=c(-3, 5)),  
                family="sn", name="SN-2d", compNames=c("x1","x2"))
plot(f2)    
x2 <- rmsn(100, dp=slot(f2,"dp"))
plot(f2, main="Distribution 'f2'", probs=c(0.5,0.9), cex.main=1.5, col=2,
  cex=0.8, compLabs=c(expression(x[1]), expression(log(z[2]-beta^{1/3}))), 
  data=x2, data.par=list(col=4, cex=0.6, pch=5))
# 
# d=3
Omega3 <- toeplitz(1/1:3)
f3 <-  makeSECdistr(dp=list(c(-9, 0, 8), Omega=Omega3, alpha=c(2, -3, 5), nu=4),
           family="ST", name="ST-3d", compNames=c("x1","x2", "z"))
plot(f3, landmarks="")

Diagnostic plots for selm fits

Description

Diagnostic plots for objects of class selm and mselm generated by a call to function selm

Usage

## S4 method for signature 'selm'
plot(x, param.type="CP", which = c(1:4), caption, 
    panel = NULL, main = "", 
    ask = prod(par("mfcol")) < length(which) && dev.interactive(), ..., 
    id.n = 3, labels.id = names(x@residuals.dp), 
    cex.id = 0.75, identline = TRUE, add.smooth = getOption("add.smooth"), 
    label.pos = c(4, 2), cex.caption = 1) 

 ## S4 method for signature 'mselm'
plot(x, param.type="CP",  which, caption, 
    panel = NULL,  main = "", 
    ask = prod(par("mfcol")) < length(which) && dev.interactive(), ..., 
    id.n = 3, labels.id = names(x@residuals.dp), 
    cex.id = 0.75, identline = TRUE, add.smooth = getOption("add.smooth"), 
    label.pos = c(4, 2), cex.caption = 1)

Arguments

x

an object of class selm or mselm.

param.type

a character string which selects the type of residuals to be used for some of of the plots; possible values are: "CP" (default), "DP", "pseudo-CP". The various type of residuals only differ by an additive term; see ‘Details’ for more information.

which

if a subset of the plots is required, specify a subset of 1:4; see ‘Details’ for a description of the plots.

caption

a vector of character strings with captions to appear above the plots.

panel

not active, only present for compatibility with package sn.

main

title to each plot, in addition to the above caption.

ask

logical; if TRUE and more than one plot is being produced, the user is asked for input, before a new figure is drawn

...

other parameters to be passed through to plotting functions.

id.n

number of points to be labelled in each plot, starting with the most extreme.

labels.id

vector of labels, from which the labels for extreme points will be chosen. NULL uses observation numbers..

cex.id

magnification of point labels.

identline

logical indicating if an identity line should be added to QQ-plot and PP-plot (default: TRUE).

add.smooth

logical indicating if a smoother should be added to most plots; see also panel above.

label.pos

positioning of labels, for the left half and right half of the graph respectively, for plots 1-3.

cex.caption

controls the size of caption.

Details

The plotting methods of package sn.gg and their arguments match those of package sn, but the rendering is different, since it is produced via the underlying package ggplot2.

The meaning of param.type is described in dp2cp. However, for these plot only the first parameter component is relevant, which affects the location of the residuals; the other components are not computed. Moreover, for QQ-plot and PP-plot, DP-residuals are used irrespectively of param.type; see Section ‘Background’.

Values which=1 and which=2 have a different effect for object of class "selm" and class "mselm". In the univariate case, which=1 plots the residual values versus the fitted values if p>1, where p denotes the number of covariates including the constant; if p=1, a boxplot of the response is produced. Value which=2 produces an histogram of the residuals with superimposed the fitted curve, when p>1; if p=1, a similar plot is generated using the response variable instead of the residuals. Default value for which is 1:4.

In the multivariate case, which=1 is feasible only if p=1 and it displays the data scatter with superimposed the fitted distribution. Value which=2 produces a similar plot but for residuals instead of data. Default value for which is 2:4 if p>1, otherwise c(1,3,4).

Value which=3 produces a QQ-plot, both in the univariate and in the multivariate case; the difference is that the squares of normalized residuals and suitably defined Mahalanobis distances, respectively, are used in the two cases. Similarly, which=4 produces a PP-plot, working in a similar fashion.

Background

Healy-type graphical diagnostics, in the form of QQ- and PP-plots, for the multivariate normal distribution have been extended to the skew-normal distribution by Azzalini and Capitanio (1999, section 6.1), and subsequently to the skew-tt distribution in Azzalini and Capitanio (2003). A discussion oh the univariate SN case is provided in the monograph by Azzalini and Capitanio (2014) at Section 3.1.1; see also Section 3.1.6. For the univariate ST case, see p.102 and p.111 of the monograph. The multivariate case is discussed in Section 5.2.1 as for the SN distribution, in Section 6.2.6 as for the ST distribution.

Author(s)

Adelchi Azzalini

References

Azzalini, A. and Capitanio, A. (1999). Statistical applications of the multivariate skew normal distribution. J.Roy.Statist.Soc. B 61, 579-602. Full-length version available at https://arXiv.org/abs/0911.2093

Azzalini, A. and Capitanio, A. (2003). Distributions generated by perturbation of symmetry with emphasis on a multivariate skew t distribution. J.Roy. Statist. Soc. B 65, 367-389. Full-length version available at https://arXiv.org/abs/0911.2342

Azzalini, A. with the collaboration of Capitanio, A. (2014). The Skew-Normal and Related Families. Cambridge University Press, IMS Monographs series.

See Also

selm, dp2cp

Examples

data(wines)
#
m10 <- selm(phenols ~ 1, family="SN", data=wines, subset=(wine=="Barolo"))
plot(m10)

oldpar <- par(mfcol=c(2,2))  
plot(m10, which=1:4, col="blue", cex=0.5, cex.id=0.6) 
par(mfcol=c(1,1)) 
par(oldpar)
#
m18 <- selm(acidity ~ sugar + nonflavanoids + wine, family="SN", data=wines)
plot(m18)
plot(m18, which=3, param.type="DP", cex=1.2, id.n=1, cex.id=1, col=2)

m28 <- selm(cbind(acidity, alcohol) ~ sugar + nonflavanoids + wine, 
            family="SN", data=wines)
plot(m28, pch=2)            
# 
data(ais)
m30 <-  selm(cbind(RCC, Hg, Fe) ~ 1, family="SN", data=ais)
plot(m30, col=2, which=2)

Plotting method for class SUNdistr

Description

Plotting method for class SUNdistr

Usage

## S4 method for signature 'SUNdistr'
plot(x, range, nlevels = 8, levels, npt,  main, comp, compLabs, gap = 0.5, ...)

Arguments

x

an object of class SUNdistr

range

in the univariate case, a vector of length 2 which defines the plotting range; in the multivariate case, a matrix with two rows where each column defines the plotting range of the corresponding component variable. If missing, a sensible choice is made.

nlevels

number of contour levels desired iff levels is not supplied.

levels

numeric vector of levels at which to draw contour lines.

npt

a numeric value or vector (in the univariate and in the multivariate case, respectively) to assign the number of evaluation points of the distribution, on an equally-spaced grid over the range defined above. Default value: 251 in the univariate case, a vector of 101's in the multivariate case.

main

a character string for main title; if missing, one is built from the available ingredients.

comp

an optional integer vector representing the subset of the vector 1:d, if d denotes the dimensionality of the distribution.

compLabs

a vector of character strings or expressions used to label the variables in the plot; if missing, slot(object,"compNames")[comp] is used.

gap

unused; only present for compatibility with the sn method.

...

additional graphical parameters

Details

For univariate density plots, probs are used to compute quantiles from the appropriate distribution, and these are superimposed to the plot of the density function, unless probs=NULL. In the multivariate case, each bivariate plot is constructed as a collection of contour curves, one curve for each probability level; consequently, probs cannot be missing or NULL. The level of the density contour lines are chosen so that each curve circumscribes a region with the quoted probability, to a good degree of approssimation; for additional information, see Azzalini and Capitanio (2014), specifically Complement 5.2 and p.179, and references therein.

Value

an invisible list. In the univariate case, the list has three components: the input object representing the distribution and two numeric vectors with the coordinates of the plotted density values. In the multivariate case, the list comprises: the input SUN-class object, the plotting range, the ggplot2 object encapsulating the plot.

Author(s)

Adelchi Azzalini

See Also

makeSUNdistr, SUNdistr-class

Examples

xi <- c(1, 0, -1)
Omega <- matrix(c(2,1,1, 1,3,1, 1,1,4), 3, 3)
Delta <- matrix(c(0.72,0.20, 0.51,0.42, 0.88, 0.94), 3, 2, byrow=TRUE)
Gamma <- matrix(c(1, 0.8, 0.8, 1), 2, 2)
dp3 <- list(xi=xi, Omega=Omega, Delta=Delta, tau=c(-0.5, 0), Gamma=Gamma)
sun3 <- makeSUNdistr(dp=dp3, name="SUN3", compNames=c("x", "w", "z"))
plot(sun3, npt=rep(51,3))
p1 <- plot(sun3, comp=1, range=c(-2, 8), col=2, lwd=2/3, lty=3, 
           main="First marginal component of 'sun3'")
p2 <- plot(sun3, comp=2:3, compLabs=c(expression(x[2]), expression(x[3])))

Plotting symmetry-modulated distributions

Description

Plotting bivariate symmetry-modulated distributions, AKA skew-symmetric distributions

Usage

plot2D.SymmModulated(range, npt=rep(101,2), xi=c(0,0), Omega, f0, G0, w, 
  par.f0, par.G0, odd="check", ...)

Arguments

range

a two-column matrix whose column-wise range is taken as the plotting intervals on the coordinated axes forming a bivariate grid of points over which the density is plotted.

npt

a numeric vector with two elements representing the number of equally-spaced points on each axis spanning the range described above; default value is rep(101,2).

xi

a numeric vector representing the location parameter; if must have length 1 for dSymmModulated and rSymmModulated, length 2 for plot2D.SymmModulated).

f0

a character string denoting the symmetric density to be modulated; admissible values for dSymmModulated and dSymmModulated are "beta", "cauchy", "logistic" (or "logis"), "normal" (or "norm"), "t", "uniform"; for the other functions the possible values are "cauchy", "normal" (or "norm"), "t"; the meaning of the names is described in the ‘Details’ section.

G0

a character string denoting the symmetric distribution used in the modulating factor; admissible values are "beta", "cauchy", "logistic" (or "logis"), "normal" (or "norm"), "t", "uniform", with meaning described in the ‘Details’ section.

w

the name (not as a character string) of a user-defined function which satisfies the condition w(−z)=−w(z)w(-z)=-w(z) for all zz; see the ‘Details’ section for additional specifications.

par.f0, par.G0

parameters required by f0 and G0, when they are of type "beta" or "t", otherwise ignored.

odd

a character string, with possible values "check" (default), "assume", "force", for regulation of the behaviour about the condition that w is an odd function, as explained in the ‘Details’ section.

Omega

a symmetric positive-definite matrix which regulates the dependence structure of f0 and so of the final density.

...

optional parameters regulating the function w and, for plot2D.SymmModulated only, graphical parameters to be supplied to function geom_contour.

Value

An invisible list containing the x and y coordinates, the corresponding density values, and the ggplot2 plot object, gg.obj.

Background

Background information for plot2D.SymmModulated, as well as related functions related to user-defined ‘symmetry-modulated’ distributions, are provided in symm-modulated-distr.

Details

For the bivariate case only, plot2D.SymmModulated computes a density over a grid of coordinates and produces a contour plot. The plotting methods and their arguments match those of package sn, but the rendering is different, since it is produced using package ggplot2.

The distribution names used in f0 and G0 have, in the univariate case, the same meaning as described in the Distributions page, with the following exceptions, to achive symmetry about 0: "uniform" denotes a uniform distribution over the interval (−1,1)(-1, 1); "beta" denotes the a symmetric Beta distribution with support over the interval (−1,1)(-1, 1) and a common value of the shape parameters.

In the multivariate case, the available options "normal" and "t" for f0 refer to densities computed by dmnorm and dmt with 0 location and correlation matrix Ωˉ\bar\Omega, implied by Ω\Omega. Argument G0 has the same meaning as in the univariate case.

Options "beta" and "t" for f0 and G0 require the specification of a shape parameter, via the arguments par.f0 and par.G0, respectively. For "beta" the parameter represents the common value of the shape parameters of Beta; for "t", it represents df of TDist and dmt.

Function w most be of the form w <- function(z, ...) where ... are optional additional parameters and z represents valued of the standardized form of the density; in the univariate case, x and z are related by z=(x-xi)/omega and an analogous fact holds in the multivariate setting. The function must satisfy the condition w(−z)=−w(z)w(-z)=-w(z). It is assumed that the function is vectorized and, in the multivariate case, it will be called with z representing a matrix with d columns, if d denotes the dimensionality of the random variable.

Argument odd regulates the behaviour with respect to the condition w(−z)=−w(z)w(-z)=-w(z). If its value is "assume", the condition is just assumed to hold, and no action is taken. If the value is "check" (deafult), a limited check is performed; namely, in case of densities, the check is at 0 and the supplied x points, while for random numbers the check is at 0 and the generated points. The value "force" ensures that the condition is satisfied by actually constructing a modified version of the user-supplied function w, such that the required condition is enforced.

Author(s)

Adelchi Azzalini

References

Arellano-Valle, R. B., Gómez, H. W. and Quintana, F. A. (2004). A new class of skew-normal distributions. Comm. Stat., Theory & Methods, 58, 111-121.

Azzalini, A. and Capitanio, A. (2003). Distributions generated by perturbation of symmetry with emphasis on a multivariate skew-t distribution. J.Roy. Statist. Soc. B 65, 367–389. Full version of the paper at https://arXiv.org/abs/0911.2342

Azzalini, A. with the collaboration of Capitanio, A. (2014). The Skew-Normal and Related Families. Cambridge University Press, IMS Monographs series.

Wang, J., Boyer, J. and Genton, M. G. (2004). A skew-symmetric representation of multivariate distributions. Statistica Sinica, 14, 1259-1270.

See Also

symm-modulated-distr, Distributions, Beta, TDist, dmnorm, dmt, geom_contour

Examples

# example 1D - it actually refers to a univariate case, which is the
# univariate analogue of the bivariate distribution on pp.372-3 of
# Azzalini & Capitanio (2003)    
wTrigs <- function(z, p, q) sin(z * p)/(1 + cos(z * q))
x <- seq(-1, 1, length=101)
y <- dSymmModulated(x, 0, 1, f0="beta", G0="logistic", w=wTrigs, par.f0=2,
        par.G0=NULL, p=5, q=0.5)   
ggplot(data.frame(x, y), aes(x, y)) + geom_line(linetype=1, colour=4)
#
# example 2D - this really illustrates plot2D.SymmModulated
range <- cbind(c(-3,3), c(-3,3))
wMvTrigs <- function(z, p, q) sin(z %*% p)/(1 + cos(z %*% q))
plot2D.SymmModulated(range, xi=c(0,0), Omega=diag(2), f0="normal", G0="normal",
    w=wMvTrigs, par.f0=NULL, par.G0=NULL, p=c(2,3), q=c(1,1), col=4)
# w(.) as in (1.6) of Azzalini & Capitanio (2014, p.4) and the plot is as in the
# bottom-right panel of their Figure 1.1.