继回答我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”)
但是图形有点混乱:颜色没有组合在一起!
为什么会有这种行为?
是否还有一个选项可以在不改变数据的情况下对颜色进行正确分组?
为什么重塑出来的问题? 'stat = identity'只会画出你给它的东西。在你的情况下,一个凌乱的数据集。你必须手动处理表格才能给你想要的结果(我不明白它应该是什么样子)。 –
我尝试将最轻的代码嵌入到php代码插件中以包含在网站中(tikiwiki CMS) - 因此非R-wise用户仍可以从其数据中自定义一些统计信息。另外,因为在一个页面中可以有很多插件,所以我希望保持服务器用于显示统计数据低......这意味着重塑不是真的没有问题:) –