你如何在 knitr word 文档中创建 sections/subsections 和 ggplot
how do you create sections/subsections and ggplot in knitr word document
我有一个数据框 cpu 服务器用于许多不同的应用程序。像这样:
data
App Server Date Cpu
Web web01 1/1/2015 10
Web web01 1/2/2015 10
Web web01 1/3/2015 20
Web web01 1/4/2015 30
Web web01 1/5/2015 4
TomCat tom01 1/1/2015 10
TomCat tom01 1/2/2015 10
TomCat tom01 1/3/2015 20
TomCat tom01 1/4/2015 30
TomCat tom01 1/5/2015 4
我需要能够使用带有部分的 knitr 创建 ggplot 图表。例如,第一部分是 Web,接下来是 TomCat 等等。我需要能够循环执行此操作,因为有数百个应用程序。
我正在尝试这个,但我没有在 word 文档中看到图表:
{r qplot,fig.width=8, fig.height=5, message=FALSE}
library(ggplot2)
app<-unique(data$App, drop=TRUE)
app<-droplevels(app)
for (app in data){
ggplot(subset(data, App %in% data), aes(Date, Cpu, group=Server, colour=server))+geom_line() + facet_wrap(~Server)
}
我有两个问题:
- 如何像 sweave 一样在 knitr 中自动创建部分?
- 如何在 for 循环中创建 ggplot 图表?
非常感谢任何见解。
看来您需要更改几处:
- 你的
for
循环应该以 for(a in app){
开始
- 你的子集调用应该是
subset(data, App %in% a)
- 您的
ggplot
需要包裹在 print
中
- 其他几个小错别字。这是对我有用的代码。
---
title: "Untitled"
output: word_document
---
```{r}
data <-
"App Server Date Cpu
Web web01 1/1/2015 10
Web web01 1/2/2015 10
Web web01 1/3/2015 20
Web web01 1/4/2015 30
Web web01 1/5/2015 4
TomCat tom01 1/1/2015 10
TomCat tom01 1/2/2015 10
TomCat tom01 1/3/2015 20
TomCat tom01 1/4/2015 30
TomCat tom01 1/5/2015 4"
data <- read.table(text = data, header = TRUE)
```
## Start the Plots
```{r qplot,fig.width=8, fig.height=5, message=FALSE, results = 'asis'}
library(ggplot2)
app<-unique(data$App, drop=TRUE)
app<-droplevels(app)
for (a in app){
print(ggplot(subset(data, App %in% a),
aes(Date, Cpu, group=Server, colour=Server)) +
geom_line() +
facet_wrap(~Server))
cat("\n\n## New Plot\n\n")
}
```
我有一个数据框 cpu 服务器用于许多不同的应用程序。像这样:
data
App Server Date Cpu
Web web01 1/1/2015 10
Web web01 1/2/2015 10
Web web01 1/3/2015 20
Web web01 1/4/2015 30
Web web01 1/5/2015 4
TomCat tom01 1/1/2015 10
TomCat tom01 1/2/2015 10
TomCat tom01 1/3/2015 20
TomCat tom01 1/4/2015 30
TomCat tom01 1/5/2015 4
我需要能够使用带有部分的 knitr 创建 ggplot 图表。例如,第一部分是 Web,接下来是 TomCat 等等。我需要能够循环执行此操作,因为有数百个应用程序。
我正在尝试这个,但我没有在 word 文档中看到图表:
{r qplot,fig.width=8, fig.height=5, message=FALSE}
library(ggplot2)
app<-unique(data$App, drop=TRUE)
app<-droplevels(app)
for (app in data){
ggplot(subset(data, App %in% data), aes(Date, Cpu, group=Server, colour=server))+geom_line() + facet_wrap(~Server)
}
我有两个问题:
- 如何像 sweave 一样在 knitr 中自动创建部分?
- 如何在 for 循环中创建 ggplot 图表?
非常感谢任何见解。
看来您需要更改几处:
- 你的
for
循环应该以for(a in app){
开始
- 你的子集调用应该是
subset(data, App %in% a)
- 您的
ggplot
需要包裹在print
中
- 其他几个小错别字。这是对我有用的代码。
---
title: "Untitled"
output: word_document
---
```{r}
data <-
"App Server Date Cpu
Web web01 1/1/2015 10
Web web01 1/2/2015 10
Web web01 1/3/2015 20
Web web01 1/4/2015 30
Web web01 1/5/2015 4
TomCat tom01 1/1/2015 10
TomCat tom01 1/2/2015 10
TomCat tom01 1/3/2015 20
TomCat tom01 1/4/2015 30
TomCat tom01 1/5/2015 4"
data <- read.table(text = data, header = TRUE)
```
## Start the Plots
```{r qplot,fig.width=8, fig.height=5, message=FALSE, results = 'asis'}
library(ggplot2)
app<-unique(data$App, drop=TRUE)
app<-droplevels(app)
for (a in app){
print(ggplot(subset(data, App %in% a),
aes(Date, Cpu, group=Server, colour=Server)) +
geom_line() +
facet_wrap(~Server))
cat("\n\n## New Plot\n\n")
}
```