在R中使用spineplot
,我试图创建一系列图表,比较一些潜在的协变量与年龄类别,以便我们可以做一些初步的数据探索。我在R Markdown中编写了这段代码,我使用RStudio将文件编织成HTML输出。但是,我很难通过我的循环生成每个绘图以获得适当的RMarkdown标头。我已经尝试将cat("## my header")
放在产生每个绘图的循环中,但由于某种原因它只显示第一个绘图,而不是其余的。 打印出其余部分,但不会被解释为Markdown。
以下是我的代码示例:
---
title: "Minimal Working Example"
author: "TARehman"
date: "Wednesday, August 27, 2014"
output:
html_document:
toc: yes
---
# Spineplots
```{r mwe, echo=FALSE, results='asis', fig.height=5, fig.width=8, dpi=300}
tempdata <- data.frame(age=c("0-1","0-1","1-3","1-3","3-7","10-15","3-7","7-10"),
covA=c("Class 0","Class 1","Class 3","Class 2","Class 4","Class 3","Class 1","Class 2"),
covB=c("Class 1","Class 3","Class 2","Class 4","Class 4","Class 1","Class 1","Class 0"),
covC=c("Class 3","Class 3","Class 2","Class 3","Class 1","Class 4","Class 4","Class 4"))
temp_covars <- c("covA","covB","covC")
temp_locvec <- c(0.1,0.3,0.5,0.7,0.9)
temp_labvec <- c("0-1","1-3","3-7","7-10","10-15")
temp_colvec <- rainbow(n = 5,start = 7/12,end = 9/12,alpha = 0.8)
for(x in temp_covars) {
cat(paste("## Spineplot of",x,"vs. age groups\n",sep=" "))
spineplot(x = tempdata[[x]],
y = tempdata$age,
ylab = "Age Group",
xlab = "Class",
col = temp_colvec,
off = 0,
yaxlabels = c(NA,NA,NA,NA,NA),
main = paste("Classes of",x,"versus age groups",sep=" "))
for(j in 1:5) {
axis(side = 2,
at = temp_locvec[j],
labels = temp_labvec[j],
col.axis = temp_colvec[j],
font.axis = 2)
}
rm(j)
}
rm(list=c("x","temp_locvec","temp_labvec","temp_colvec"))
```
输出看起来像这样。
答案 0 :(得分:5)
事实证明你需要额外的回车。正如Yihui指出的那样,标题需要一个空行才能被Markdown正确解释。在我的循环结尾添加一个简单的猫(&#34; \ n \ n&#34;)可以解决问题。
我曾尝试只添加一个换行符,但错过了需要两个才能正确解释标题。
---
title: "Working Solution"
author: "TARehman"
date: "September 3, 2014"
output:
html_document:
toc: yes
---
# Spineplots
```{r mwe, echo=FALSE, results='asis', fig.height=5, fig.width=8, dpi=300}
tempdata <- data.frame(age=c("0-1","0-1","1-3","1-3","3-7","10-15","3-7","7-10"),
covA=c("Class 0","Class 1","Class 3","Class 2","Class 4","Class 3","Class 1","Class 2"),
covB=c("Class 1","Class 3","Class 2","Class 4","Class 4","Class 1","Class 1","Class 0"),
covC=c("Class 3","Class 3","Class 2","Class 3","Class 1","Class 4","Class 4","Class 4"))
temp_covars <- c("covA","covB","covC")
temp_locvec <- c(0.1,0.3,0.5,0.7,0.9)
temp_labvec <- c("0-1","1-3","3-7","7-10","10-15")
temp_colvec <- rainbow(n = 5,start = 7/12,end = 9/12,alpha = 0.8)
for(x in temp_covars) {
cat(paste("## Spineplot of",x,"vs. age groups\n",sep=" "))
spineplot(x = tempdata[[x]],
y = tempdata$age,
ylab = "Age Group",
xlab = "Class",
col = temp_colvec,
off = 0,
yaxlabels = c(NA,NA,NA,NA,NA),
main = paste("Classes of",x,"versus age groups",sep=" "))
for(j in 1:5) {
axis(side = 2,
at = temp_locvec[j],
labels = temp_labvec[j],
col.axis = temp_colvec[j],
font.axis = 2)
}
rm(j)
cat("\n\n")
}
rm(list=c("x","temp_locvec","temp_labvec","temp_colvec"))
```
答案 1 :(得分:1)
我粘贴了我为another question准备的简短示例:
```{r, results='asis'}
cat("\n\n## Title")
for (s in unique(cars$speed)){
cat("\n\n### speed",s)
}
```
我在the other question中删除了该示例,因为它采用了markdown语法,并且不适合Latex / Rnw主题。