r - 冲积地 block 的边际

标签 r margin

我用这个例子

library(alluvial)
tit <- as.data.frame(Titanic)

# only two variables: class and survival status
tit2d <- aggregate( Freq ~ Class + Survived, data=tit, sum)

alluvial( tit2d[,1:2], freq=tit2d$Freq, xw=0.0, alpha=0.8,
          gap.width=0.1, col= "steelblue", border="white",
          layer = tit2d$Survived != "Yes" , cex.axis =8)

注意我用cex.axis =8我明白了
enter image description here

轴标签超越
我尝试使用 par(mar=c(10, 10, 10, 10))但没有结果
感谢您的任何想法

最佳答案

alluvial函数的源代码有一个bug

该函数设置 par(mar=c(2,1,1,1)) 硬编码,因此在外部使用 par() 没有任何效果。

您可以在本地将函数的源代码更改为以下两个选项之一:

  • 添加参数 mar_ 并传递边距,并在正确的位置设置 par(mar=mar_)
  • 只是在本地将行覆盖到所需的边距

  • 我发现第一个选项更有吸引力,因为您可以从函数外部设置值并更轻松地进行优化。

    源代码:
    function (..., freq, col = "gray", border = 0, layer, hide = FALSE, 
        alpha = 0.5, gap.width = 0.05, xw = 0.1, cw = 0.1, blocks = TRUE, 
        ordering = NULL, axis_labels = NULL, cex = par("cex"), cex.axis = par("cex.axis")) 
    {
        p <- data.frame(..., freq = freq, col, alpha, border, hide, 
            stringsAsFactors = FALSE)
        np <- ncol(p) - 5
        if (!is.null(ordering)) {
            stopifnot(is.list(ordering))
            if (length(ordering) != np) 
                stop("'ordering' argument should have ", np, " components, has ", 
                    length(ordering))
        }
        n <- nrow(p)
        if (missing(layer)) {
            layer <- 1:n
        }
        p$layer <- layer
        d <- p[, 1:np, drop = FALSE]
        p <- p[, -c(1:np), drop = FALSE]
        p$freq <- with(p, freq/sum(freq))
        col <- col2rgb(p$col, alpha = TRUE)
        if (!identical(alpha, FALSE)) {
            col["alpha", ] <- p$alpha * 256
        }
        p$col <- apply(col, 2, function(x) do.call(rgb, c(as.list(x), 
            maxColorValue = 256)))
        isch <- sapply(d, is.character)
        d[isch] <- lapply(d[isch], as.factor)
        if (length(blocks) == 1) {
            blocks <- if (!is.na(as.logical(blocks))) {
                rep(blocks, np)
            }
            else if (blocks == "bookends") {
                c(TRUE, rep(FALSE, np - 2), TRUE)
            }
        }
        if (is.null(axis_labels)) {
            axis_labels <- names(d)
        }
        else {
            if (length(axis_labels) != ncol(d)) 
                stop("`axis_labels` should have length ", names(d), 
                    ", has ", length(axis_labels))
        }
        getp <- function(i, d, f, w = gap.width) {
            a <- c(i, (1:ncol(d))[-i])
            if (is.null(ordering[[i]])) {
                o <- do.call(order, d[a])
            }
            else {
                d2 <- d
                d2[1] <- ordering[[i]]
                o <- do.call(order, d2[a])
            }
            x <- c(0, cumsum(f[o])) * (1 - w)
            x <- cbind(x[-length(x)], x[-1])
            gap <- cumsum(c(0L, diff(as.numeric(d[o, i])) != 0))
            mx <- max(gap)
            if (mx == 0) 
                mx <- 1
            gap <- gap/mx * w
            (x + gap)[order(o), ]
        }
        dd <- lapply(seq_along(d), getp, d = d, f = p$freq)
        rval <- list(endpoints = dd)
    ===============================================
    ===============Need to edit====================
        op <- par(mar = c(2, 1, 1, 1))
    ===============================================
        plot(NULL, type = "n", xlim = c(1 - cw, np + cw), ylim = c(0, 
            1), xaxt = "n", yaxt = "n", xaxs = "i", yaxs = "i", xlab = "", 
            ylab = "", frame = FALSE)
        ind <- which(!p$hide)[rev(order(p[!p$hide, ]$layer))]
        for (i in ind) {
            for (j in 1:(np - 1)) {
                xspline(c(j, j, j + xw, j + 1 - xw, j + 1, j + 1, 
                    j + 1 - xw, j + xw, j) + rep(c(cw, -cw, cw), 
                    c(3, 4, 2)), c(dd[[j]][i, c(1, 2, 2)], rev(dd[[j + 
                    1]][i, c(1, 1, 2, 2)]), dd[[j]][i, c(1, 1)]), 
                    shape = c(0, 0, 1, 1, 0, 0, 1, 1, 0, 0), open = FALSE, 
                    col = p$col[i], border = p$border[i])
            }
        }
        for (j in seq_along(dd)) {
            ax <- lapply(split(dd[[j]], d[, j]), range)
            if (blocks[j]) {
                for (k in seq_along(ax)) {
                    rect(j - cw, ax[[k]][1], j + cw, ax[[k]][2])
                }
            }
            else {
                for (i in ind) {
                    x <- j + c(-1, 1) * cw
                    y <- t(dd[[j]][c(i, i), ])
                    w <- xw * (x[2] - x[1])
                    xspline(x = c(x[1], x[1], x[1] + w, x[2] - w, 
                      x[2], x[2], x[2] - w, x[1] + w, x[1]), y = c(y[c(1, 
                      2, 2), 1], y[c(2, 2, 1, 1), 2], y[c(1, 1), 
                      1]), shape = c(0, 0, 1, 1, 0, 0, 1, 1, 0, 0), 
                      open = FALSE, col = p$col[i], border = p$border[i])
                }
            }
            for (k in seq_along(ax)) {
                text(j, mean(ax[[k]]), labels = names(ax)[k], cex = cex)
            }
        }
        axis(1, at = rep(c(-cw, cw), ncol(d)) + rep(seq_along(d), 
            each = 2), line = 0.5, col = "white", col.ticks = "black", 
            labels = FALSE)
        axis(1, at = seq_along(d), tick = FALSE, labels = axis_labels, 
            cex.axis = cex.axis)
        par(op)
        invisible(rval)
    }
    

    我将问题发生的位置标记为:

    ================================================

    ==============需要编辑======================

    op <- par(mar = c(2, 1, 1, 1))

    ================================================

    将行更改为 par(mar=c(5, 5, 3, 10)) 后,我得到:

    enter image description here

    关于r - 冲积地 block 的边际,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58068577/

    相关文章:

    r - 将具有交替缺失值的两个字符串列合并为一个

    r - 如何使用可以与 c() 组合的 R vctrs 包构建对象

    r - 使用 geom_dotplot 时绘图区域被截断

    javascript - jQuery 动画边距顶部

    c - C : Consult SEXP PROTECT Stack Height 中的 R 扩展

    r - 遍历多个列表以在 R 中为每个列表获取一个值

    css - 负底边距有什么影响

    html - 为什么这张图片有右边距?

    html - 将 DIV 定位在正确的位置

    css - 图像占据了两个图像之间不必要的填充和边距或空间