plots_graphics: Graphics-Based Plotting Functions

Description Usage Arguments Details Value Author(s) See Also Examples

Description

The 1d and 2d plotting functions based on the R package graphics.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
rug_1d_graphics(x, width=if(horizontal) 1 else 0.4,
                height=if(horizontal) 0.4 else 1, col=par("fg"),
                horizontal=TRUE, plotAsp=0.1, plotID, turn,
                add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)
points_1d_graphics(x, cex=0.4, horizontal=TRUE, plotAsp=0.1, plotID, turn,
                   add=FALSE, ann=FALSE, axes=FALSE,
                   xlim=c(0,1), ylim=c(0,1), ...)
jitter_1d_graphics(x, cex=0.4, offset=0.25, horizontal=TRUE, plotAsp=0.1, plotID, turn,
                   add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)
density_1d_graphics(x, density.args=list(), offset=0.25, method=c("single", "double"),
                    horizontal=TRUE, plotAsp=0.1, plotID, turn,
                    add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)
boxplot_1d_graphics(x, horizontal=TRUE, cex=0.4, plotAsp=0.1, range=NULL, plotID,
                    turn, add=FALSE, axes=FALSE, ...)
hist_1d_graphics(x, breaks=NULL, col=NULL, plotAsp=0.1,
                 horizontal=TRUE, plotID, turn,
                 add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1),
                 ylim=c(0,1), ...)
label_1d_graphics(loc.x=0.5, loc.y=0.5, labels=NULL,
                  srt=if(horizontal) 0 else 90, cex=0.75,
                  x, plotAsp=0.1, horizontal=TRUE, plotID, turn,
                  add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)
arrow_1d_graphics(loc=c(xlim[2], ylim[2])/2, length=0.6, angle=plotAsp*30,
                  x, horizontal=TRUE, plotAsp=0.1, plotID, turn,
                  add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)
rect_1d_graphics(loc.x=0.5, loc.y=0.5, horizontal=TRUE,
                 width=if(horizontal) 1.04 else 0.4,
                 height=if(horizontal) 0.4 else 1.04,
                 x, plotAsp=0.1, plotID, turn,
                 add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)
lines_1d_graphics(loc.x=if(horizontal) c(-0.04, 1.04) else c(0.5, 0.5),
                  loc.y=if(horizontal) c(0.5, 0.5) else c(-0.04, 1.04),
                  type="l", x, horizontal=TRUE, plotAsp=0.05, plotID, turn,
                  add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)

points_2d_graphics(x, type="p", cex=0.4, plotID, turn,
                   add=FALSE, ann=FALSE, axes=FALSE,
                   xlim=c(0,1), ylim=c(0,1), ...)
density_2d_graphics(x, ngrids=25, nlevels=10, drawlabels=FALSE,
                    plotID, turn,
                    add=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)
axes_2d_graphics(x, length=0.1, eps=0.04, code=2, xpd=NA,
                 plotID, turn, add=FALSE, ann=FALSE, axes=FALSE,
                 xlim=c(0,1), ylim=c(0,1), ...)
label_2d_graphics(loc.x=1, loc.y=0, labels=NULL,
                  adj=c(1,0), cex=0.75, x, plotID, turn,
                  add=FALSE, ann=FALSE, axes=FALSE,
                  xlim=c(0,1), ylim=c(0,1), ...)
arrow_2d_graphics(loc=c(xlim[2], ylim[2])/2, length=0.2, angle=30,
                  x, plotID, turn,
                  add=FALSE, ann=FALSE, axes=FALSE,
                  xlim=c(0,1), ylim=c(0,1), ...)
rect_2d_graphics(loc.x=0.5, loc.y=0.5, width=1.08, height=1.08,
                 x, plotID, turn,
                 add=FALSE, ann=FALSE, axes=FALSE, xlim=c(0,1), ylim=c(0,1), ...)

Arguments

x

An n-vector (for 1d plots) or (n,2)-matrix (for 2d plots) containing the data. It is passed from zenplot().

width

The width of the rugs.

height

The height of the rugs.

horizontal

A logical indicating whether the 1d plot is horizontal or vertical. It is passed from zenplot() (for 1d plots only).

plotAsp

The fraction (in [0,1]) of the shorter side divided by the longer side of the plot region. It is passed from zenplot() (for 1d plots only).

plotID

The plot identification passed on from zenplot(). This is a list containing the variable name(s), the column index(es) and the plotNo (among either all 1d or all 2d plots). It is passed from zenplot().

