开发者

Quick help creating a stacked bar chart (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 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)

#OR

ggplot(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")

Quick help creating a stacked bar chart (ggplot2)

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")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