【发布时间】: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
使用的图形对象:corrplot、corrgram、...
操作系统:Debian 8.5
【问题讨论】:
标签: r resize axis-labels