Search code examples
rlayoutplot

How to create a multiple plots having same X axis?


I am trying to make a single plot using dataset with same X axis but different Y axis. As an example, I have this dataset:

A1 <- rnorm(100)
B1 <- rnorm(100)
B2 <- rnorm(100)
B3 <- rnorm(100)

grid <- matrix(c(1:3),nrow=3,ncol=1,byrow=TRUE)
layout(grid)

plot(A1,B1)
plot(A1,B2)
plot(A1,B3)

This is what I get and comes with multiple X axis:

enter image description here

I know how to do it using ggplot2 but I am looking for another way like using layout. Any help would be much appreciated.


Solution

  • Its too easy by working with par(mar) and layout function.

    par(mar=c(6,6,4,4))
    layout(matrix(1:3, ncol = 1), widths = 1, heights = c(2.3,2,2.3), respect = FALSE)
    par(mar = c(0, 4.1, 4.1, 2.1))
    plot(B1,A1,xaxt='n')
    par(mar = c(0, 4.1, 0, 2.1))
    plot(B2,A1,xaxt='n')
    par(mar = c(4.1, 4.1, 0, 2.1))
    plot(B3,A1)
    

    enter image description here