views:

60

answers:

1

I have a group of people, with a space-separated text file for each person. In these files, the right value indicates the height of that person in cm and the left value indicates the date in %d/%m/%Y format:

09/05/1992 0
17/03/1993 50
02/08/1994 65.5
03/12/1995 72

A height of 0 marks the birth date of the person.

This R script draws a graph of the heights of John and Amy and outputs it to a PDF:

pdf("Heights.pdf")

john <- read.table("John",sep="")
names(john) <- c("time","height")
jt <- strptime(john$time, "%d/%m/%Y")
jh <- john$height

amy <- read.table("Amy",sep="")
names(amy) <- c("time","height")
at <- strptime(amy$time, "%d/%m/%Y")
ah <- amy$height

plot(jt,jh,type="b",pch=20,col="red",
xlab="Date",ylab="Height",
ylim=c(min(jh,ah),max(jh,ah)))
points(at,ah,type="b",pch=20,col="green")
title("Heights")

How can I extend this script to:

  • Graph all files in the current directory ending with .heights?
  • Make the graph relative to each person's birth date?
+2  A: 

I think this will do it. Plotting with ggplot is the easiest way to go. You can pretty up the plot from there.

# Get all the files ending with .heights
filelist <- list.files(pattern = "\\.heights")

# Get all the data. Put into a single data.frame
# Assuming that you don't have thousands of
# files/measurements, rbind()ing shouldn't be too slow. 
df <- data.frame(person = character(),
                 dates = character(),
                 height = numeric())

# Iterate through, collecting the data into a data.frame
for (fname in filelist){
  x <- read.table(fname, sep="", as.is = TRUE)
  person <- gsub("\\.heights", "", fname)
  names(x) <- c("dates", "height")
  df <- rbind(df, data.frame(person = rep(person, times = nrow(x)),
                             dates = x$dates, 
                             height = x$height))
}

# Convert dates to POSIXct
df$dates <- strptime(as.character(df$dates), "%d/%m/%Y")
df$dates <- as.POSIXct(df$dates)

# Plot with qplot
require(ggplot2)
pdf("Heights.pdf")
qplot(dates, height, data = df, color = person)
dev.off()

# Plot with base graphics
pdf("Heights_2.pdf")
plot(df$dates, df$height, col = as.numeric(df$person))
dev.off()
Kevin
`there is no package called 'ggplot2'` - is there any way of doing this using only built-in commands?
Josh
I added the code for plotting with base graphics. It's more labor intensive to add a legend (if you want one), change colors and symbols, etc., whereas most of these things aare handled automatically by qplot. If you are planning to do extensive plotting, then it is probably worth the time investment to learn ggplot. You can install it with install.packages("ggplot2", dep = TRUE). Excellent online help is at http://had.co.nz/ggplot2/
Kevin
That code plots all the files as a single line, rather than one line for each file. I tried editing the code, but I just don't have enough experience with **R**.
Josh