堆积条形图,独立的填充次序为每个堆栈堆积条形图,独立的填充次序为每个堆栈(Stacked barch

2019-05-12 11:23发布

我现在面临的一个行为ggplot2 ,排序和堆叠barplot,我无法理解。
我读过关于它的(一些问题在这里 , 在这里等),但偏偏我无法找到一个适合我的解决方案。 也许答案很简单,我无法看到它。 希望这不是一个傻瓜。

我的主要目标是让每个堆栈独立有序的基础上,排序列(这里称为ordering )。

在这里我有一些数据:

library(dplyr)
library(ggplot2)
dats <- data.frame(id = c(1,1,1,2,2,3,3,3,3),
                   value = c(9,6,4,5,6,4,3,4,5),
                   ordering = c(1,2,3,2,3,1,3,2,4),
                   filling = c('a','b','c','b','a','a','c','d','b')) %>% arrange(id,ordering)

因此,有一个ID,一个值,用来顺序的值,并填充,数据,他们应该在剧情进行排序,为寻找的ordering列。

我试图绘制它:这个想法是画出与x轴的堆叠条形图id ,值value ,由实心filling ,但是,填充物为订单的价值ordering ,在升序排列也就是最大的价值 ordering 在底部为每列 。 所述的顺序filling是作为数据集有所相等,即,每个列具有一个独立的顺序。

正如你能想象那些都是假的数据,所以ID的数量可以改变。

 id value ordering filling
1  1     9        1       a
2  1     6        2       b
3  1     4        3       c
4  2     5        2       b
5  2     6        3       a
6  3     4        1       a
7  3     4        2       d
8  3     3        3       c
9  3     5        4       b

当我绘制出来,有件事我不明白:

library(dplyr) 
dats$filling <- reorder(dats$filling, -dats$ordering)

ggplot(dats,aes(x = id,
                y = value,
                fill = filling)) + 
  geom_bar(stat = "identity",position = "stack") +
  guides(fill=guide_legend("ordering")) 

第二和第三ID不正确排序,我应该有原始数据集的顺序。

Answer 1:

如果使用单独的geom_bar S,可以使订单不同。

dats %>% 
  ggplot(aes(x = id, y = value, fill = reorder(filling,-ordering))) + 
    geom_bar(stat = "identity", position = "stack", data = dats %>% filter(id == 1)) +
    geom_bar(stat = "identity", position = "stack", data = dats %>% filter(id == 2)) +
    geom_bar(stat = "identity", position = "stack", data = dats %>% filter(id == 3)) +
    guides(fill=guide_legend("ordering")) 

更普遍:

bars <- map(unique(dats$id)
            , ~geom_bar(stat = "identity", position = "stack"
                       , data = dats %>% filter(id == .x)))

dats %>% 
  ggplot(aes(x = id, y = value, fill = reorder(filling,-ordering))) + 
    bars +
    guides(fill=guide_legend("ordering"))


Answer 2:

问题是,你的情况,不同的酒吧应该使用相同的值(水平) filling以不同的顺序。 这与冲突的方式ggplot工作原理:取因子水平(其已经具备了一定的顺序),并在每个酒吧以同样的方式将它们应用。

那么解决方法是...创建许多因子水平。

ggplot(dats, aes(x = id, y = value, fill = interaction(-ordering, id))) + 
  geom_bar(stat = "identity", position = "stack")

这一次,现在是太详细太“大方”。 然而,我们现在能做的就是应对传说和不同的颜色:

dats <- arrange(dats, id, -ordering)
aux <- with(dats, match(sort(unique(filling)), filling))
ggplot(dats, aes(x = id, y = value, fill = interaction(-ordering, id))) + 
  geom_bar(stat = "identity", position = "stack") +
  scale_fill_manual("Ordering", values = scales::hue_pal()(4)[dats$filling],
                    labels = with(dats, filling[aux]), 
                    breaks = with(dats, interaction(-ordering, id)[aux]))

在这里,我第一次重新排列的行dats ,以避免这样做以后。 然后aux是辅助矢量

aux
# [1] 3 2 1 8

给予其中水平的任意位置(每个) abc ,和d (以该顺序)出现在dats ,这再次是有用的后面。 然后,我简单地设置相应的刻度值,标签和休息......最后,我用scales::hue_pal恢复原来的颜色调色板。



Answer 3:

这里的问题是,所述元件filling = d只出现在第三组中具有低的值。 一种解决方案,可以以填充非当前值与0:

library(dplyr)
#> 
#> Attachement du package : 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union
library(ggplot2)
dats <- data.frame(id = c(1,1,1,1,2,2,2,2,3,3,3,3),
                   value = c(9,6,4,0,5,6,0,0,4,3,4,5),
                   ordering = c(1,2,3,5,2,3,5,5,1,3,2,4),
                   filling = c('a','b','c','d','b','a','c','d','a','c','d','b')) %>% arrange(id,ordering)


ggplot(dats,aes(x = id,
                y = value,
                fill = reorder(filling,-ordering))) + 
  geom_bar(stat = "identity",position = "stack") +
  guides(fill=guide_legend("ordering")) 

由创建于2018年12月3日reprex包 (v0.2.1)



文章来源: Stacked barchart, independent fill order for each stack
标签: r ggplot2