Justyna
Justyna

Reputation: 777

Increasing space between plots in rmarkdown

I am trying to plot two figures in one line and I want to increase the space between them. I searched this forum and a few other websites but none of the options I found seems to be working. Changing the mai, mar and oma values moved everything around but the space remains the same. How can I keep the figures as they are now (size wise) but increasing the gap between them?

Here is my code:

```{r echo=FALSE, fig.width=6, fig.height=6}
g.erd <- erdos.renyi.game(100, 150, type="gnm")
par(mfrow = c(1, 2),  mai = c(1, 0.1, 0.1, 0.1))
plot(g.erd, layout=layout.circle, vertex.label=NA)
```

```{r echo=FALSE, fig.width=3, fig.height=3.5}
hist(degree(g.erd), xlab="Degree", ylab="Frequency", main="")
par(mfrow = c(1, 1))  
```

and here is how my plot looks like right now: https://i.sstatic.net/V2Fc7.png

Upvotes: 4

Views: 12675

Answers (3)

TYL
TYL

Reputation: 1637

You can try adding markdown breaks between each chunk. <br>, like this:

```{r, echo=F}
plot(cars)
```

<br><br><br>

```{r, echo=F}
plot(cars)
```

Before:

enter image description here

After:

enter image description here

You can stack multiple <br> to achieve the desired gap you want.

Upvotes: 3

Mark Staples
Mark Staples

Reputation: 71

A 'hackish' solution in ggplot2 would be to put extra line spaces before the beginning of your second graph's title with \n like so:

ggtitle("\n\nPlot Title")

Upvotes: 7

Tad Dallas
Tad Dallas

Reputation: 1189

This approach kind of works. It depends on why you want the different sizes, but you may be able to fiddle with the layout width and height parameters, or the par(mar=c() to get the spacing and size you want. You could also create a layout that have 3 plotting areas, and leave one blank, as a way to try to force the smaller histogram into the desired location (layout.show(layout(matrix(c(1,1,2,3),ncol=2)))).

```{r echo=FALSE, fig.width=6, fig.height=6}
library(igraph)
g.erd <- erdos.renyi.game(100, 150, type="gnm")

layout(matrix(c(1,2), ncol=2), width=c(1,1))
par(mar=c(1,1,1,1))
plot(g.erd, layout=layout.circle, vertex.label=NA)

par(mar=c(10,5,9,1))
hist(degree(g.erd), xlab="Degree", ylab="Frequency", main="")
```

Hope this helps. Good luck.

edit: I've changed the plotting code to approximate equal graph size, but it's just sort of a guess, and other folks might be able to offer a better solution.

Upvotes: 0

Related Questions