Fab
Fab

Reputation: 45

How to adapt the size of multiple plots?

How can I adapt the size of the following plots with regard to their length of the x-axis?

enter image description here

The width of the plots should refer to the length of their respective section of the x-axis. The height should be the same for all plots.

Upvotes: 2

Views: 158

Answers (1)

Rui Barradas
Rui Barradas

Reputation: 76653

The function you want is base graphics function help("layout").

First I will make up a dataset, since you have not posted one. I will not draw the regression lines, just the points.

Data creation code.

fun <- function(X, A) {
  apply(X, 1, function(.x){
    xx <- seq(.x[1], .x[2], length.out = 100)
    y <- A[1]*xx + A[2] + rnorm(100, 0, 25)
    list(xx, y)
  })}

Coef <- matrix(c(0.24, 0.54, 
                 0.75, 0.54,
                 0.33, 2.17,
                 0.29, 3.3,
                 0.29, 4.41), byrow = TRUE, ncol = 2)

X <- matrix(c(0.1, 0.49,
              0.5, 2.49,
              2.5, 3.9,
              4.0, 5.9,
              6.0, 12.0), byrow = TRUE, ncol = 2)

set.seed(1234)
res <- fun(X, Coef)

The problem.

Define a layout matrix with each plot in a sequence from first to 5th. And the widths given by the X ranges.

layout_mat <- matrix(c(1, 2, 3, 4, 5), 1, 5, byrow = TRUE)
w <- apply(X, 1, diff)
l <- layout(layout_mat, widths = w)
layout.show(l)

Now make some room for the axis annotation, saving the default graphics parameters, and plot the 5 graphs.

om <- par(mar = c(3, 0.1, 0.1, 0.1),
          oma = c(3, 2, 0.1, 0.1))
for(i in 1:5) plot(res[[i]][[1]], res[[i]][[2]])
par(om)

enter image description here

Upvotes: 1

Related Questions