How to Reduce Space Gap Between Multiple Graphs in R

Remove space between two graphs

If you set the bottom and top margins to zero for each plot then you can do this:

> par(mfrow=c(2,1))
> par(mar=c(0,1,1,1))
> plot(1:10)
> par(mar=c(1,1,0,1))
> plot(1:10)

producing:

Sample Image

the mar parameter is bottom, left, top, right, and I should have made the left and bottom margins a bit bigger to make room for the axis labelling.

If you do a lot of this sort of "faceting" plot you might be better off using ggplot.

Add extra spacing between a subset of plots

The layout() function is your friend. You could for example define a plot matrix

1 2
3 4
5 6
7 8

and then put empty plots in for the third and fourth. Or just stick to six and call par to add extra spacing at the bottom.

How to reduce the empty space between the columns of a multiple plot layout?

You can save the plot by controlling with overall width and height of the image i.e. plot page size, for example, as a png file.

The argument pty = "s" forces a square plot. So by playing around with the width and height arguments of the plot page size you can get the appearance you want.

Alternatively you can use the respect argument of layout and use cex.lab to vary the axis label size.

layout(matrix(1:8, ncol=2, byrow=TRUE), respect = TRUE)

par(oma=c(0, 0, 0, 0),
mar=c(5,4,1,1),
pty="s")

for(i in 1:8) plot(iris$Sepal.Length, iris$Petal.Length, sub = i,
cex.lab = 1.5)

Sample Image

How to reduce the space between to plots when using patchwork

You can do this you can adjust + plot_layout(widths = c()) or you could adjust the margins using & theme(plot.margin = ...) however, I don't think plot.margin will work in this case.

To implement widths into your plot, you will need to add a spacer plot and use widths to adjust the spacer so that the plots full join together

 G3 <- G1 + plot_spacer() + G2 + plot_layout(widths = c(4, -1.1 ,4.5),guides = "collect")& theme(legend.position = "top")

Here the width of plot1 is 4, the width of the spacer plot is -1.1 which allows you to join the plots together and the width of plot2 is 4.5. I am not sure why plot2 needs to have a larger width than plot1 but the two plots don't look right when both their widths are set to 4.

example

How to decrease spacing between subplots

title1 / (p1 | p2) /
title2 / (p3 | p4) +
plot_layout(heights = c(1,6,1,6)) +
....

Sample Image

And you can adjust the text size in your title1 and title2 to make those more consistent with the other text. Here's with size = 5:

Sample Image

Reduce spacing between two plots on a single graph

By default stripchart plots the groups at x-positions 1…n. In your case, that’s at x=1 and x=2. You can control this with the at parameter:

stripchart(…, at = c(1.25, 1.75))

This for instances moves both groups inwards and makes the spacing more harmonious. Alternatively/additionally you can set the x-limits via xlim:

stripchart(…, xlim = c(0.5, 2.5))

This has a similar effect as above.



Related Topics



Leave a reply



Submit