2
votes

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

1

1 Answers

1
votes

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