繁体   English   中英

如何使用 boxplot() 将刻度线标签移离轴

[英]How to move tick mark labels away from axis using boxplot()

这是一个用于演示的玩具示例:

boxplot(1:90 ~ c({1:90} %% 3),names=c("A","B\nB","C\nC\nC"))

如果你绘图,你会看到我的查询的动机。 我想将组的刻度线标签移离轴。 而且,出于我自己的原因,我不想“不打印任何内容”并使用 axes() 函数……相反,我想知道如何通过将参数传递给 boxplot() 调用的子例程来做到这一点。 我的理解是这应该是可行的,但迄今为止我已经证明没有成功。

例如,这里提出的解决方案对我的目的来说太不雅了:
R,更改轴刻度线和刻度线标签之间的距离

有谁知道如何做到这一点?

检查输入参数的语法后,函数boxplot()调用“bxp”函数。 在这里你可以看到它传递给它的参数:

if (plot) {
    if (is.null(pars$boxfill) && is.null(args$boxfill)) 
        pars$boxfill <- col
    do.call("bxp", c(list(z, notch = notch, width = width, 
        varwidth = varwidth, log = log, border = border, 
        pars = pars, outline = outline, horizontal = horizontal, 
        add = add, at = at), args[namedargs]))
    invisible(z)
} 

函数bxp依次调用函数“axis”,它只查看“xaxt”、“yaxt”、“xaxp”、“yaxp”、“las”、“cex.axis”、“col.axis”参数来绘制轴:

if (axes) {
    ax.pars <- pars[names(pars) %in% c("xaxt", "yaxt", "xaxp", 
        "yaxp", "las", "cex.axis", "col.axis", "format")]
    if (is.null(show.names)) 
        show.names <- n > 1
    if (show.names) 
        do.call("axis", c(list(side = 1 + horizontal, at = at, 
            labels = z$names), ax.pars))
    do.call("Axis", c(list(x = z$stats, side = 2 - horizontal), 
        ax.pars))
}

从上面可以看出,没有任何参数可以改变可以传递给 boxplot 的 X 标签的偏移量。

默认情况下,使用ggplot以您想要的方式显示标签:

df <- data.frame( x= as.factor(c({1:90} %% 3)), y = 1:90 )
ggplot(df,aes(x=x, y=y) ) + 
  geom_boxplot()+ 
  scale_x_discrete(labels=c("A","B\nB","C\nCC\nCCC"))

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM