Skip to content

Instantly share code, notes, and snippets.

@agstudy
Created March 14, 2013 08:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agstudy/24c74b0b8a4c38564c33 to your computer and use it in GitHub Desktop.
Save agstudy/24c74b0b8a4c38564c33 to your computer and use it in GitHub Desktop.
Hack function cdplot to change axis font size. The function take two new arguments: cex.x, cex.y
my.cdplot <- function (x, y, plot = TRUE, tol.ylab = 0.05, ylevels = NULL, cex.x=2,cex.y=3,
bw = "nrd0", n = 512, from = NULL, to = NULL, col = NULL,
border = 1, main = "", xlab = NULL, ylab = NULL, yaxlabels = NULL,
xlim = NULL, ylim = c(0, 1), ...)
{
if (is.null(xlab))
xlab <- deparse(substitute(x))
if (is.null(ylab))
ylab <- deparse(substitute(y))
if (is.null(col))
col <- gray.colors(length(levels(y)))
col <- rep(col, length.out = length(levels(y)))
if (is.null(yaxlabels))
yaxlabels <- levels(y)
xorig <- x
x <- as.numeric(x)
if (!is.factor(y))
stop("dependent variable should be a factor")
if (!is.null(ylevels))
y <- factor(y, levels = if (is.numeric(ylevels))
levels(y)[ylevels]
else ylevels)
dx <- if (is.null(from) & is.null(to))
stats::density(x, bw = bw, n = n, ...)
else stats::density(x, bw = bw, from = from, to = to, n = n,
...)
x1 <- dx$x
ny <- length(levels(y))
yprop <- cumsum(prop.table(table(y)))
y1 <- matrix(rep(0, n * (ny - 1L)), nrow = (ny - 1L))
rval <- list()
for (i in seq_len(ny - 1L)) {
dxi <- stats::density(x[y %in% levels(y)[seq_len(i)]],
bw = dx$bw, n = n, from = min(dx$x), to = max(dx$x),
...)
y1[i, ] <- dxi$y/dx$y * yprop[i]
rval[[i]] <- stats::approxfun(x1, y1[i, ], rule = 2)
}
names(rval) <- levels(y)[seq_len(ny - 1L)]
y1 <- rbind(0, y1, 1)
y1 <- y1[, which(x1 >= min(x) & x1 <= max(x))]
x1 <- x1[x1 >= min(x) & x1 <= max(x)]
if (is.null(xlim))
xlim <- range(x1)
if (any(ylim < 0) || any(ylim > 1)) {
warning("y axis is on a cumulative probability scale, 'ylim' must be in [0,1]")
if (min(ylim) > 1 || max(ylim) < 0)
ylim <- c(0, 1)
else ylim <- c(max(min(ylim), 0), min(max(ylim), 1))
}
if (plot) {
dev.hold()
on.exit(dev.flush())
plot(0, 0, xlim = xlim, ylim = ylim, type = "n", axes = FALSE,
xaxs = "i", yaxs = "i", xlab = xlab, ylab = ylab,
main = main)
for (i in seq_len(NROW(y1) - 1L)) polygon(c(x1, rev(x1)),
c(y1[i + 1, ], rev(y1[i, ])), col = col[i], border = border)
Axis(xorig, side = 1,cex.axis=cex.x)
equidist <- any(diff(y1[, 1L]) < tol.ylab)
if (equidist)
axis(2, at = seq.int(1/(2 * ny), 1 - 1/(2 * ny),
by = 1/ny), labels = yaxlabels, tick = FALSE,cex.axis=cex.y)
else axis(2, at = (y1[-1L, 1L] + y1[-NROW(y1), 1L])/2,
labels = yaxlabels, tick = FALSE,cex.axis=cex.y)
axis(4)
box()
}
invisible(rval)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment