0

我有一群人,每个人都有一个以空格分隔的文本文件。在这些文件中,右边的值表示该人的身高,cm左边的值表示%d/%m/%Y格式的日期:

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

一个高度0标志着这个人的出生日期。

这个R脚本绘制了 John 和 Amy 的身高图,并将其输出到 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")

如何将此脚本扩展到:

  • .heights绘制当前目录中以?结尾的所有文件
  • 制作相对于每个人的出生日期的图表?
4

1 回答 1

2

我认为这会做到。用 ggplot 绘图是最简单的方法。你可以从那里美化情节。

# 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()
于 2010-08-02T00:36:49.287 回答