rdategraphrelative-date

R heights graph - relative to birth date


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:


Solution

  • 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()