如何在gnuplot或R中绘制分组并发时间线?
我有这样一个数据集:如何在gnuplot或R中绘制分组并发时间线?,r,gnuplot,R,Gnuplot,我有这样一个数据集: xStart xEnd yStart yEnd a 100 200 70 90 b 40 120 60 200 我需要绘制一个按“a”和“b”分组的时间图,并显示“a”的事件从100开始,到200结束,而“a”的其他事件从70开始,到90结束。如何使用gnuplot或R实现这一点???#我使用的是您给出的示例数据。 # I'm using the example data you gave
xStart xEnd yStart yEnd
a 100 200 70 90
b 40 120 60 200
我需要绘制一个按“a”和“b”分组的时间图,并显示“a”的事件从100开始,到200结束,而“a”的其他事件从70开始,到90结束。如何使用gnuplot或R实现这一点???#我使用的是您给出的示例数据。
# 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
#但你会在csv文件或类似文件中读取。
对不起,我执行了你的代码,结果是一个两点图。是否需要除此代码之外的其他代码?