3

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.stack.imgur.com/V2Fc7.png

Justyna
  • 737
  • 2
  • 10
  • 25
  • what is your output format? html, pdf, word? One solution would be to add some CSS if you're using html output – jalapic May 21 '15 at 15:57
  • 1
    grid table might help you here, take a look at this: http://stackoverflow.com/questions/21529926/arrange-ggplots-together-in-custom-ratios-and-spacing – Zfunk May 21 '15 at 16:23

3 Answers3

7

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")

Mark Staples
  • 71
  • 1
  • 3
1

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.

TYL
  • 1,577
  • 20
  • 33
0

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.

Tad Dallas
  • 1,179
  • 5
  • 13
  • So I actually want to have both figures to be of the same size and further apart then they are. But I can't figure out hoy to control for a plot placement in markdown. When I tried to put them with just one size (i.e., not splitting into two chunks) the graph was miniature compared to the histogram. – Justyna May 22 '15 at 11:59
  • Would it be possible to eyeball it by altering `mar` of each plot. igraph plots tend tend to have weird margins? – Tad Dallas May 23 '15 at 18:19