How to plot a stair steps function with ggplot?

I have the graph like this:
enter image description here

R code for generating it is:

DF <- data.frame(date = as.Date(runif(100, 0, 800),origin="2005-01-01"), 
                 outcome = rbinom(100, 1, 0.1))
DF <- DF[order(DF$DateVariable),] #Sort by date
DF$x <- seq(length=nrow(DF)) #Add case numbers (in order, since sorted)
DF$y <- cumsum(DF$outcome)
library(ggplot2)
ggplot(DF, aes(x,y)) + geom_path() + #Ploting
scale_y_continuous(name= "Number of failures") +
scale_x_continuous(name= "Operations performed")

I want something like this:
enter image description here

The difference is in the form of steps in case of failure (I need rectangular).

My questions are:

  • How to achieve this with ggplot2?
  • Is there any better option for visualizing failure rate over time/iterations performed?
  • What variant is easier to understand: this or that or, maybe something different?

Answer

As noted by @chl the answer is simply using geom_step() instead of geom_path() in the example above.

Result (the plot has different data):

enter image description here

Attribution
Source : Link , Question Author : Yuriy Petrovskiy , Answer Author : Community

Leave a Comment