【问题标题】:Bar plot: Bar width based on time - R条形图:基于时间的条形宽度 - R
【发布时间】:2016-02-06 20:51:24
【问题描述】:

有没有办法创建条形图,其中条形的宽度与数据所代表的时间段相关?

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

【问题讨论】:

    标签: r graph plot bar-chart


    【解决方案1】:

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

    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)
    

    【讨论】:

    • 谢谢!删除条之间空格的快速方法?
    • barplot(df$data, width=df$difference,space=0) 会起作用。
    猜你喜欢
    • 1970-01-01
    • 2013-03-22
    • 2018-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多