Search code examples
rplotggplot2cumulative-line-chart

Plotting a straight line to cumulative occurance graph


I have a data of machine failures with one column which defines time between failures (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))

I want to plot a cumulative occurrence graph. I can do that using

library(ggplot2)
ggplot(mydf, aes(x = tbf)) + stat_ecdf()

which creates a plot as shown belowenter image description here

However, I want a straight line fit into this plot. I do not want uneven line but a straight line fitted to it. I tried

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)

However, I get the figure the one below:: enter image description here

My requirement is to get a figure something like::

enter image description here

Where am I getting it wrong? Any help will be appreciated.


Solution

  • It looks like you want to make a plot with the same variables on both axes? From this line: plot(mydf$cumm_time, mydf$cumm_time), there is either a typo or you are plotting a plot with the dependent variable of your data (cumsum(tbf)) on both the X and Y axis.

    I will assume that you meant to type plot(mydf$cumm_fmode, mydf$cumm_time).

    If you do this, then the rest of your code is okay.

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

    gives

    changed x to mydf$cumm_fmode