marc
marc

Reputation: 2197

Using geom_bar() to stack values that add up?

I have a data frame in R which looks like:

        Month numFlights numDelays  onTime
1  2000-01-01    7520584   1299743 6220841
2  2000-02-01    6949127   1223397 5725730
3  2000-03-01    7808080   1390796 6417284
4  2000-04-01    7534239   1178425 6355814
5  2000-05-01    7720013   1236135 6483878
6  2000-06-01    7727349   1615408 6111941
7  2000-07-01    8000680   1652590 6348090
8  2000-08-01    7990440   1481498 6508942
9  2000-09-01    6811541    875381 5936160
10 2000-10-01    7026150   1046749 5979401
11 2000-11-01    6689783    987175 5702608
12 2000-12-01    6895454   1535196 5360258

What I'm looking to do, is create a bar chart where for each month (on the x axis), the bar reaches the numbers of delayed flights + number of flight on time. I tried figuring out how to do that using the example

qplot(factor(cyl), data=mtcars, geom="bar", fill=factor(gear))

but my data isn't formatted the same way as mtcars. And I can't just use an alpha value, because I want to eventually add cancelled flights.

I know that some questions are very similar to this one, but not similar enough for me to work it out (this question is almost the same but I don't know how to manipulate the facet_grid yet.) Any ideas?

Thanks!

Upvotes: 0

Views: 192

Answers (1)

agstudy
agstudy

Reputation: 121588

You can do this for example:

library(reshape2)
dat.m <- melt(dat,id.vars='Month',measure.vars=c('numDelays','onTime'))

library(ggplot2)
library(scales)

ggplot(dat.m) +
    geom_bar(aes(Month,value,fill=variable))+
    scale_y_continuous(labels = comma) +
    coord_flip() + 
    theme_bw()

enter image description here

Upvotes: 1

Related Questions