条形图:条形宽度基于时间 - R

时间:2015-11-05 20:42:59

标签: r graph plot bar-chart

有没有办法创建条形图,条形的宽度是相对于数据所代表的时间段?

df <-read.table(file="x.txt", header=T, sep="\t", dec=".", na="NA")

> df
            Date_Time           Date_End   data
1  3/23/2015 00:00:00  4/1/2015 23:59:59   0.00
2   4/2/2015 00:00:00 4/14/2015 23:59:59 -34.67
3  4/15/2015 00:00:00 4/20/2015 23:59:59  -4.44
4  4/21/2015 00:00:00 4/24/2015 23:59:59  14.45
5  4/25/2015 00:00:00  5/5/2015 23:59:59  -8.67
6   5/6/2015 00:00:00 5/12/2015 23:59:59 -86.68
7  5/13/2015 00:00:00 5/19/2015 23:59:59  -8.68
8  5/20/2015 00:00:00 5/26/2015 23:59:59 -24.41
9  5/27/2015 00:00:00  6/2/2015 23:59:59  20.46
10  6/3/2015 00:00:00  6/9/2015 23:59:59  31.07
11 6/10/2015 00:00:00 6/16/2015 23:59:59  58.45
12 6/17/2015 00:00:00 6/23/2015 23:59:59   0.62
13 6/24/2015 00:00:00 6/26/2015 23:59:59 -92.45

我希望将数据绘制为条形图,条形宽度从Date_TimeDate_End

1 个答案:

答案 0 :(得分:1)

width中有barplot个参数。在下面的代码中,我添加了一个时差列,它将是条的宽度。从开始到结束的时间差越长,条形越大。

df <-read.table(text="Row_name  Date_Time Date_End   data
1  3/23/2015-00:00:00  4/1/2015-23:59:59   0.00
2   4/2/2015-00:00:00 4/14/2015-23:59:59 -34.67
3  4/15/2015-00:00:00 4/20/2015-23:59:59  -4.44
4  4/21/2015-00:00:00 4/24/2015-23:59:59  14.45
5  4/25/2015-00:00:00  5/5/2015-23:59:59  -8.67
6   5/6/2015-00:00:00 5/12/2015-23:59:59 -86.68
7  5/13/2015-00:00:00 5/19/2015-23:59:59  -8.68
8  5/20/2015-00:00:00 5/26/2015-23:59:59 -24.41
9  5/27/2015-00:00:00  6/2/2015-23:59:59  20.46
10  6/3/2015-00:00:00  6/9/2015-23:59:59  31.07
11 6/10/2015-00:00:00 6/16/2015-23:59:59  58.45
12 6/17/2015-00:00:00 6/23/2015-23:59:59   0.62
13 6/24/2015-00:00:00 6/26/2015-23:59:59 -92.45", header=T,stringsAsFactors =F)

df[[2]] <- strptime(df[[2]], "%m/%d/%Y-%H:%M:%S")
df[[3]] <- strptime(df[[3]], "%m/%d/%Y-%H:%M:%S")
df$difference <-as.numeric(df[[3]]-df[[2]])

barplot(df$data, width=df$difference)

enter image description here