将直线绘制为累积出现图

时间:2016-12-11 23:35:31

标签: r plot ggplot2 cumulative-line-chart

我有一个机器故障数据,其中一列定义了故障间隔时间(tbf)

structure(list(tbf = c(2441, 2934, 4285, 2285, 4027, 2419, 2437, 2519, 3294, 2858, 3023, 2567, 3112, 2283, 3068, 2215, 3915, 2354.290323, 2477, 2258, 2742.5, 5198, 2837, 3282, 2474, 2883, 3837, 5054, 4874, 3559.5, 2783, 4246, 2602)), .Names = "tbf", class = "data.frame", row.names = c(NA, -33L))

我想绘制累积发生图。我可以用

做到这一点
library(ggplot2)
ggplot(mydf, aes(x = tbf)) + stat_ecdf()

创建如下enter image description here

所示的图

但是,我希望直线适合这个情节。我不想要不均匀的线条,而是需要一根直线。我试过了

library(dplyr)
# add cumulative time and failures
mydf <-  mydf %>% mutate(cumm_time = cumsum(tbf), cumm_fmode = row_number())

# fit linear regression
fit <- lm(cumm_time ~ cumm_fmode, data = mydf)
# plot points
plot(mydf$cumm_time, mydf$cumm_time)
# plot straight line
abline(fit)

然而,我得到的数字如下: enter image description here

我的要求是获得类似::

的数字

enter image description here

我在哪里弄错了?任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

看起来你想在两个轴上制作一个带有相同变量的图?从这一行:plot(mydf$cumm_time, mydf$cumm_time),有一个拼写错误,或者你正在用X和Y轴上的数据(cumsum(tbf))的因变量绘制一个情节。

我会假设您打算输入plot(mydf$cumm_fmode, mydf$cumm_time)

如果你这样做,那么剩下的代码就可以了。

plot(mydf$cumm_fmode, mydf$cumm_time)
abline(fit)

给出

changed x to mydf$cumm_fmode