简体   繁体   中英

Setting par() based on the number of items

I have some lists of plots with different length, length = 2,3,4,5, 10, 20 ... and I usually use par(mfrow = c(x,y)) to alter my display to see all the plots. For example, if the list contains 4 plots, then I will make a 2x2 grid ( par(mfrow = c(2,2) ); if the list contains 3 plots, then I will make a 1x3 grid ( par(mfrow = c(1,3) ).

My question is: Is there a way that the display can split up nicely without me manually input the x and y in par(mfrow=c(x,y) , but instead the machine can find out x and y based on the length of the given list?

Extra: and the title font size can change accordingly?

One solution can be,

x=floor(sqrt(l)) # l is the length of the list
y=ceil(l/x)

You could use floor of sqrt of the lengths.

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

For the title font size you could use sqrt multiplied by a pleasant constant:

plot(..., main="") 
mtext("Title", 3, 1, cex=sqrt(p$l[1]))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM