如何自动调整R图轴标签到矩阵大小?

时间:2016-11-05 09:37:20

标签: r resize axis-labels

我想让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输出有限的测试数据 baseball , 图2实际输出, 图3预期输出

enter image description here enter image description here enter image description here

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

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

完整代码here可正确显示,但为NULL提供背面奇怪输出,这里有一些关于优化参数的关键点

# 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

2 个答案:

答案 0 :(得分:2)

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

顺便说一句,如果您使用corrgram而没有main参数,则oma=c(4,4,4,4)

中需要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)

enter image description here enter image description here

答案 1 :(得分:1)

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

corrgram

centers output