2

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 have set up what I believe to be the necessary logic and syntax to accomplish, but I continue to struggle with data classes and can't get the script to run long enough to debug the rest. I am fairly new at R, so I am unsure of how to proceed.

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.POSIXct(as.Date(numbers[4*i+1])), numbers[4*i+2], data="numbers", xlab="Date", ylab="Feet", main=numbers[4*i+2,1])
  } else if (i < 15) {
    qplot(as.POSIXct(as.character(numbers[4*i-6]), format="%m/%d/%Y %H:%M"), numbers[4*i-5], data="numbers", xlab="Date", ylab="Feet", main=numbers[4*i-5,1])
  } else {
    qplot(as.POSIXct(as.character(numbers[4*i-13]), format="%m/%d/%Y %H:%M"), numbers[4*i-12], data="numbers", xlab="Date", ylab="Feet", main=numbers[4*i-12,1])
  }

In place of "as.POSIXct," I have also tried "as.Date," "as.POSIXlt," and simply plotting the dataset as is, but all result in similar errors informing me that ggplot can't use whatever class I have in use. I have tried using the basic plot function below, but then receive errors that my x and y are of different lengths, which I have checked and are of equal lengths.

par(mfrow=c(3,7))
for(i in 1:num){
  if (i < 8) {
    x <- as.POSIXct(as.character(numbers[4*i+1]), format="%m/%d/%Y %H:%M")
    x <- numbers[4*i+1]
    y <- numbers[4*i+2]
    main <- numbers[4*i+2,1]
  } else if (i < 15) {
    x <- as.POSIXct(as.character(numbers[4*i-6]), format="%m/%d/%Y %H:%M")
    y <- numbers[4*i-5]
    main <- numbers[4*i-5,1]
  } else {
    x <- as.POSIXct(as.character(numbers[4*i-13]), format="%m/%d/%Y %H:%M")
    y <- numbers[4*i-12]
    main <- numbers[4*i-12,1]
  }
  plot(x, y, type="l", xlab="Date", ylab="Feet", heading=main)
}

Any wisdom or guidance would be appreciated. Here is my dataset: https://drive.google.com/file/d/0BzEFYP8cpjHpUlluSlNZaTN1RDA/view?usp=sharing

Dr.Vini42
  • 35
  • 4

1 Answers1

1

There are a few things in here that I have changed, [ (subsetting) was confused with [[ (indexing), the ggplot data frame was specified as a character variable (resulting in an error message that ggplot could not deal with character variables), the column names (that are read into the names attribute) was being confused with the first row etc.

To be fair, this happens to every one at first. I would strongly suggest reading the first two chapters of Hadley Wickham's "Advanced-R" programming book (or something similar) before you try anything else this ambitious. You will see what I mean, it is very difficult working with R if one does not understand names (and other attributes), subsetting, data frames, and lists in general (a data frame is a specialized list in R) fairly well.

Link to the online version is here: http://adv-r.had.co.nz/

Also try and get things working for a small example first. This is just too big to begin with.

Anyway here is the corrected code:

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:1){
  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])
#    qplot(as.POSIXct(as.character(numbers[4*i+1]), format="%m/%d/%Y %H:%M"), numbers[[4*i+2]],  xlab="Date", ylab="Feet", main=numbers[4*i+2,1])
  } 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])
#    qplot(as.POSIXct(as.character(numbers[4*i-6]), format="%m/%d/%Y %H:%M"), numbers[[4*i-5]],  xlab="Date", ylab="Feet", main=numbers[4*i-5,1])
  } 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])
#    qplot(as.POSIXct(as.character(numbers[4*i-13]), format="%m/%d/%Y %H:%M"), numbers[[4*i-12]], xlab="Date", ylab="Feet", main=numbers[4*i-12,1])
  }
}

And here is the plot (I cut it down to one plot because they take a long time):

enter image description here

Mike Wise
  • 22,131
  • 8
  • 81
  • 104
  • Thank you so much for all of your help! I had worked for hours and couldn't tell if I was even heading in the right direction. And thank you for the link to that book. I'm sure it will go miles toward helping me know what I'm doing here. – Dr.Vini42 Feb 12 '16 at 12:50
  • Well, please mark it correct then please. I do this for the points as well as that I like helping people. You could consider upvoteing it sometime too, when you get to 15 points. – Mike Wise Feb 12 '16 at 14:14
  • Sorry about that. I wasn't sure how to mark a correct answer. This was my first post. And I had already upvoted your answer, I just don't yet have enough points for it to be visible. – Dr.Vini42 Feb 13 '16 at 15:10
  • I'm having trouble getting my plots to output after running the script as you have it. The script will run normally, but not plots will be displayed, even if I modify the script as you did to output a single plot. The only way I have found to produce the desired output is to wrap each qplot function with print(), but I cannot seem to get the full function to display all 21 plots, only individual ones. Am I missing something? – Dr.Vini42 Feb 15 '16 at 21:52
  • 1
    Are you using R-Studio? You can cycle through the plots and see them one after another in the plot window. Otherwise you might want to consider embedding them in an R-Markdown file and viewing them as an html or pdf output. – Mike Wise Feb 16 '16 at 01:15
  • 1
    Another option is to arrange them with arrange.grob and print them out as one, but that would be a lot of plots for that approach. – Mike Wise Feb 16 '16 at 01:15
  • Yes, I am R-Studio. When I run the code as you provided, I don't see any plots output. When I set the i value outside of the loop and then run a specific line of code with the function qplot(), then I can get a plot to be output like the one you posted. I created an R-Markdown file and copied the same code within ```{r, echo=FALSE} and ```, and I received a document with no plots. – Dr.Vini42 Feb 18 '16 at 02:01
  • I would suggest posting the issue as a new R-markdown question then. I am sure it is something small, but without seeing the code it is hard to figure out. Make sure it is reproducable, i.e. post the data like you did here. – Mike Wise Feb 18 '16 at 02:21
  • Okay, here it is: http://stackoverflow.com/questions/35471802/several-plots-of-large-time-series-not-displaying-in-r-markdown. Thanks again for all your help so far. – Dr.Vini42 Feb 18 '16 at 02:41