Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.36 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. library(lattice)
  2.  
  3. day <- c("110513")
  4. directions <- c("0", "180", "L45", "L90", "L135", "R45", "R90", "R135")
  5.  
  6. direction.n <- length(directions)
  7. wave.names <- paste("w.", directions, sep="")
  8.  
  9. for (d in directions) {
  10.   x <- read.csv(paste(day, "-1-a-", d, "_proc-wave.csv", sep=""),
  11.                 skip=10, header=T)
  12.   assign(paste("w.", d, sep=""), x)
  13. }
  14.  
  15. roi.n <- dim(x)[2] - 2 # minus time column and extra X column, it depends on csv files
  16.  
  17. my.reshape <- function(){
  18.   w.ret <- data.frame()
  19.   for (i in wave.names) {
  20.     w.n <- get(i)[, -dim(x)[2]] # remove the extra X column
  21.     for (j in 1:roi.n) {
  22.       w.ret <- rbind(w.ret,
  23.                      data.frame(time=w.n$time,
  24.                                 value=w.n[,j+1],
  25.                                 direction=i,
  26.                                 roi=j))
  27.     }
  28.   }  
  29.   return(w.ret)
  30. }
  31.  
  32. plot.for.roi <- function(w=w.ret){
  33.   roi.plot <- xyplot(-value ~ time | roi, group=direction, w, type="l")
  34.   pdf(file="plot.for.roi.pdf")
  35.   print(roi.plot)
  36.   dev.off()
  37.   print(roi.plot)
  38. }
  39.  
  40. plot.for.direction <- function(w=w.ret){
  41.   direction.plot <- xyplot(-value ~ time | direction, group=roi, w, type="l")
  42.   pdf(file="plot.for.direction.pdf")
  43.   print(direction.plot)
  44.   dev.off()
  45.   print(direction.plot)
  46. }
  47.  
  48. my.f <- function(){
  49.   source("wave.plot.r")
  50. }
  51.  
  52. ## USAGE
  53. ## > source("wave.plot.r")
  54. ## > ls()
  55. ##  [1] "d"                  "day"                "direction.n"      
  56. ##  [4] "directions"         "my.f"               "my.reshape"        
  57. ##  [7] "plot.for.direction" "plot.for.roi"       "roi.n"            
  58. ## [10] "w.0"                "w.180"              "w.L135"            
  59. ## [13] "w.L45"              "w.L90"              "w.R135"            
  60. ## [16] "w.R45"              "w.R90"              "wave.names"        
  61. ## [19] "x"                
  62. ## > w <- my.reshape()
  63. ## > head(w)
  64. ##   time     value direction roi
  65. ## 1    0  0.848247       w.0   1
  66. ## 2   30  0.848247       w.0   1
  67. ## 3   60  1.853140       w.0   1
  68. ## 4   90 -0.403139       w.0   1
  69. ## 5  120 -0.262454       w.0   1
  70. ## 6  150 -0.573379       w.0   1
  71. ## > str(w)
  72. ## 'data.frame':        3960 obs. of  4 variables:
  73. ##  $ time     : num  0 30 60 90 120 150 180 210 240 270 ...
  74. ##  $ value    : num  0.848 0.848 1.853 -0.403 -0.262 ...
  75. ##  $ direction: Factor w/ 8 levels "w.0","w.180",..: 1 1 1 1 1 1 1 1 1 1 ...
  76. ##  $ roi      : int  1 1 1 1 1 1 1 1 1 1 ...
  77. ## > plot.for.roi(w)
  78. ## > plot.for.direction(w)