turn

A character (in "l" (for “left”), "r" (for “right”), "d" (for “down”), "u" (for “up”)) giving the turn as passed from zenplot().

add

A logical indicating whether the current plot should be added to (or on top of) the previous one.

ann

A logical indicating whether the plot should be annotated.

axes

A logical indicating whether axes should be drawn.

xlim,ylim

The x and y limits of the plotting region.

range

The argument range of the underlying boxplot() (determines how far the plot whiskers extend out of the box). If range=NULL, this will be automatically determined depending on the sample size.

breaks

The break points for the histogram as passed to the underlying hist(). If NULL, the default is to use 20 equi-width bins covering the range of the data.

col

A vector of colors for the bars or bar components; see barplot().

loc

The location of the center of the arrow.

loc.x,loc.y

x- and y-coordinates of the points combined by lines (for lines_1d_graphics()) or of the label (for label_1d_graphics(), label_2d_graphics()).

type

The plot type.

density.args

A list() of arguments for density().

offset

A number in [0,0.5] determining the distance between the 1d and 2d plots (for creating space between the two).

method

The type of density used.

ngrids

The number of grid points in each dimension (a scalar or an integer vector of length two).

nlevels

The number of contour levels to be drawn.

drawlabels

A logical indicating whether the contours should be labeled.

labels

The label to be used (with a useful default if NULL).

srt

The rotation of the label in degrees.

cex

The character expansion of the label or points.

length

The length of the arrow head.

angle

The angle between the shaft and one of the edges of the arrow head.

eps

Distance by which the axes are moved away from the plot region.

code

Integer code determining the kind of arrows to be drawn; see arrows.

xpd

A logical or NA, determining the region with respect to which clipping takes place; see par().

adj

x (and optionally y) adjustment of the label.

...

Additional (mostly but not exclusively graphical) parameters passed to the underlying graphics functions.

Details

These functions based on the R package graphics are provided as useful choices for the arguments plot1d and plot2d of zenplot(). See zenplot() how to use them, their source code for how to adjust them or how to write your own plot1d or plot2d. The main idea is that major arguments are passed to the underlying graphics functions as formal arguments and the ellipsis argument is used to pass all other (mostly graphical) paramaters. Hereby it is important to note that besides the ellipsis arguments (which are passed to both plot1d and plot2d), zenplot() passes further arguments to plot1d and plot2d. For plot1d, they are x, horizontal, plotAsp, plotID, turn (as described above). For plot2d, they are x, plotID, turn (as described above).

Value

invisible().

Author(s)

Marius Hofert and Wayne Oldford

See Also

zenplot() for how to use these functions. plots_grid for similar functions based on the R package grid.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17

Example output

