merging data sets

16 views
Skip to first unread message

Joe Grzymski

unread,
Sep 10, 2011, 3:40:52 PM9/10/11
to ggplot2
All-
The following amateur R code will make plots of some of the most important variables in any one of the files attached to this email.
These are simple plots of time and a measured or derived variable.

The only differences among the files is they each are from a different wavelength of excitation light.
Does anyone have a data formatting strategy for making a single
plot of time on x-axis and the measure variable of say column 10 from each file?
My hope to simplify analysis and making multiple plots was to create a data frame with time as the id.var
but I can't seem to figure out how to melt data when the files are of different sizes and time is not the same in each file (it only varies
slightly given that the instrument can't measure each wavelength simultaneously)

Thanks.
Joe

R code to make some graphs of the data in attached files:

# Clear all objects
rm(list=ls())
require(lubridate)
require(plyr)
library(ggplot2)
library(TTR)


string = "/Users/joeg/Desktop/testcolor2.pdf"
pdf(string, height = 11, width = 17)

pltList<-list()
pltList[]

# Create a data set using a file from merger.pl
rd=read.table('week243_249_color2.out',sep='\t',header=T)
attach(rd)
rd$TimeSec_fl=ISOdatetime(1970,1,1,0,0,0)+rd$TimeSec_fl
rd$TimeSec_fl=force_tz(rd$TimeSec_fl,"UTC")

vals=c(8,10,11,12,13,14,15,18,35)

for (i in vals)
{
    pltName<-paste('a',i,sep='')
    fl<-data.frame(rd$TimeSec_fl,var=rd[,i])
   
    p<- ggplot(fl,aes(x=fl$rd.TimeSec_fl,y=fl$var)) +geom_point(size=3) +xlab("Time")+ ylab(" ")+ opts(title=toString(colnames(rd)[i])) + scale_x_datetime(major='6 hours',limits=as.POSIXct(c("2011-09-01 20:00:00","2011-09-07 12:00:00")),format="%b %d\n%H:%M")

    
    print(p)
    pltList[[pltName]]=p
}

dev.off() 
 

three_colors.tar.gz

Dennis Murphy

unread,
Sep 10, 2011, 8:28:45 PM9/10/11
to joeg...@gmail.com, ggplot2
Hi:

The code below combines the three files and then creates a plot function that plots a variable x vs. time with three panels, one for each data set. My initial thought was to put them into one plot with a color aesthetic, but decided that individual panels might be better. You can always edit the plot function to get what you need. I tried it on a subset of the variables in the combined data frame and it seemed to work for me (Win7, R-2.13.1patched, ggplot 0.8.9, plyr 1.6).

I unpacked your tar.gz file in my R starting directory; each file started with 'week', so that's where we'll begin.

library('ggplot2')
# After the new version of ggplot2 comes out, you'll need to include this
# library('plyr')     

# Make a list of the files to read:
files <- list.files(pattern = '^week')
# Function to read a particular file
readfun <- function(x) read.table(x, header = TRUE, sep = '\t', 
                                     stringsAsFactors = FALSE)
# Apply readfun() to all files in the list and rbind them
colormrg <- do.call('rbind', lapply(files, readfun))

# Create two new variables: 
#    - type, to distinguish the different data sets
#    - DATETIME, to generate a POSIXct time variable
# We could use transform() or within() here, but let's
# use the mutate() function from plyr instead:
colormrg <- mutate(colormrg,
                type = rep(c('allcolor', 'color1', 'color2'),
                           sapply(jlist, nrow)),
                DATETIME = as.POSIXct(paste(DATE_fl, TIME_fl), 
                                                      format = '%m/%d/%Y %H:%M:%S'))

# Generate a vector of variable names to be plotted:
# (feel free to change this - I used all but the obvious exceptions)
nms <- names(colormrg)[-c(1:3, 23, 37:39)]

