Jinlei Zhu Jinlei Zhu - 12 days ago 7
R Question

How to increase the size of compact letter display in plot.cld() in multcomp package in R

Might be a silly question: How can I increase the size of letters at the top of the plot.cld()? For instance, how can I increase the size of display letters 'b','a','a' in the following plot? Hopfully I am not the only guy facing this annoyance. Thanks in advance!

library(multcomp)
data(warpbreaks)
amod <- aov(breaks ~ tension, data = warpbreaks)
tuk <- glht(amod, linfct = mcp(tension = "Tukey"))
tuk.cld <- cld(tuk)
old.par <- par(mai=c(1,1,1.25,1), no.readonly=TRUE)
plot(tuk.cld)
par(old.par)

Answer

From getAnywhere("plot.cld") I do not see how you could specifically alter the font size of the top labels. So... one way to solve this is adapting the existing function according to your needs:

library(multcomp)
data(warpbreaks)
amod <- aov(breaks ~ tension, data = warpbreaks)
tuk <- glht(amod, linfct = mcp(tension = "Tukey"))
tuk.cld <- cld(tuk)
par(mai=c(1,2,1.25,1), no.readonly=TRUE)
plot(tuk.cld, cex.top = 2)

with

plot.cld <- function (x, type = c("response", "lp"), cex.top = 1, ...) 
{
    mcletters <- x$mcletters
    msletters <- mcletters$monospacedLetters
    vletters <- sapply(msletters, function(x) paste(strsplit(x, 
        "")[[1]], "\n", collapse = ""))
    vletters <- vletters[gsub(" ", "", levels(x$x))]
    msletters <- msletters[gsub(" ", "", levels(x$x))]
    type <- match.arg(type)
    dat <- x[c("x", "y", "lp")]
    if (is.null(x$weights)) {
        dat$weights <- rep(1, NROW(x$y))
    }
    else {
        dat$weights <- x$weights
    }
    dat <- as.data.frame(dat)
    xn <- x$xname
    yn <- x$yname
    if (!is.null(list(...)$xlab)) 
        xn <- list(...)$xlab
    if (!is.null(list(...)$ylab)) 
        yn <- list(...)$ylab
    if (x$covar || type == "lp") {
        boxplot(lp ~ x, data = dat, xlab = xn, ylab = "linear predictor", 
            ...)
        axis(3, at = 1:nlevels(dat$x), labels = vletters)
    }
    else {
        if (is.integer(dat$y)) 
            dat$y <- as.numeric(dat$y)
        switch(class(dat$y), numeric = {
            boxplot(y ~ x, data = dat, xlab = xn, ylab = yn, 
                ...)
            axis(3, at = 1:nlevels(dat$x), labels = vletters, cex.axis = cex.top)
        }, factor = {
            at <- xtabs(weights ~ x, data = dat)/sum(dat$weights)
            at <- cumsum(at) - at/2
            mosaicplot(xtabs(weights ~ x + y, data = dat), main = NULL, 
                xlab = xn, ylab = yn, ...)
            axis(3, at = at, labels = vletters, tick = FALSE)
        }, Surv = {
            plot(survfit(y ~ x, data = dat), lty = 1:nlevels(dat$x), 
                ...)
            nc <- nchar(levels(dat$x))
            spaces <- unlist(lapply(max(nc) - nc, function(x) return(paste(rep(" ", 
                x), collapse = ""))))
            legend("topright", lty = 1:nlevels(dat$x), legend = paste(levels(dat$x), 
                spaces, ": ", msletters, sep = ""), ...)
        })
    }
}

giving

enter image description here