Using spineplot
in R, I am trying to create a series of plots that compare some potential covariates against age categories so we can do some preliminary data exploration. I have this code written up in R Markdown and I am using RStudio to knit the file into an HTML output. However, I am having difficulty getting each plot produced by my loop to have an appropriate RMarkdown header. I have tried putting cat("## my header")
in the loop that produces each plot, but for some reason it only shows up for the first plot, and not the rest. It prints out for the rest of them, but does not get interpreted as Markdown.
Here's an example of my code:
---
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"))
```
The output looks like this.