stacked plot in R

Sunday, January 18, 2015
Consider the following example: there is a three-stage truck maintenance pipeline. Initially, when a Truck comes to the maintenance service, it is added to the first stage and its status in the pipeline is set to "New". When the technicians start working on it, for diagnosis the problem and removing the issue, its status is changed to "in progress". When the truck is released from maintenance service its status is change to "released". Note that a track status can convert from "New" to "released", if the technicians believe there is nothing wrong with the system and it was just a false alarm.

The pipeline manager would like to know that for a set of trucks coming for maintenance how their statuses change over time. This is called for a stacked plot where the number of trucks in different stages are plotted in stack format. Stack plots are fairly easy to create in R and here is how:

Suppose we have the following data frame:

In this data frame, level is the status of the maintenance, date is the date of status update, and data is the number of trucks in that date with that status.

In order to plot the data in stacked format, we use geom_area in ggplot2 and set the position field to stacked. 

p <- ggplot(maintanance_df, aes( day, data))
p + geom_area(aes(colour = level, fill= level), position = 'stack') +
  scale_x_discrete(labels = xlabel) + 
  xlab("date") +
  ylab("# of trucks") +
  ggtitle("Status of trucks in maintenance pipeline")
This would result in the following stacked plot:


Post a Comment


Favorite Quotes

"I have never thought of writing for reputation and honor. What I have in my heart must out; that is the reason why I compose." --Beethoven

"All models are wrong, but some are useful." --George Box

Copyright © 2015 • Ensemble Blogging