2013-08-06 394 views
1

继回答我former question我还有一个问题认识:GGPLOT2统计=“身份”和堆叠颜色柱状图中给出了“条纹”条形图

如何,没有重塑数据,绘制堆叠条形图不同的颜色取决于另一个类别,同时使用stats =“identity”来总结每个堆叠区域的值?

统计信息标识可以很好地总结值,但对于非堆栈列。在堆叠的列中,堆叠以某种方式“倍增”或“条带化”,如下图所示。

一些数据样本:

element <- rep("apples", 15) 
qty <- c(2, 1, 4, 3, 6, 2, 1, 4, 3, 6, 2, 1, 4, 3, 6) 
category1 <- c("Red", "Green", "Red", "Green", "Yellow") 
category2 <- c("small","big","big","small","small") 
d <- data.frame(element=element, qty=qty, category1=category1, category2=category2) 

其中给出该表:

id element qty category1 category2 
1 apples 2  Red  small 
2 apples 1  Green  big 
3 apples 4  Red  big 
4 apples 3  Green  small 
5 apples 6 Yellow  small 
6 apples 2  Red  small 
7 apples 1  Green  big 
8 apples 4  Red  big 
9 apples 3  Green  small 
10 apples 6 Yellow  small 
11 apples 2  Red  small 
12 apples 1  Green  big 
13 apples 4  Red  big 
14 apples 3  Green  small 
15 apples 6 Yellow  small 

然后:
ggplot(d,AES(X =类别1,Y =数量,填写=类别2)) + geom_bar(stat =“identity”)

但是图形有点混乱:颜色没有组合在一起!

ggplot graph is striped 为什么会有这种行为?

是否还有一个选项可以在不改变数据的情况下对颜色进行正确分组?

+0

为什么重塑出来的问题? 'stat = identity'只会画出你给它的东西。在你的情况下,一个凌乱的数据集。你必须手动处理表格才能给你想要的结果(我不明白它应该是什么样子)。 –

+1

我尝试将最轻的代码嵌入到php代码插件中以包含在网站中(tikiwiki CMS) - 因此非R-wise用户仍可以从其数据中自定义一些统计信息。另外,因为在一个页面中可以有很多插件,所以我希望保持服务器用于显示统计数据低......这意味着重塑不是真的没有问题:) –

回答

1

我是用了一段时间这个解决方案,但它发生在我的大型数据库(60 000个)的有序堆叠条形GGPLOT2正在拟定,这取决于缩放级别,一些白色的空间中横杠之间。不知道这个问题来自哪里 - 但一个疯狂的猜测是,我堆叠太多酒吧:页。

集结与plyr数据解决了这个问题:

element <- rep("apples", 15) 
qty <- c(2, 1, 4, 3, 6, 2, 1, 4, 3, 6, 2, 1, 4, 3, 6,) 
category1 <- c("Red", "Green", "Red", "Green", "Yellow") 
category2 <- c("small","big","big","small","small") 
d <- data.frame(element=element, qty=qty, category1=category1, category2=category2) 

plyr:

d <- ddply(d, .(category1, category2), summarize, qty=sum(qty, na.rm = TRUE)) 

为了解释这个公式的简要内容:

ddply(1, .(2, 3), summarize, 4=function(6, na.rm = TRUE)) 

1:数据帧名称 2,3:保留列 - >分组因子进行计算通过 总结:创建一个新的数据框(不像变换) 4:计算列 函数的名称:函数应用 - 这里的总和() 6:在其列应用功能

4,5,6可以重复更多的计算字段...

ggplot2: ggplot(d,aes(x = category1,y = qty,fill = category2))+ geom_bar(stat =“identity”))

所以,现在,正如RomanLuštrik所建议的那样,根据要显示的图形来汇总数据。

应用ddply后,的确,数据清洗剂:

category1 category2 qty 
1  Green  big 3 
2  Green  small 9 
3  Red  big 12 
4  Red  small 6 
5 Yellow  small 18 

我终于明白如何管理我的数据集,由于这种真正伟大的信息源: http://jaredknowles.com/r-bootcamp https://dl.dropbox.com/u/1811289/RBootcamp/slides/Tutorial3_DataSort.html

这一个太: http://streaming.stat.iastate.edu/workshops/r-intro/lectures/6-advancedmanipulation.pdf

...只是因为?ddply有点...奇怪(例子不同于解释对选项) - 看起来没有什么告诉速记写作...但我可能错过了一个点...

2

一种方法是通过category2订购您的数据。这也可以在ggplot()呼叫中完成。

ggplot(d[order(d$category2),], aes(x=category1, y=qty, fill=category2)) + 
      geom_bar(stat="identity")