Quick help creating a complex histogram (ggplot2)

I have the following data frame:

> DF
Year Metric MWh
2003 Demand 498343
2004 Demand 1250904
2005 Demand 1665176
2006 Demand 2317643
2007 Demand 2455311
2008 Demand 3557987
2009 Demand 4268125
2010 Demand 5403704
2011 Demand 6596158
2012 Demand 7814387
2013 Demand 9008863
2014 Demand 10291085
2015 Demand 11796549
2003 Actual 159677
2004 Actual 192748
2005 Actual 248844
2006 Actual 372661
2007 Actual 705656
2008 Actual 838721
2009 Actual 1188242
2010 Actual 1708979
2011 Actual 0
2012 Actual 0
2013 Actual 0
2014 Actual 0
2015 Actual 0
2003 High 0
2004 High 0
2005 High 0
2006 High 0
2007 High 0
2008 High 0
2009 High 0
2010 High 0
2011 High 3631730
2012 High 5729024
2013 High 6741785
2014 High 9342798
2015 High 11094798
2003 Low 0
2004 Low 0
2005 Low 0
2006 Low 0
2007 Low 0
2008 Low 0
2009 Low 0
2010 Low 0
2011 Low 1637220
2012 Low 1850615
2013 Low 2064011
2014 Low 2277406
2015 Low 2490801

I want to create a very simple glass histogram with:
- x axis: year
- y axis: MWh
- 1 stack with the request, high, low and actual ("Metric"), in the indicated order, stacked with each other (unlike on-top). So far, I have managed to figure out how to do this with values โ€‹โ€‹that are in the TOLE from each other:

DF$'Metric <- factor(DF$'Metric',levels=c("Demand","High","Low","Actual"))

qplot(x=Year,data=DF,geom="bar",weight=MWh,fill=Metric)
#OR
ggplot(DF,aes(x=factor(Year),y=MWh,fill=factor(Metric))) + geom_bar(position="stack")

Essentially, what I'm looking for is one bar per year, where the Demand value is the highest and the lower values โ€‹โ€‹(in the above order) are combined. I believe I need to use position="fill" somewhere, but I'm not sure where to put it. Basically, I am trying to show that demand will grow steadily, while supply (actual and projected low growth versus projected high growth) cannot satisfy it in a very simple and compact schedule. If this is not possible, perhaps it would be better to simply group them side by side?

Any help is much appreciated !! Thanks!!

+6
r ggplot2
source share
2 answers

I'm not sure what you really would like to build, but from position="fill" I get the impression that you want to build the relative proportions of the Metric per year. This can be done easily using the following steps.

Data loading:

 DF <- dput(DF) structure(list(Year = c(2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L, 2003L, 2004L, 2005L, 2006L, 2007L, 2008L, 2009L, 2010L, 2011L, 2012L, 2013L, 2014L, 2015L), Metric = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("Demand", "High", "Low", "Actual"), class = "factor"), MWh = c(498343L, 1250904L, 1665176L, 2317643L, 2455311L, 3557987L, 4268125L, 5403704L, 6596158L, 7814387L, 9008863L, 10291085L, 11796549L, 159677L, 192748L, 248844L, 372661L, 705656L, 838721L, 1188242L, 1708979L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 3631730L, 5729024L, 6741785L, 9342798L, 11094798L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1637220L, 1850615L, 2064011L, 2277406L, 2490801L)), .Names = c("Year", "Metric", "MWh"), row.names = c(NA, -52L), class = "data.frame") 

And build stacked stripes with the same height (with percentages):

 ggplot(DF,aes(x=factor(Year),y=MWh,fill=factor(Metric))) + geom_bar(position="fill") 

enter image description here

I could have misunderstood what you want to build, as well as build two different graphs on the same picture with the grid viewports. I recommend looking at the gridextra package, especially for arrange .

+17
source share

position="identity" will not move the bars at all (unlike standard styling), so they will be superimposed. You should keep order of factor levels, because in this way the bars can be hidden one after another.

  ggplot(DF,aes(x=factor(Year),y=MWh,fill=factor(Metric))) + geom_bar(position="identity") 
+4
source share

All Articles