【问题标题】:How to auto adjust R figure axis labels to matrix size?如何将 R 图形轴标签自动调整为矩阵大小?
【发布时间】:2017-04-05 05:26:04
【问题描述】:

我想让 y 轴标签的字体大小可调整为 y 轴上的输入数据大小,如图 3 所示,与图 1-2 中标签不在旁边的当前情况相反相应的行。 代码

library("corrgram")

# https://stackoverflow.com/a/40387233/54964
ids <- seq(1,18)
x_at <- seq(0.075, 0.925, length.out = length(ids))
y_at <- seq(0.075, 0.91, length.out = length(ids))

createLabels <- function(xlab, ylab, x_labels, y_labels){
ids <- y_labels # assume here
x_at <- seq(0.075, 0.925, length.out = length(ids))
y_at <- seq(0.075, 0.91, length.out = length(ids))
mtext(xlab, side = 1, line = 4)
mtext(ylab, side = 2, line = 3)
axis(1, at=x_at, labels=x_labels, line = 1.5, tick=F, cex.axis=.7)
axis(2, at=y_at, labels=y_labels, line = 1, tick=F, cex.axis=.7, las=1) # horizontal y-axis labels; rawr
}

corrgram(baseball,main="Baseball data PC2/PC1 order")
createLabels(xlab="Patient 1 ID", ylab="Patient 2 ID", x_labels=ids, y_labels=ids)

图。 1 输出有限的测试数据棒球, 图 2 真实案例的输出, 图 3 预期输出

预期输出:在y轴上根据输入数据大小自动调整标签字体大小; makeMatrixPlot(list, ids, title) 创建的输出示例在图 3 中找到 here

使用长 ID 的大数据集测试 Istrel 的 answer

完整的代码here,它可以正确显示,但后面的奇怪输出是NULLs,这里有一些关于优化参数的关键点

# https://stackoverflow.com/a/40485734/54964
cex_lab<-0.9 # little smaller fontsize for matrix >= 20x20
oma<-c(4, 4, 6, 4)
gap<-0
las<-2 # both axis labels always perpendicular

输出复杂性作为警告和许多 NULL

In max(l.wid) : no non-missing arguments to max; returning -Inf
[[1]]
[[1]][[1]]
NULL
...
[[1]][[7]]
NULL


[[2]]
[[2]][[1]]
NULL
...    
[[2]][[7]]
NULL


[[3]]
[[3]][[1]]
NULL
...  
[[3]][[7]]
NULL

例如调用它

library("corrplot")
library("psych")

ids <- seq(1,11) 

M.cor <- cor(mtcars)
colnames(M.cor) <- ids
rownames(M.cor) <- ids

p.mat <- psych::corr.test(M.cor, adjust = "none", ci = F)
p.mat <- p.mat[["r"]]

corrplot(M.cor, 
  method = "color", 
  type = "upper", 
  tl.col = 'black', 
  diag = TRUE, 
  p.mat = p.mat, 
  sig.level = 0.0000005
)

createLabels(xlab="Patient 1 ID", ylab="Patient 2 ID and Ages", x_labels=ids, y_labels="") 

R:3.3.1
使用的图形对象:corrplotcorrgram、...
操作系统:Debian 8.5

【问题讨论】:

    标签: r resize axis-labels


    【解决方案1】:

    Corrgram 使用来自par()mfrow 绘制正方形。我们可以用它来标记。标签的数量应等于矩阵中的列数。如果在corrgram 函数中设置omagap 参数,则必须在createLabels 函数中指定相同的参数。

    顺便说一句,如果你使用 corrgram 而不使用 main 参数,你将需要 oma=c(4,4,4,4) in createLabels

    createLabels <- function(xlab, ylab, x_labels, y_labels, 
                    cex_lab=1.3, oma=c(4, 4, 6, 4), gap=0, las=0){
    # oma and gap args same as in corrgram
    nc <- length(x_labels) # get number of columns
    
    # Inititate mfrow partition as in corrgram
    opar <- par(mfrow = c(nc, nc), mar = rep.int(gap/2, 4), oma = oma) #save old par parameters
    
    # Create matrix of outer cells indexes and positions
    left_side <- matrix(c(nc:1, rep(1, nc), rep(2, nc)), nc)
    bottom_side <- matrix(c(rep(nc, nc), 1:nc, rep(1, nc)), nc)
    sides <- rbind(left_side, bottom_side)
    
    
    # Dublicate labels vector
    labels <- c(y_labels, x_labels)
    
    # Loop over each outer cell
    for (i in c(1:(nc*2))){
        cell_ind <- sides[i, ]
        par(mfg=c(cell_ind[1], cell_ind[2], nc, nc)) # set current plotting cell
        usr<-par("usr")
        clip(usr[1], -2, usr[3], usr[4]) # alter clipping region
        axis(cell_ind[3], at=0.5, labels=labels[i], 
            outer=TRUE, xpd=NA, lwd=NA, mgp=c(3,0,0.2), cex.axis=cex_lab, las=las)
    }
    
    # Add labels
    par(opar)
    mtext(xlab, side = 1, line = -1.5, outer=TRUE, xpd=NA)
    mtext(ylab, side = 2, line = -1.5, outer=TRUE, xpd=NA)
    }
    
    corrgram(baseball,main="Baseball data PC2/PC1 order")
    createLabels(xlab="Patient 1 ID", ylab="Patient 2 ID", 1:18, 1:18)
    

    【讨论】:

    • 1:18 是函数的参数。即createLabels("X-lab", "Y-lab", x_labels=c(1:18))。因此,您可以指定任何名称向量,而其长度等于数据大小。如果你需要像 c("a", "b", "c")这样的名字怎么办
    • 我添加了 y_labels。现在就试试。当然,您可以将这些参数作为硬编码变量移动。
    • 试试las=2,即始终垂直。至于字体大小,是cex_lab参数,试试改吧。
    • 最初,您询问了corrgram。现在你说corrplot有问题。
    • 另外,对于我的函数,您需要长度完全等于数据维度的标签向量。 IE。对于您的示例,x_labels AND y_labels 的长度应为 11(在您的 sn-p 中,y_labels 的长度等于 1)。
    【解决方案2】:

    corrgram 包已更新(1.11 版),更好地支持沿轴的标签。我使用了 Leo 的基本想法并将其添加到包中。例如:

    require(corrgram)
    labs=colnames(state.x77)
    corrgram(state.x77, oma=c(7, 7, 2, 2),
             outer.labels=list(bottom=list(labels=labs,cex=1.5,srt=60),
                               left=list(labels=labs,cex=1.5,srt=30)))
    mtext("Bottom", side=1, cex=2, line = -1.5, outer=TRUE, xpd=NA)
    mtext("Left", side=2, cex=2, line = -1.5, outer=TRUE, xpd=NA)
    

    【讨论】:

    • 已确认,现在corrgram 1.11 对标签的支持得到了更好的支持。感谢您的新闻和更新!
    猜你喜欢
    • 1970-01-01
    • 2022-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-06
    • 2012-03-13
    相关资源
    最近更新 更多