在“for”中绘制具有不同尺寸的几张图片。循环 [英] Plot several pictures with different size in a "for" loop
问题描述
我是knitr和markdown的新手,这是我问的第一个问题。也许这个问题有一个简单的答案,我找不到。
我有一个for循环,它创建了3个ggplots。根据数据输入,循环运行300到400次。我想定义这3张图片的尺寸为:
第一张图片:width = 7,height = 3
第二张照片:width = 7,height = 3
第三张图片:width = 7,height = 12
<
```{calc calc,echo = FALSE,warning = FALSE, (x.PS in 1:length(trace.input))
{$ b(message.input)='asis',fig.show ='asis',fig.height = 3}
$ b#通过自写函数进行一些数据转换
#创建并保存标准化版本的图
ggp.PS.norm < - ggplot(print.PS.norm,aes( x =时间,y =电压,col =脉冲))
fig.PS.norm < - ggp.PS.norm + geom_line()
#创建并保存修改后的图版本
ggp.PS.smooth< - ggplot(print.PS.smooth,aes(x = Time,y = Voltage,col = Pulse))
fig.PS.smooth< - ggp。 PS.smooth + geom_line()
#为修改后的版本创建并保存绘图,如facet grid
fig.PS.smooth.sin gle< - ggp.PS.smooth + geom_line()+ facet_grid(FigCol〜FigRow)
print(fig.PS.norm)
print(fig.PS.smooth)
print(fig.PS.smooth.single)
}
```
最后,我希望能得到一个带有3 x 300到400张照片的大型PDF文件。我希望这个代码即使没有任何硬性数据也是可以理解的。
/ p>
best paj
我不知道这是否是犹太教徒但这里有一种方法:
我正在使用rmarkdown,因为这是您的问题所在in,but this can be adapted to latex
---
输出:
html_document:
css:〜/ knitr.css
---
$ b $```{r,include = FALSE}
library(knitr)
opts_knit $ set progress = FALSE,verbose = FALSE)
opts_chunk $ set(warning = FALSE,message = FALSE,echo = FALSE)
##这个函数基本上是在块内创建块,然后
##我使用results ='asis'所以html图像代码被渲染为
kexpand< - function(ht,cap){
cat(knit(text = knit_expand(text =
sprintf(```{r% s,fig.height =%s,fig.cap ='%s'} \\\
.pl\\\
```,cap,ht,cap)
)))}
library(ggplot2)
```
```{r,results ='asis',fig.width = 7}
for ii in 1:2){
##做一些事情
cat('< br />')
cat(paste0('Page',ii))
##所有图必须保存为.p1,然后使用kexpand
.pl< - qplot(mpg,wt,data = mtcars)
kexpand(3,'图1')
.pl< -qplot(mpg,wt,data = mtcars,color = cyl)
kexpand(3,'fig2')
.pl <-qplot(mpg,wt,data = mtcars,size = cyl)
kexpand(7,'fig3')
cat('< br />>< br />')
}
```
这是我的输出
I am new to knitr and markdown and this is my first question asked. Maybe this question has a simple answer that I just can't find.
I have a for-loop, which creates 3 ggplots. The loop runs 300 to 400 times depending on the data input. I want to define the size of these 3 pictures as:
1st picture: width = 7, height = 3
2nd picture: width = 7, height = 3
3nd picture: 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)
}
```
In the end I hope to get one big PDF-file with 3 x 300 to 400 pictures
I hope this code is understandable even without any hard data.
best paj
I don't know if this is kosher but here is one approach:
I'm using rmarkdown since that is what your question is in, but this could be adapted to latex
---
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 output
这篇关于在“for”中绘制具有不同尺寸的几张图片。循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!