问题
I have a large dataset of 16 independent timeseries. I would like to plot them in a 3x7 grid with the top row being each of the timeseries ending in IN and the bottom row being each of the timeseries ending in OUT. In the middle row, I will repeat each of the two timeseries ending in RN that corresponds to each IN/OUT couple. I received assistance writing the script in an earlier post, but still cannot get R Studio to output my plots. Any wisdom or guidance would be appreciated. Here are my dataset and code:
title: "Data Report"
author: "Dr.Vini42"
date: "February 17, 2016"
output: word_document
---
```{r, echo=FALSE}
library(ggplot2)
numbers <- read.csv("./AllData.csv", header=TRUE)
num <- (ncol(numbers) - 4)/4*3 #converts 36 columns to 24 plots involving 8 timeseries
par(mfrow=c(3,7))
for(i in 1:num){
if (i < 8) {
qplot(as.Date(numbers[[4*i+1]],"%m/%d/%Y %H:%M"), numbers[[4*i+2]], xlab="Date", ylab="Feet", main=names(numbers)[4*i+2])
} else if (i < 15) {
qplot(as.Date(numbers[[4*i-6]],"%m/%d/%Y %H:%M"), numbers[[4*i-5]], xlab="Date", ylab="Feet", main=names(numbers)[4*i-5])
} else {
qplot(as.Date(numbers[[4*i-13]],"%m/%d/%Y %H:%M"), numbers[[4*i-12]], xlab="Date", ylab="Feet", main=names(numbers)[4*i-12])
}
}
```
回答1:
In a for( )
loop in knitr
you need to put your plots inside a print( )
statement
Outside of the loop knitr
recognises it needs to print and does it for you. Inside the loop it doesn't.
```{r}
...
par(mfrow=c(3,7))
for(i in 1:num){
if (i < 8) {
print( qplot(as.Date(numbers[[4*i+1]],"%m/%d/%Y %H:%M"), numbers[[4*i+2]], xlab="Date", ylab="Feet", main=names(numbers)[4*i+2]) )
} else {
...
}
}
```
Update
I don't normally download whole files to test code, but, I've made an exception...
To see your issue, run these lines
i <- 1
as.Date(numbers[[4*i+2]],"%m/%d/%Y %H:%M") ## this should error
numbers[[6]] ## this shows you the data
You are trying to convert the data in numbers[[6]]
(the 6th column) to a date in the format mm/dd/yyyy hh:mm
, but the values are just decimals, and so they can't be converted into dates.
来源:https://stackoverflow.com/questions/35471802/several-plots-of-large-time-series-not-displaying-in-r-markdown