Construct several images of different sizes in a "for" loop
I'm new to knitr and markdown and this is my first question. Perhaps this question has a simple answer that I just cannot find.
I have a for loop that creates 3 ggplots. The cycle runs 300 to 400 times depending on the data input. I want to define the size of these three images as:
1st image: width = 7, height = 3
2nd image: width = 7, height = 3
3rd image: width = 7, height = 12
So far I am using the following code:
```{r calc, echo=FALSE, warning=FALSE, message=FALSE, results='asis', fig.show='asis',fig.height=3}
for(x.PS in 1:length(trace.input))
{
# some data transformation by self-written functions
# create and save plot for the normalised version
ggp.PS.norm <- ggplot(print.PS.norm, aes(x = Time, y = Voltage, col = Pulse))
fig.PS.norm <- ggp.PS.norm + geom_line()
# create and save plot for the modified version
ggp.PS.smooth <- ggplot(print.PS.smooth, aes(x = Time, y = Voltage, col = Pulse))
fig.PS.smooth <- ggp.PS.smooth + geom_line()
# create and save plot for the modified version as facet grid
fig.PS.smooth.single <- ggp.PS.smooth + geom_line() + facet_grid(FigCol ~ FigRow)
print(fig.PS.norm)
print(fig.PS.smooth)
print(fig.PS.smooth.single)
}
```
Eventually, I hope to get one large PDF with 3 x 300 to 400 paintings
I hope this code is clear even without hard data.
best paj
source to share
I don't know if this is kosher, but here's one approach:
I am using rmarkdown since this is your question, but this could be latex adapted
---
output:
html_document:
css: ~/knitr.css
---
```{r, include=FALSE}
library(knitr)
opts_knit$set(progress = FALSE, verbose = FALSE)
opts_chunk$set(warning=FALSE, message=FALSE, echo=FALSE)
## this function is basically creating chunks within chunks, and then
## I use results='asis' so that the html image code is rendered
kexpand <- function(ht, cap) {
cat(knit(text = knit_expand(text =
sprintf("```{r %s, fig.height=%s, fig.cap='%s'}\n.pl\n```", cap, ht, cap)
)))}
library(ggplot2)
```
```{r, results='asis', fig.width=7}
for (ii in 1:2) {
## do some stuff
cat('<br />')
cat(paste0('Page', ii))
## all plots should be saved as .p1 and then use kexpand
.pl <- qplot(mpg, wt, data=mtcars)
kexpand(3, 'fig1')
.pl <- qplot(mpg, wt, data=mtcars, colour=cyl)
kexpand(3, 'fig2')
.pl <- qplot(mpg, wt, data=mtcars, size=cyl)
kexpand(7, 'fig3')
cat('<br /><br />')
}
```
And this is my conclusion
source to share
My own solution at the end:
At some point I am calling this snippet below in the parent script
<<test_child,results='asis'>>=
knit.out <- NULL
for(x.PS in 1:length(trace.input)) {
knit.out <- c(knit.out,
knit_child("rawCAP_child_0-2.Rnw",
quiet = TRUE,
envir = globalenv()
)
)
}
cat(paste(knit.out, collapse = '\n'))
@
The child script does some calculations and creates numbers like this
<<,echo=FALSE>>=
# some functions creating the figures, for example the one below
ggp.PS.smooth <- ggplot(print.PS.smooth, aes(x = Time, y = Voltage, col = Pulse))
fig.PS.smooth <- ggp.PS.smooth +
geom_line() +
scale_x_continuous(
limits = c(len.1,len.total)
) +
scale_y_continuous(
limits = c(-4,4)
) +
labs(
#title = "Data smoothed by a value of XX",
x = "Time [sec.]",
y = "Voltage [mV]")
@
These pieces print numbers with different heights, there is also one table.
<<,fig.height=2,fig.width=7,fig.align='center'>>=
print(fig.PS.smooth)
@
<<,echo=FALSE,results='asis',fig.align='center'>>=
xtable(table.PS.CAP.stat[c(2:5,13:16)],
caption = "Statistics about all Pulses")
@
<<,fig.height=3,fig.width=7,fig.align='center'>>=
print(fig.PS.CAP)
@
<<,fig.height=12,fig.width=7,fig.align='center'>>=
print(fig.PS.smooth.single)
@
I am very happy with this solution because you can install almost everything without a workaround
For more information, please see page Yihui , especially a look here and perhaps, fooobar.com/questions/696493 / ... . Yihui seems to be on stackoverflow too.
Best paj
source to share