如何自动将R图形轴标签调整为矩阵大小? [英] How to auto adjust R figure axis labels to matrix size?

查看:58
本文介绍了如何自动将R图形轴标签调整为矩阵大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使y轴标签的字体大小可调整为如图3所示的y轴上的输入数据大小,这与图1-2中的标签不相邻的当前情况形成了对比.相应的行.代码

  library("corrgram")#https://stackoverflow.com/a/40387233/54964ids--seq(1,18)x_at<-seq(0.075,0.925,length.out =长度(ids))y_at<-seq(0.075,0.91,length.out = length(ids))createLabels<-函数(xlab,ylab,x_labels,y_labels){ids--y_labels#在这里假设x_at<-seq(0.075,0.925,length.out =长度(ids))y_at<-seq(0.075,0.91,length.out = length(ids))多行文字(xlab,侧面= 1,线= 4)多行文字(ylab,侧面= 2,线= 3)轴(1,at = x_at,标签= x_labels,线= 1.5,刻度= F,cex.axis = .7)axis(2,at = y_at,labels = y_labels,line = 1,tick = F,cex.axis = .7,las = 1)#水平y轴标签;拉尔}corrgram(baseball,main =棒球数据PC2/PC1订单")createLabels(xlab =患者1 ID",ylab =患者2 ID",x_labels = ids,y_labels = ids) 

图.1输出有限的测试数据棒球,图2带有实际情况的输出,图3预期产量

预期输出:自动将标签字体大小调整为y轴上的输入数据大小;在

I want to make the fontsize of the y-axis labels adjustable to the input data size on the y-axis like in Fig. 3, in contrast to the current situation in Fig. 1-2 where labels are not next to the corresponding lines. Code

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)

Fig. 1 Output with the limited test data baseball, Fig. 2 Output with the real case, Fig. 3 Expected output

Expected output: automatically adjustable label font size to the input data size on the y-axis; example of the output created by makeMatrixPlot(list, ids, title) found here in Fig. 3

Testing Istrel's answer with big data set where long IDs

Complete code here which visualises correctly but gives backside strange outputs an NULLs, here some key points about optimised parameters

# 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

Output complications as the warning and many NULLs

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

Call it for instance by

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
Used graphic objects: corrplot, corrgram, ...
OS: Debian 8.5

解决方案

The corrgram package has been updated (version 1.11) with better support for labels along the axes. I've used the basic idea from Leo and added it to the package. For example:

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)

这篇关于如何自动将R图形轴标签调整为矩阵大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