stacked plot in R

January 18, 2015
By

(This article was first published on Ensemble Blogging, and kindly contributed to R-bloggers)

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:

To leave a comment for the author, please follow the link and comment on their blog: Ensemble Blogging.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Sponsors

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)