问题描述
我有一些不同长度的地块列表,长度= 2,3,4,5,10,20 ...,我通常使用TargetRelation.withName("organizations")
来更改显示以查看所有地块。例如,如果列表包含4个图,那么我将创建一个2x2网格(par(mfrow = c(x,y))
);如果列表包含3个图,则我将创建一个1x3网格(par(mfrow = c(2,2)
)。
我的问题是:是否有一种方法可以使显示器很好地拆分,而无需我在par(mfrow = c(1,3)
中手动输入x
和y
,但是机器可以找出{{ 1}}和par(mfrow=c(x,y)
是基于给定列表的长度?
额外:标题字体大小是否可以相应更改?
解决方法
一个解决方案可以是
x=floor(sqrt(l)) # l is the length of the list
y=ceil(l/x)
,
您可以使用floor
个长度中的sqrt
。
lgts <- 1:20
p <- data.frame(l=lgts,x=floor(sqrt(lgts)))
p$y <- ceiling(with(p,l/x))
p
# l x y
# 1 1 1 1
# 2 2 1 2
# 3 3 1 3
# 4 4 2 2
# 5 5 2 3
# 6 6 2 3
# 7 7 2 4
# 8 8 2 4
# 9 9 3 3
# 10 10 3 4
# 11 11 3 4
# 12 12 3 4
# 13 13 3 5
# 14 14 3 5
# 15 15 3 5
# 16 16 4 4
# 17 17 4 5
# 18 18 4 5
# 19 19 4 5
# 20 20 4 5
op <- par(mfrow=c(p$x[1],p$y[1])) ## set par
## plot 1
par(op) ## reset
对于标题字体大小,您可以使用sqrt
乘以一个宜人的常数:
plot(...,main="")
mtext("Title",3,1,cex=sqrt(p$l[1]))