2

New to markdown here... Probably missing something simple... I'm trying to create a markdown document with plots generated in a for loop, but want to prevent wrapping the plots == i.e., keep a single column.

This is similar to what appears here: Knitr how to prevent text wrapping in output. I can't get this to work for plots.

Ideally there could be a line break between each. I found this, but I'm not familiar with pandoc and it's adding another layer to what I am trying to keep simple. Will go this route if it's the best solution, but I bet there is something simpler. Putting the plots in a table? Haven't figured that out yet...

Example code (edited since first post):

```{r}
value <- rnorm(100)
index <- c(rep(1,20),rep(2,20),rep(3,20),rep(4,20),rep(5,20))
group1 <- 1:5
group2 <- 1:2
df <- as.data.frame(cbind(value,index,group1,group2))

for (i in unique(df$group1)){
par(mfrow=c(1,2))
plot(value~index,data=subset(df,group1==i & group2==1),type="l",pch=16,main=unique(paste("plot",i,"group=",group1)))
plot(value~index,data=subset(df,group1==i & group2==2),type="l",pch=16,main=unique(paste("plot",i,"group=",group2)))
box("outer")
}
```
Community
  • 1
  • 1
gwslane
  • 31
  • 4
  • Hint: what do you think this line does? `par(mfrow=c(1,2))` – Marius Jul 23 '13 at 23:37
  • Creates a single row, two column plot... of which I would like many... each with their own titles, axes labels, etc, ordered in a column... – gwslane Jul 23 '13 at 23:39

1 Answers1

10

You can adjust the plot hook to append a new line


Edit - 25 July 2013

Yihui's far simpler suggestion to force a new line before the plot-inclusion line

i.e. add a chunk that defines

```{r setup, echo = FALSE}
hook_plot = knit_hooks$get('plot')
knit_hooks$set(plot = function(x, options) paste('\n', hook_plot(x, options), sep = ''))
```
mnel
  • 113,303
  • 27
  • 265
  • 254
  • This is a bit over my head... might even suggest that you copied a response into the wrong post??? – gwslane Jul 24 '13 at 00:25
  • 2
    @gwslane --- no. Have you tried adding this code chunk.... It works -- it appends a new line `\n` before every inclusion of a image, this means that the images will be on new lines (and hence in one column). – mnel Jul 24 '13 at 00:29
  • 3
    @gwslane -- what is sketchy, the approach or your understanding? – mnel Jul 24 '13 at 01:14
  • 2
    Not sketchy, this is how it works under the hood. You just haven't needed to look yet. – Bryan Hanson Jul 24 '13 at 01:28
  • 3
    @gwslane you are being a bit abrasive which is a good way to not have people spend time helping you in the future. – Tyler Rinker Jul 24 '13 at 02:39
  • 1
    +1 for an excellent answer **and** dealing graciously with an ungrateful, rude, OP. Shame I can't give +2 or more... – Gavin Simpson Jul 24 '13 at 02:56
  • my bad. faux pas. sorry 'bout that. up too late last night... still over my head. will take some more time with it. – gwslane Jul 24 '13 at 10:56
  • @mnel oh, stuff in my backyard was exposed to the public :) this looks a little bit scary... how about just `hook_plot = knit_hooks$get('plot'); knit_hooks$set(plot = function(x, options) paste('\n', hook_plot(x, options), sep = ''))`? – Yihui Xie Jul 25 '13 at 01:34
  • @yihui -- far simpler. I was about to suggest a `plot.format` option to pass as the `sprintf`, but your approach is far simpler. – mnel Jul 25 '13 at 01:57