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.
To place multiple figures side-by-side from the same code chunk, you can use the fig. show='hold' option along with the out. width option. Figure 2.5 shows an example with two plots, each with a width of 50% .
We can insert headings and subheadings in R Markdown using the pound sign # . There are six heading/subheading sizes in R Markdown. The number of pound signs before your line of text determines the heading size, 1 being the largest heading and 6 being the smallest.
The following sections dive into the three components of an R Markdown document in more details: the markdown text, the code chunks, and the YAML header.
For example, # Say Hello to markdown . A single hashtag creates a first level header. Two hashtags, ## , creates a second level header, and so on. italicized and bold text - Surround italicized text with asterisks, like this *without realizing it* .
It turns out that you need an extra carriage return. As Yihui pointed out, a header needs a blank line before it to be interpreted correctly by Markdown. Adding a simple cat("\n\n") to the end of my loop fixes things.
I had tried only adding one line break, but missed that two were needed to get the header to be interpreted correctly.
---
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"))
```
I paste a short example I had prepared for another question:
```{r, results='asis'}
cat("\n\n## Title")
for (s in unique(cars$speed)){
cat("\n\n### speed",s)
}
```
I deleted that example in the other question as it was on markdown syntax and didn't fit with Latex/ Rnw topic.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With