【问题标题】:Cut data and access groups to draw percentile lines剪切数据和访问组以绘制百分位线
【发布时间】:2013-06-12 12:58:03
【问题描述】:

我对 R 很陌生,所以请温柔一点。

我有一个包含时间戳和一些数据的数据集。 现在我想画一个图表:

  • 数据按例如分组60 分钟间隔和
  • 绘制了一些百分位线。

我想要一个图表,其中时间为 x 轴,间隙为 y 轴。 我想像 boxplot 之类的东西,但为了更好地概述 - 因为我有一个长测量 - 而不是盒子,我希望有连接线的线

  • 平均值,
  • 3 个百分位,
  • 97 个百分位和
  • 100 个百分位数

这是一个示例数据:

> head(B, 10)
                        times     gaps
1  2013-06-10 15:40:02.654168 1.426180
2  2013-06-10 15:40:18.936882 2.246462
3  2013-06-10 15:40:35.215668 3.227132
4  2013-06-10 15:40:48.328785 1.331284
5  2013-06-10 15:40:53.809485 1.294128
6  2013-06-10 15:41:04.027745 2.292671
7  2013-06-10 15:41:25.876519 1.293501
8  2013-06-10 15:41:42.929280 1.342166
9  2013-06-10 15:42:11.700626 3.203901
10 2013-06-10 15:42:23.059550 1.304467

我可以用cut来划分数据:

C <- table(cut(B, breaks="hour"))

C <- data.frame(cut(B, breaks="hour"))

但是我怎样才能从这个中画出图表呢?我不知道如何访问组的差距值。否则我可以

quantile(C$gaps, c(.03, .5, .97, 1))

提前感谢您的帮助 拉蒙

【问题讨论】:

    标签: r cut percentile


    【解决方案1】:

    问得好。我一直在拔头发,直到找到this,它描述了plyr 的一个有趣的“功能”。所以这个解决方案使用了 ggplot、plyr、reshape2——希望是对 R 的一个很好的介绍。如果你需要在几天内添加剪辑,你也可以通过在 ddply() 中添加一个变量来做到这一点。

    library(plyr)
    library(reshape2)
    library(ggplot2)
    Hs <- read.table(
      header=TRUE, text='
    dates times     gaps
    1  2013-06-10 15:40:02.654168 1.426180
    2  2013-06-10 15:40:18.936882 2.246462
    3  2013-06-10 15:40:35.215668 3.227132
    4  2013-06-10 15:40:48.328785 1.331284
    5  2013-06-10 15:40:53.809485 1.294128
    6  2013-06-10 15:41:04.027745 2.292671
    7  2013-06-10 16:41:25.876519 1.293501
    8  2013-06-10 16:41:42.929280 1.342166
    9  2013-06-10 16:42:11.700626 3.203901
    10 2013-06-10 16:42:23.059550 1.304467')
    Hs$dates <- paste(Hs$date, Hs$times, sep = " ")
    Hs$dates <- strptime(Hs$date, "%Y-%m-%d %H:%M:%S")
    class(Hs$dates) # "POSIXlt" "POSIXt" 
    Hs$h1 <- Hs$dates$hour
    Hs$dates <- as.POSIXct(strptime(Hs$date, "%Y-%m-%d %H:%M:%S"))
    class(Hs$dates) # "POSIXct" "POSIXt" 
    library(ggplot2)
    ggplot(Hs, aes(factor(h1), gaps)) + 
      geom_boxplot(fill="white", colour="darkgreen") # easy way!  Traditional boxplot.
    ggplot(Hs, aes(factor(h1), gaps)) + geom_boxplot() +
          stat_boxplot(coef = 1.7, fill="white", colour="darkgreen") 
    

    我不知道添加“coef = 1.7”是否适合您- 如果不继续通过汇总表进一步创建值

    cuts <- c(.03, .5, .97, 1)
    x <- ddply(Hs, .(h1), function (x)
    {summarise(x, y = quantile(x$gaps, cuts))})
    x$cuts <- cuts
    x <- dcast(x, h1 ~ cuts, value.var = "y")
    x.melt <- melt(x, id.vars = "h1")
    

    这里是您要求的线条加上另一个方框图,只是为了好玩。

    ggplot(x.melt, aes(x = h1, y = value, color = variable)) + geom_point(size = 5) + 
      geom_line() + scale_colour_brewer(palette="RdYlBu") + xlab("hours")
    ggplot(x, aes(factor(h1),  ymin = 0, lower = `0.03`, middle = `0.5`,
                         upper = `0.97`, ymax = `1`)) + 
             geom_boxplot(stat = "identity", fill="white", colour="darkgreen")
    

    希望这会有所帮助。

    【讨论】:

    • 还了解了 POSIXlt、class()、ddply()、summarise(),我的问题也得到了解答 :-) 非常感谢!
    猜你喜欢
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2019-10-02
    • 1970-01-01
    • 2017-01-19
    • 2018-05-10
    • 1970-01-01
    相关资源
    最近更新 更多