I have the following data frame:
> DFYear Metric MWh2003 Demand 4983432004 Demand 12509042005 Demand 16651762006 Demand 23176432007 Demand 24553112008 Demand 35579872009 Demand 42681252010 Demand 54037042011 Demand 65961582012 Demand 78143872013 Demand 90088632014 Demand 102910852015 Demand 117965492003 Actual 1596772004 Actual 1927482005 Actual 2488442006 Actual 3726612007 Actual 7056562008 Actual 8387212009 Actual 11882422010 Actual 17089792011 Actual 02012 Actual 02013 Actual 02014 Actual 02015 Actual 02003 High 02004 High 02005 High 02006 High 02007 High 02008 High 02009 High 02010 High 02011 High 36317302012 High 57290242013 High 67417852014 High 93427982015 High 110947982003 Low 02004 Low 02005 Low 02006 Low 02007 Low 02008 Low 02009 Low 02010 Low 02011 Low 16372202012 Low 18506152013 Low 20640112014 Low 22774062015开发者_如何学编程 Low 2490801
I want to create a very simple stacked bar chart with:
-- x-axis: Year -- y-axis: MWh -- 1 stack with Demand, High, Low, and Actual ('Metric'), in that order, stacked OVER one another (as opposed to on-top). So far, I've only managed to figure out how to do it with the values stacked ON TOP of each other:
DF$'Metric <- factor(DF$'Metric',levels=c("Demand","High","Low","Actual"))
qplot(x=Year,data=DF,geom="bar",weight=MWh,fill=Metric)#ORggplot(DF,aes(x=factor(Year),y=MWh,fill=factor(Metric))) + geom_bar(position="stack")
Essentially, what I'm looking for is a single bar per year where the "Demand" value is the highest, and the lower values (in the order above) are stacked over. I believe I have to use a position="fill" somewhere, but I'm not sure where to put it. Basically, what I am trying to show is that Demand will be steadily rising, while Supply (Actual vs. projected Low growth vs. projected High growth) has been unable to meet it in a very simple, compact graphic. If this is not possible, perhaps it would be better to simply group them side-by-side?
Any help is much appreciated!! Thanks!!
I am not sure about what you really would like to plot, but from the position="fill" I have an impression you want to plot the relative proportions of Metric per year. This can be done easily via the followings.
Loading data:
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 plot the stacked bars with the same height (with percentages):
ggplot(DF,aes(x=factor(Year),y=MWh,fill=factor(Metric))) +
geom_bar(position="fill")

I might misunderstood what you want to plot, and plotting two distinct graph in the same picture also possible with specifying grid viewports. I recommend looking at gridextra package, especially for arrange.
position="identity" will not move the bars at all (as opposed to the default stacking), so they will be overlayed. You have to watch out for the ordering of the factor levels though, because this way the bars can hide behind each other.
ggplot(DF,aes(x=factor(Year),y=MWh,fill=factor(Metric))) + geom_bar(position="identity")
加载中,请稍侯......
精彩评论