0

I have a dataset like:

    xStart  xEnd    yStart  yEnd
a   100     200     70      90  
b   40      120     60      200

I need to plot a time graph grouped by "a" and "b" and show that a event starts at 100 and ends at 200 for "a", and other event starts at 70 and ends at 90 for "a". How can I do this with gnuplot or R??

4

1 回答 1

0
# I'm using the example data you gave.
# but you'd be reading in a csv file or something similar.

df <- read.table(textConnection("    xStart  xEnd    yStart  yEnd
a   100     200     70      90  
b   40      120     60      200"), header = T)

# add the row names as a column
df$cols <- rownames(df)

> df
  xStart xEnd yStart yEnd cols
a    100  200     70   90    a
b     40  120     60  200    b

library(plyr)
# instead of gnuplot (which I don't use), I'm using ggplot2
# see http://had.co.nz for more info about the package.
# below, the function ddply (in plyr) splits the data by cols (a or b)
# then plots those data and returns it to a list named plots.
# the length of plots will be equal to the number of unique groups in your data
library(ggplot2)
# change the plot type below to whatever you like.
plots <- dlply(df, .(cols), function(x){
    data <- data.frame(x1 = as.numeric(x[, 1:2]), y1 = as.numeric(x[, 3:4]))
    ggplot(data, aes(x1, y1)) + geom_point() + ggtitle(unique(x$cols))
    })

plots[1]
# to see the first plot
plots[2]
# to see the second one
length(plots) # to see how many plots were generated
于 2012-09-15T22:41:48.683 回答