Warning message:
no DISPLAY variable so Tk is not available 
function (zargs, loc = 0.5, length = 0.5, width = 1, col = par("fg"), 
    add = FALSE, plot... = NULL, ...) 
{
    r <- extract_1d(zargs)
    x <- as.matrix(r$x)
    horizontal <- r$horizontal
    lim <- r$xlim
    check_zargs(zargs, "num", "turns")
    turn.out <- zargs$turns[zargs$num]
    if (turn.out == "d" || turn.out == "l") 
        loc <- 1 - loc
    if (add) 
        usr <- par("usr")
    if (horizontal) {
        xlim <- lim
        ylim <- 0:1
        x0 <- x
        y0 <- loc - length/2
        x1 <- x
        y1 <- loc + length/2
        if (add) 
            opar <- par(usr = c(usr[1:2], 0, 1))
    }
    else {
        xlim <- 0:1
        ylim <- lim
        x0 <- loc - length/2
        y0 <- x
        x1 <- loc + length/2
        y1 <- x
        if (add) 
            opar <- par(usr = c(0, 1, usr[3:4]))
    }
    if (add) {
        on.exit(par(opar))
    }
    else {
        plot_region(xlim = xlim, ylim = ylim, plot... = plot...)
    }
    if (length(x) > 0) 
        segments(x0 = x0, y0 = y0, x1 = x1, y1 = y1, col = col, 
            lwd = width, ...)
}
<environment: namespace:zenplots>
function (zargs, loc = 0.5, cex = 0.4, add = FALSE, plot... = NULL, 
    ...) 
{
    r <- extract_1d(zargs)
    x <- as.matrix(r$x)
    horizontal <- r$horizontal
    lim <- r$xlim
    check_zargs(zargs, "num", "turns")
    turn.out <- zargs$turns[zargs$num]
    if (turn.out == "d" || turn.out == "l") 
        loc <- 1 - loc
    if (length(loc) == 1) 
        loc <- rep(loc, length(x))
    if (add) 
        usr <- par("usr")
    if (horizontal) {
        xlim <- lim
        ylim <- 0:1
        x <- x
        y <- loc
        if (add) 
            opar <- par(usr = c(usr[1:2], 0, 1))
    }
    else {
        ylim <- lim
        xlim <- 0:1
        y <- x
        x <- loc
        if (add) 
            opar <- par(usr = c(0, 1, usr[3:4]))
    }
    if (add) {
        on.exit(par(opar))
    }
    else {
        plot_region(xlim = xlim, ylim = ylim, plot... = plot...)
    }
    points(x = x, y = y, cex = cex, ...)
}
<environment: namespace:zenplots>
function (zargs, loc = 0.5, offset = 0.25, cex = 0.4, add = FALSE, 
    plot... = NULL, ...) 
{
    r <- extract_1d(zargs)
    x <- r$x
    stopifnot(0 <= offset, offset <= 0.5, 0 <= loc, loc <= 1, 
        offset <= min(loc, 1 - loc))
    loc. <- loc + runif(length(x), min = -offset, max = offset)
    points_1d_graphics(zargs, loc = loc., cex = cex, add = add, 
        plot... = plot..., ...)
}
<environment: namespace:zenplots>
function (zargs, density... = NULL, offset = 0.08, add = FALSE, 
    plot... = NULL, ...) 
{
    r <- extract_1d(zargs)
    x <- r$x[!is.na(r$x)]
    horizontal <- r$horizontal
    check_zargs(zargs, "num", "turns")
    turn.out <- zargs$turns[zargs$num]
    if (length(x) == 0) {
        if (!add) {
            opar <- par(usr = c(0, 1, 0, 1))
            on.exit(par(opar))
            plot_region(xlim = 0:1, ylim = 0:1, plot... = plot...)
        }
    }
    else {
        stopifnot(0 <= offset, offset <= 0.5)
        dens <- do.call(density, args = c(list(x), density...))
        xvals <- dens$x
        keepers <- (min(x) <= xvals) & (xvals <= max(x))
        x. <- xvals[keepers]
        y. <- dens$y[keepers]
        if (turn.out == "d" || turn.out == "l") 
            y. <- -y.
        if (horizontal) {
            xlim <- range(x.)
            ylim <- range(0, y.)
            x <- c(xlim[1], x., xlim[2])
            y <- c(0, y., 0)
            y <- (1 - 2 * offset) * y + offset * if (turn.out == 
                "d") 
                ylim[1]
            else ylim[2]
        }
        else {
            xlim <- range(0, y.)
            ylim <- range(x.)
            x <- c(0, y., 0)
            y <- c(ylim[1], x., ylim[2])
            x <- (1 - 2 * offset) * x + offset * if (turn.out == 
                "l") 
                xlim[1]
            else xlim[2]
        }
        if (add) {
            usr <- par("usr")
            opar <- par(usr = c(xlim, ylim))
            on.exit(par(opar))
        }
        else {
            plot_region(xlim = xlim, ylim = ylim, plot... = plot...)
        }
        polygon(x = x, y = y, ...)
    }
}
<environment: namespace:zenplots>
function (zargs, cex = 0.4, range = NULL, axes = FALSE, add = FALSE, 
    ...) 
{
    r <- extract_1d(zargs)
    x <- r$x
    horizontal <- r$horizontal
    if (is.null(range)) {
        n <- length(x)
        q25 <- qnorm(0.25)
        iqr <- qnorm(0.75) - q25
        range <- (q25 - qnorm(0.35/(2 * n)))/iqr
    }
    boxplot(x, horizontal = horizontal, range = range, cex = cex, 
        add = add, axes = axes, ...)
}
<environment: namespace:zenplots>
function (zargs, loc = c(0.5, 0.5), label = NULL, box = FALSE, 
    add = FALSE, plot... = NULL, ...) 
{
    r <- extract_1d(zargs)
    horizontal <- r$horizontal
    if (is.null(label)) 
        label <- names(r$x)
    check_zargs(zargs, "num", "turns")
    turn.out <- zargs$turns[zargs$num]
    if (turn.out == "d") 
        loc <- 1 - loc
    if (turn.out == "r") {
        loc <- rev(loc)
        loc[2] <- 1 - loc[2]
    }
    if (turn.out == "l") {
        loc <- rev(loc)
        loc[1] <- 1 - loc[1]
    }
    srt <- if (horizontal) {
        0
    }
    else {
        if (turn.out == "r") 
            -90
        else 90
    }
    opar <- par(usr = c(0, 1, 0, 1))
    on.exit(par(opar))
    if (!add) 
        plot_region(xlim = 0:1, ylim = 0:1, plot... = plot...)
    text(x = loc[1], y = loc[2], labels = label, srt = srt, ...)
    if (box) 
        box(...)
}
<environment: namespace:zenplots>
function (zargs, loc = c(0.5, 0.5), angle = 60, length = 0.6, 
    add = FALSE, plot... = NULL, ...) 
{
    check_zargs(zargs, "num", "turns", "width1d", "width2d")
    turn.out <- zargs$turns[zargs$num]
    horizontal <- turn.out %in% c("d", "u")
    if (turn.out == "d") 
        loc <- 1 - loc
    if (turn.out == "r") {
        loc <- rev(loc)
        loc[2] <- 1 - loc[2]
    }
    if (turn.out == "l") {
        loc <- rev(loc)
        loc[1] <- 1 - loc[1]
    }
    width1d <- zargs$width1d
    width2d <- zargs$width2d
    arrow <- zenarrow(turn.out, angle = angle, length = length, 
        coord.scale = width1d/width2d)
    arr <- loc + arrow
    opar <- par(usr = c(0, 1, 0, 1))
    on.exit(par(opar))
    if (!add) 
        plot_region(xlim = 0:1, ylim = 0:1, plot... = plot...)
    segments(x0 = rep(arr[1, 2], 2), y0 = rep(arr[2, 2], 2), 
        x1 = c(arr[1, 1], arr[1, 3]), y1 = c(arr[2, 1], arr[2, 
            3]), ...)
}
<environment: namespace:zenplots>
function (zargs, loc = c(0.5, 0.5), width = 1, height = 1, add = FALSE, 
    plot... = NULL, ...) 
{
    check_zargs(zargs, "num", "turns")
    turn.out <- zargs$turns[zargs$num]
    horizontal <- turn.out %in% c("d", "u")
    if (turn.out == "d") 
        loc <- 1 - loc
    if (turn.out == "r") {
        loc <- rev(loc)
        loc[2] <- 1 - loc[2]
    }
    if (turn.out == "l") {
        loc <- rev(loc)
        loc[1] <- 1 - loc[1]
    }
    if (!horizontal) {
        tmp <- width
        width <- height
        height <- tmp
    }
    x <- c(loc[1] - width/2, loc[1] + width/2)
    y <- c(loc[2] - height/2, loc[2] + height/2)
    opar <- par(usr = c(0, 1, 0, 1))
    on.exit(par(opar))
    if (!add) 
        plot_region(xlim = 0:1, ylim = 0:1, plot... = plot...)
    rect(xleft = x[1], ybottom = y[1], xright = x[2], ytop = y[2], 
        ...)
}
<environment: namespace:zenplots>
function (zargs, loc = c(0.5, 0.5), length = 1, add = FALSE, 
    plot... = NULL, ...) 
{
    check_zargs(zargs, "num", "turns")
    turn.out <- zargs$turns[zargs$num]
    horizontal <- turn.out %in% c("d", "u")
    if (turn.out == "d") 
        loc <- 1 - loc
    if (turn.out == "r") {
        loc <- rev(loc)
        loc[2] <- 1 - loc[2]
    }
    if (turn.out == "l") {
        loc <- rev(loc)
        loc[1] <- 1 - loc[1]
    }
    if (horizontal) {
        x <- c(loc[1] - length/2, loc[1] + length/2)
        y <- rep(loc[2], 2)
    }
    else {
        x <- rep(loc[1], 2)
        y <- c(loc[2] - length/2, loc[2] + length/2)
    }
    opar <- par(usr = c(0, 1, 0, 1))
    on.exit(par(opar))
    if (!add) 
        plot_region(xlim = 0:1, ylim = 0:1, plot... = plot...)
    lines(x, y = y, ...)
}
<environment: namespace:zenplots>
function (zargs, cex = 0.4, box = FALSE, add = FALSE, group... = NULL, 
    plot... = NULL, ...) 
{
    r <- extract_2d(zargs)
    xlim <- r$xlim
    ylim <- r$ylim
    x <- as.matrix(r$x)
    y <- as.matrix(r$y)
    same.group <- r$same.group
    if (same.group) {
        if (!add) 
            plot_region(xlim = xlim, ylim = ylim, plot... = plot...)
        points(x = x, y = y, cex = cex, ...)
        if (box) 
            box(...)
    }
    else {
        args <- c(list(zargs = zargs, add = add), group...)
        do.call(group_2d_graphics, args)
    }
}
<environment: namespace:zenplots>
function (zargs, ngrids = 25, drawlabels = FALSE, axes = FALSE, 
    box = FALSE, add = FALSE, group... = NULL, ...) 
{
    r <- extract_2d(zargs)
    xlim <- r$xlim
    ylim <- r$ylim
    x <- r$x
    y <- r$y
    same.group <- r$same.group
    if (same.group) {
        data <- na.omit(cbind(x, y))
        dens <- kde2d(data[, 1], data[, 2], n = ngrids, lims = c(xlim, 
            ylim))
        contour(dens$x, dens$y, dens$z, drawlabels = drawlabels, 
            axes = axes, add = add, ...)
        if (box) 
            box(...)
    }
    else {
        args <- c(list(zargs = zargs, add = add), group...)
        do.call(group_2d_graphics, args)
    }
}
<environment: namespace:zenplots>
function (zargs, loc = c(0.98, 0.05), label = NULL, adj = 1:0, 
    box = FALSE, add = FALSE, group... = NULL, plot... = NULL, 
    ...) 
{
    r <- extract_2d(zargs)
    same.group <- r$same.group
    vlabs <- r$vlabs
    check_zargs(zargs, "vars", "num")
    vars <- zargs$vars
    num <- zargs$num
    if (same.group) {
        xlab <- vlabs[vars[num, 1]]
        ylab <- vlabs[vars[num, 2]]
        if (is.null(label)) 
            label <- paste0("(", xlab, ", ", ylab, ")")
        opar <- par(usr = c(0, 1, 0, 1))
        on.exit(par(opar))
        if (!add) 
            plot_region(xlim = 0:1, ylim = 0:1, plot... = plot...)
        text(x = loc[1], y = loc[2], labels = label, adj = adj, 
            ...)
        if (box) 
            box(...)
    }
    else {
        args <- c(list(zargs = zargs, add = add), group...)
        do.call(group_2d_graphics, args)
    }
}
<environment: namespace:zenplots>
function (zargs, loc = c(0.5, 0.5), angle = 60, length = 0.2, 
    add = FALSE, group... = NULL, plot... = NULL, ...) 
{
    r <- extract_2d(zargs)
    same.group <- r$same.group
    check_zargs(zargs, "num", "turns")
    turn.out <- zargs$turns[zargs$num]
    if (same.group) {
        arrow <- zenarrow(turn.out, angle = angle, length = length, 
            coord.scale = 1)
        arr <- loc + arrow
        opar <- par(usr = c(0, 1, 0, 1))
        on.exit(par(opar))
        if (!add) 
            plot_region(xlim = 0:1, ylim = 0:1, plot... = plot...)
        segments(x0 = rep(arr[1, 2], 2), y0 = rep(arr[2, 2], 
            2), x1 = c(arr[1, 1], arr[1, 3]), y1 = c(arr[2, 1], 
            arr[2, 3]), ...)
    }
    else {
        args <- c(list(zargs = zargs, add = add), group...)
        do.call(group_2d_graphics, args)
    }
}
<environment: namespace:zenplots>
function (zargs, loc = c(0.5, 0.5), width = 1, height = 1, add = FALSE, 
    group... = NULL, plot... = NULL, ...) 
{
    r <- extract_2d(zargs)
    same.group <- r$same.group
    if (same.group) {
        x <- c(loc[1] - width/2, loc[1] + width/2)
        y <- c(loc[2] - height/2, loc[2] + height/2)
        opar <- par(usr = c(0, 1, 0, 1))
        on.exit(par(opar))
        if (!add) 
            plot_region(xlim = 0:1, ylim = 0:1, plot... = plot...)
        rect(xleft = x[1], ybottom = y[1], xright = x[2], ytop = y[2], 
            ...)
    }
    else {
        args <- c(list(zargs = zargs, add = add), group...)
        do.call(group_2d_graphics, args)
    }
}
<environment: namespace:zenplots>

zenplots documentation built on May 2, 2019, 4:34 p.m.