2012-09-15 40 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

对不起,我执行了你的代码,结果是有两点的情节。除了这段代码还有其他必要的东西吗 –