Search code examples
rplotaxes

R: keeping axis labels and scales when plotting multiple graphs


I am using the following formula to plot 5 graphs together :

my_data <- as.data.frame(datasets::volcano)

layout(matrix(c(1,2,3,4,5,6), 3, 2, byrow = TRUE))
par(mar=c(1,1,1,1))
plot(my_data$V1, my_data$V2)
plot(my_data$V3, my_data$V4)
plot(my_data$V5, my_data$V6)
plot(my_data$V7, my_data$V8)
plot(my_data$V9, my_data$V10)
plot.new()

5 plots

It works well but I loose the scale of the y-axis of the 1, 3 and 5th plots and the x-axis of the latter. This keeps happening for any future ones I do until I dev.off().

Thanks!


Solution

  • You can use par(mfrow = c(3, 2)) and adjust your margins. You may need to increase the size of your window to accommodate the plot

    my_data <- as.data.frame(datasets::volcano)
    par(mfrow = c(3, 2))
    par(mar = c(2, 2, 2, 2))
    plot(my_data$V1, my_data$V2)
    plot(my_data$V3, my_data$V4)
    plot(my_data$V5, my_data$V6)
    plot(my_data$V7, my_data$V8)
    plot(my_data$V9, my_data$V10)
    

    p