gpt4 book ai didi

R,布局 : How to determine the width of the parts of a plotting region in order to center a title?

转载 作者:行者123 更新时间:2023-12-04 05:52:17 25 4
gpt4 key购买 nike

我想将标题相对于我使用以下代码创建的绘图矩阵(而不是整个绘图)居中:

d <- 3
d2 <- d*d
layout.mat <- matrix(1:d2, byrow=TRUE, ncol=d) # plot matrix
layout.mat <- cbind(layout.mat, rep(0, d)) # space
layout.mat <- cbind(layout.mat, rep(d2+1, d)) # column on the right side
wspace <- 6*par("csi")*2.54 # width of the space in character height in cm
wside <- 3*par("csi")*2.54 # width of the right side in character height in cm
layout(layout.mat, respect=TRUE, widths=c(rep(1, d), lcm(wspace), lcm(wside)))
layout.show(d2+1)
par(mar=rep(0, 4), oma=c(4,4,6,4))
for(i in 1:d){
for(j in 1:d){
plot.new()
plot.window(xlim=c(0,1), ylim=c(0,1))
ll <- par("usr")
rect(ll[1], ll[3], ll[2], ll[4])
text(0.5, 0.5, paste("i=",i,", j=",j,sep=""), cex=1.4)
}
}
plot.new()
plot.window(xlim=c(0,1), ylim=c(0,1))
ll <- par("usr")
rect(ll[1], ll[3], ll[2], ll[4])
text(0.5, 0.5, "side", cex=1.4)

## title
mtext("This title should be centered according to the plot matrix", side=3,
line=2, outer=TRUE, adj=0.5, at=0.5)

如何确定(在给定的固定距离 wspacewside )绘图区域的宽度,以便我可以减去 wspace+wside从中确定绘图矩阵的宽度?最终目标是确定 adj 的精确值。的 mtext这样标题就在绘图矩阵上方居中,而不是整个绘图区域。

最佳答案

在您的 for 中添加此代码如何?环形:

if (i == 1 & j == 2){
mtext("This title should be centered according to the plot matrix", side=3,
line=2, outer=FALSE, adj=0.5, at=0.5)
}

请注意对 outer = FALSE 的更改.这是它的外观,标题文本较短(长版本在一个小情节的两侧运行,但仍居中):

enter image description here

当然,这个 hack 只在你有奇数列时才有效。更通用的解决方案可能是更改布局以在顶部包含一个短而宽的水平区域,并直接在该区域中绘制文本:
d <- 4
d2 <- d*d
layout.mat <- matrix(1:d2, byrow=TRUE, ncol=d) # plot matrix
layout.mat <- cbind(layout.mat, rep(0, d)) # space
layout.mat <- cbind(layout.mat, rep(d2+1, d)) # column on the right side
layout.mat <- rbind(c(rep(18,d),0,0),layout.mat) #Add row on top
wspace <- 6*par("csi")*2.54 # width of the space in character height in cm
wside <- 3*par("csi")*2.54 # width of the right side in character height in cm
#Note adjustments to heights
layout(layout.mat, respect=TRUE, widths=c(rep(1, d), lcm(wspace), lcm(wside)),
heights = c(0.25,rep(1,nrow(layout.mat)-1)))
layout.show(d2+1)
par(mar=rep(0, 4), oma=c(4,4,6,4))
for(i in 1:d){
for(j in 1:d){
plot.new()
plot.window(xlim=c(0,1), ylim=c(0,1))
ll <- par("usr")
rect(ll[1], ll[3], ll[2], ll[4])
text(0.5, 0.5, paste("i=",i,", j=",j,sep=""), cex=1.4)
}
}
plot.new()
plot.window(xlim=c(0,1), ylim=c(0,1))
ll <- par("usr")
rect(ll[1], ll[3], ll[2], ll[4])
text(0.5, 0.5, "side", cex=1.4)

## title
plot.new()
plot.window(xlim=c(0,1), ylim=c(0,1))
ll <- par("usr")
#rect(ll[1], ll[3], ll[2], ll[4])
text(0.5, 0.5, "top", cex=1.4)

enter image description here

关于R,布局 : How to determine the width of the parts of a plotting region in order to center a title?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9940994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com