2012-09-15 42 views
0

我有一個數據集,如:如何在gnuplot或R中繪製分組併發時間線?

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

我需要繪製由「A」和「B」,並分組時間曲線圖顯示,事件在100開始,200爲「一」結束,其他事件從70開始,到90結束爲「a」。我怎樣才能做到這一點與gnuplot或R?

回答

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 
+0

對不起,我執行了你的代碼,結果是有兩點的情節。除了這段代碼還有其他必要的東西嗎 –