# Function to create the plot and export it to a pdf file
# argument is a quoted variable name
# aes_string allows one to insert character strings to represent arguments
# I used 12 hour gaps to avoid overplotting of date-time labels and used
# mo/xx instead of 'Mon xx' to save on space
# You may want to use breaks = in the scale_x_datetime() call to set up
# different labeling points.

plotfun <- function(var) {
  print(ggplot(colormrg, aes_string(x = 'DATETIME', y = var)) +
           geom_point() +
           labs(x = 'Time', y = "") +
           opts(title = var) +
           facet_wrap(~ type, ncol = 1) +
           scale_x_datetime(
                      major = '12 hours',
                      limits = c(as.POSIXct('2011-09-01 20:00:00'), 
                                  as.POSIXct('2011-09-07 12:00:00')),
                      format = '%m/%d\n%H:%M')
       )
  ggsave(paste(var, 'pdf', sep = '.'))
  invisible()
  }

# This is plyr's version of mapply(); the idea is to generate a plot
# for each variable in the vector nms. 
# The _ means don't return anything - the plot is the 'side effect' output
# Expect this to take a couple of minutes...
m_ply(nms, plotfun)

HTH,
Dennis

--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

dani

unread,
Sep 14, 2011, 3:21:54 AM9/14/11
to ggplot2
Thanks Dennis, based on your code I wrote a function to read my files
and create a data frame. I post it in case anyone is interested. I am
not a programmer so probably my code is not optimal.

I do psychophysics. My files are always:
-two letters to designate the subject
-the name of the experiment
-a number specifying the session in which I collected the data

read.files<-function(path='data/',subject='.',exp='.',session='.') {
subject<-gsub(',',replace='|',subject)
exp<-gsub(',',replace='|',exp)
session<-gsub(',',replace='|',session)
r.exp<-paste('^(',subject,').*','(',exp,').*','[',session,']',sep='')
names.files<-list.files(pattern=r.exp,path=path)
print(names.files)
readfun<-function(x) read.table(paste(path,x,sep=''), header = TRUE)
data<- do.call('rbind', lapply(names.files, readfun))
data
}

Examples:

> data<-read.files()
[1] "dlexpa1.txt" "dlexpa2.txt" "dlexpa3.txt" "dlexpb1.txt"
[5] "dlexpb2.txt" "snexpa1.txt" "snexpa2.txt" "snexpb1.txt"
[9] "snexpb2.txt" "waexpb2.txt"

> data<-read.files(subject='dl',exp='expa',session=2)
[1] "dlexpa2.txt"

> data<-read.files(subject='dl')
[1] "dlexpa1.txt" "dlexpa2.txt" "dlexpa3.txt" "dlexpb1.txt"
[5] "dlexpb2.txt"

> data<-read.files(session='2')
[1] "dlexpa2.txt" "dlexpb2.txt" "snexpa2.txt" "snexpb2.txt"
[5] "waexpb2.txt"

> data<-read.files(subject='dl,wa')
[1] "dlexpa1.txt" "dlexpa2.txt" "dlexpa3.txt" "dlexpb1.txt"
[5] "dlexpb2.txt" "waexpb2.txt"

> data<-read.files(subject='dl',session='2-3')
[1] "dlexpa2.txt" "dlexpa3.txt" "dlexpb2.txt"
> > *Joseph J Grzymski* *Ph.D***
> > Tel: 775-453-6373 | Fax: 775-673-7485
> > joegj...@gmail.com | antarctica.dri.edu
> > [image: Facebook]<http://www.facebook.com/pages/DRI-Antarctica/248076525218935> [image:
> > Twitter] <http://www.twitter.com/miAntarctica>
> > Signature powered by
> > <http://r1.wisestamp.com/r/landing?u=2d67bc3a0c4ca423&v=2.6.1.0&t=1315...>
> > WiseStamp<http://r1.wisestamp.com/r/landing?u=2d67bc3a0c4ca423&v=2.6.1.0&t=1315...>
Reply all
Reply to author
Forward
0 new messages