ggplot2和带负值的堆积条形图 [英] ggplot2 and a Stacked Bar Chart with Negative Values
问题描述
分部年营业收入
1 A 2012 11460
2 B 2012 7431
3 C 2012 -8121
4 D 2012 15719
5 E 2012 364
6 A 2011 12211
7 B 2011 6290
8 C 2011 - 2657
9 D 2011 14657
10 E 2011 1257
11 A 2010 12895
12 B 2010 5381
13 C 2010 -2408
14 D 2010 11849
15 E 2010 517
如何在ggplot2中完成堆叠条形图,负值?这里是我使用的基本图表顺序:
$ g $ p $ g $ gpp(income_m,aes(x = Year,y = OperatingIncome,fill =分区))+ geom_bar()+
+ scale_fill_brewer(type =seq,palette = 1)
这会返回一个错误:
警告消息:当ymin!= 0时堆叠不正确定义
而不是预期的结果 - 负值显示在X轴下方 - 它们不在堆积的条形图中。是否有一种方法可以生成包含正数和负数所有值的图表?解析方案
更新:从 ggplot2 2.2.0,自动处理负值堆叠,无需为正值和负值创建单独的图层。
如果我明白你在找什么,诀窍是把两个正面和负面的数据放在单独的图层中,并使用 stat =identity
:
dat < - read.table(text =分部年营业收入
1 A 2012 11460
2 B 2012 7431
3 C 2012 -8121
4 D 2012 15719
5 E 2012 364
6 A 2011 12211
7 B 2011 6290
8 C 2011 -2657
9 D 2011 14657
10 E 2011 1257
11 A 2010 12895
12 B 2010 538 1
13 C 2010 -2408
14 D 2010 11849
15 E 2010 517,header = TRUE,sep =,row.names = 1)
dat1< - subset(dat,OperatingIncome> = 0)
dat2< - subset(dat,OperatingIncome< 0)
ggplot()+
geom_bar(data = dat1,aes(x = Year,y = OperatingIncome,fill = Division),stat =identity)+
geom_bar(data = dat2,aes(x = Year,y = OperatingIncome,fill = Division),stat =identity)+
scale_fill_brewer(type =seq,palette = 1)
Given the following dataset:
Division Year OperatingIncome
1 A 2012 11460
2 B 2012 7431
3 C 2012 -8121
4 D 2012 15719
5 E 2012 364
6 A 2011 12211
7 B 2011 6290
8 C 2011 -2657
9 D 2011 14657
10 E 2011 1257
11 A 2010 12895
12 B 2010 5381
13 C 2010 -2408
14 D 2010 11849
15 E 2010 517
How do I complete a stacked bar chart in ggplot2 that includes the negative values? Here's the basic chart sequence I'm using:
ggplot(income_m, aes(x=Year, y=OperatingIncome, fill=Division)) + geom_bar() +
+ scale_fill_brewer(type = "seq", palette = 1)
This returns an error:
Warning message: Stacking not well defined when ymin != 0
And instead of the expected result - negative values being displayed below the X axis - they are simply not present in the stacked bar chart. Is there a way to produce the chart with all values, both positive and negative, accounted for?
Update: As of ggplot2 2.2.0, stacking for negative values is handled automatically, without having to create separate layers for the positive and negative values.
If I understand what you're looking for, the trick is to put the two positive and negative data in separate layers, and also to use stat = "identity"
:
dat <- read.table(text = " Division Year OperatingIncome
1 A 2012 11460
2 B 2012 7431
3 C 2012 -8121
4 D 2012 15719
5 E 2012 364
6 A 2011 12211
7 B 2011 6290
8 C 2011 -2657
9 D 2011 14657
10 E 2011 1257
11 A 2010 12895
12 B 2010 5381
13 C 2010 -2408
14 D 2010 11849
15 E 2010 517",header = TRUE,sep = "",row.names = 1)
dat1 <- subset(dat,OperatingIncome >= 0)
dat2 <- subset(dat,OperatingIncome < 0)
ggplot() +
geom_bar(data = dat1, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") +
geom_bar(data = dat2, aes(x=Year, y=OperatingIncome, fill=Division),stat = "identity") +
scale_fill_brewer(type = "seq", palette = 1)
这篇关于ggplot2和带负值的堆积条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!