快速帮助创建堆积条形图(ggplot2) [英] Quick help creating a stacked bar chart (ggplot2)
问题描述
我有以下数据框:
> DF
Year Metric MWh
2003需求498343
2004年需求1250904
2005需求1665176
2006需求2317643
2007需求2455311
2008需求3557987
2009需求4268125
2010需求5403704
2011需求6596158
2012年需求7814387
2013需求9008863
2014需求10291085
2015需求11796549
2003实际159677
2004年实际值192748
2005实际值248844
< br>
2006实际372661
2007实际705656
2008实际值838721
2009实际值1188242
2010 Actual 1708979
2011实际0
2012实际0
2013实际0
2014实际0
2015 Actual 0
2003高0
2004高0
2005高0
2006高0
2007高0
2008高0
< br>
2009高0
2010高0
2011高3631730
2012高5729024
2013高6741785
2014高9342798
2015高11094798
2003低0
2004低0
2005低0
2006低0
2007低0
2008低0
2009低0
2010低0
2011低1637220
2012低1850615
2013低2064011
2014低2277406
2015低2490801
我想创建一个非常简单的堆积条形图:
- x-axis:Year
- y轴:MWh
- 按顺序将需求,高,低和实际值('度量标准')堆叠在一起(相对于顶部)。到目前为止,我只能设法弄清楚如何使用堆叠在彼此的ON TOP值:/ b
$ b
DF $'公制< - 因子(DF $'指标',水平= c(需求,高,低,实际))
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)
我期待的是每年一个单栏,其中需求值最高,而较低的值(按上述顺序)叠加在一起。我相信我必须在某处使用 position =fill
,但我不知道该把它放在哪里。基本上,我试图说明的是,需求将稳步增长,而供应(实际与预期的低增长与预期的高增长)无法以非常简单,紧凑的图表达到。如果这是不可能的,也许最好是将它们并排放在一起?
任何帮助都非常感谢!谢谢!!
我不确定你真正想要的情节,但是从 position =fill
我有一个想要绘制每年 Metric 的相对比例的印象。这可以通过以下方式轻松完成。
加载数据:
DF <-dput(DF)
结构(列表(年份= 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, b $ b 2010L,2011L,2012L,2013L,2014L,2015L,2003L,2004L,2005L,
2006L,2007L,2008L,2009L,2010L,2011L,2012L,2013L,2014L,
2015L),度量=结构(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,3L),标签= c(需求,
高,低 =因子),MWh = c(498343L,
1250904L,1665176L,2317643L,2455311L,3557987L,4268125L,5403704L,
6596158L,7814387L,9008863L, 10291085L,11796549L,159677L,
192748L,248844L,372661L,705656L,838721L,1188242L,1708979L,0b, 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)
$ c
并绘制相同高度的堆积条(以百分比表示):
<$ p $ b $ ggplot(DF,aes(x = factor(Year),y = MWh,fill = factor(Metric)))+
geom_bar(position =fill)
我可能会误解你想要绘制的内容,并且在指定网格视口的同一图片中绘制两个不同的图也是可能的。我建议您查看 gridextra 软件包,特别是 arrange
。
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")
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
.
这篇关于快速帮助创建堆积条形图(ggplot2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!