1

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])
  }
}

``` 
Community
  • 1
  • 1
Dr.Vini42
  • 35
  • 4

1 Answers1

2

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.

tospig
  • 7,762
  • 14
  • 40
  • 79
  • I tried this approach and received the following error: Error in charToDate(x) : character string is not in a standard unambiguous format Calls: ... as.Date.numeric -> as.Date -> as.Date.character -> charToDate In addition: Warning message: In (function (toc = FALSE, toc_depth - 3, fig_width = 5, fig_height = 4, : table of contents for word_document requires pandoc >= 1.14 Execution halted – Dr.Vini42 Feb 18 '16 at 12:21
  • @Dr.Vini42 that suggests it's an issue with your data not being able to be converted to a date. Try and do all your `as.Date (number ( ... ) )` formatting outside of the plotting code – tospig Feb 18 '16 at 18:23
  • I'm confused about why I didn't see this error when I ran the code as a standalone R script. Anyway, I now have `numbers.date <- as.Date(numbers[[4*i+1]], "%m/%d/%Y %H:%M") qplot(numbers.date, numbers[[4*i+2]], xlab="Date", ylab="Feet", main=names(numbers)[4*i+2])` But I am receiving the same error message. When I run this new script as an R script, I receive the error: Error in charToDate(x) : character string is not in a standard unambiguous format – Dr.Vini42 Feb 18 '16 at 23:00
  • @Dr.Vini42 - you might have to turn this new issue into a separate question, but, what is `str(numbers)` ? – tospig Feb 18 '16 at 23:29
  • I'm not sure exactly what you're asking, but the command `str(numbers)` output: 'data.frame': 189038 obs. of 32 variables: $ Date6926RN : Factor w/ 189038 levels "1/1/2016 0:00",..: 86555 86556 86557 86558 86559 86560 86561 86562 86563 86564 ... $ X6926RN : num 0 0 0 0 0 0.02 0.01 0.06 0.07 0.11 ... $ Date6810 : Factor w/ 162744 levels "","1/1/2016 0:00",..: 82752 82753 82754 82755 82756 82757 82758 82759 82760 82761 ... $ X6810RN : num 0 0 0 0 0 0 0 0 0 0 ... $ Date6810EIN: Factor w/ 147389 levels "","1/1/2016 0:00",..: 70080 70081 70082 70083 70084 70085 70086 ... – Dr.Vini42 Feb 21 '16 at 17:04
  • Your dates are `factors`. Convert them to `character`. – tospig Feb 21 '16 at 17:11
  • Okay. I tried `numbers.date <- as.Date(as.character(numbers[[4*i+1]]),"%m/%d/%Y %H:%M")` and received the error `Error in .subset2(x, i, exact = exact) : subscript out of bounds` Then I tried `numbers.dt <- as.character(numbers[[4*i+1]]) numbers.date <- as.Date(numbers.dt,"%m/%d/%Y %H:%M")` and received the familiar error `Error in charToDate(x) : character string is not in a standard unambiguous format`. – Dr.Vini42 Feb 23 '16 at 22:18
  • @Dr.Vini42 I think you need to start a new question, and include the output of `dput(head(numbers))` so others can use your data directly. It's difficult to diagnose your issue without seeing the data. – tospig Feb 24 '16 at 00:25
  • dput(head(numbers)) "9/9/2015 9:54", "9/9/2015 9:56", "9/9/2015 9:58"), class = "factor"), X7010EOU = c(0.017, 0.021, 0.022, 0.022, 0.022, 0.021)), .Names = c("Date6926RN","X6926RN","Date6810","X6810RN","Date6810EIN","X6810EIN","Date6810EOU","X6810EOU","Date6810WIN","X6810WIN","Date6810WOU","X6810WOU","Date6812EIN","X6812EIN","Date6812EOU","X6812EOU","Date6812WIN","X6812WIN","Date6812WOU","X6812WOU","Date6926EIN","X6926EIN","Date6926EOU","X6926EOU","Date6926WIN","X6926WIN","Date6926WOU","X6926WOU","Date7010EIN","X7010EIN","Date7010EOU","X7010EOU"),row.names = c(NA, 6L), class = "data.frame") – Dr.Vini42 Feb 26 '16 at 02:25
  • I clipped the end of the output of dput(head(numbers)) into the last comment. I have included my full dataset in the question statement at the top of the page. Here is another link to it: [link](https://drive.google.com/file/d/0BzEFYP8cpjHpUlluSlNZaTN1RDA/view?usp=sharing) And I'm unsure of what additional question to post because I'm still unsure of what the problem is. I hope this helps. I wasn't exactly sure what you were asking for. – Dr.Vini42 Feb 26 '16 at 02:27
  • @Dr.Vini42 The purpose of `dput` is to output the structure of the data you're working with in such a way that someone else can copy & paste it and use it. But, it has to be the entire output, you can't clip it. It would be better to edit your question and paste in the entire output of `dput(head(numbers))` – tospig Feb 26 '16 at 09:33
  • @Dr.Vini42 - i've udpated my answer to show you your issue. – tospig Feb 26 '16 at 11:15