堆叠的条形图,每个堆叠的独立填充顺序 [英] Stacked barchart, independent fill order for each stack

查看:102
本文介绍了堆叠的条形图,每个堆叠的独立填充顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正面临ggplot2的行为,我无法理解的排序和堆积条形图.
我已经阅读了一些有关它的问题(,依此类推),但不幸的是,我找不到适合我的解决方案.也许答案很简单,但我看不到.希望不是骗子.

I'm facing a behaviour of ggplot2, ordering and stacked barplot that I cannot understand.
I've read some question about it (here,here and so on), but unluckily I cannot find a solution that suits to me. Maybe the answer is easy and I cannot see it. Hope it's not a dupe.

我的主要目标是根据排序列(在此称为ordering)独立地对每个堆栈进行排序.

My main goal is to have each stack ordered independently, based on the ordering column (called here 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列一样应该在图中进行排序.

So there is an ID, a value, a value to use to order, and a filling, the data are as they should be ordered in the plot, as looking the ordering column.

我试图绘制它:这个想法是将xc绘制为x轴的堆叠条形图,值valuefilling填充,但是填充按顺序具有ordering的值,按升序排列,即 ie ordering 每列底部的最大值. filling的顺序与数据集的顺序相同,即每一列都有独立的顺序.

I tried to plot it: the idea is to plot as a stacked barchart with x axis the id, the value value, filled by filling, but the filling has as order the value of ordering, in an ascending ordering, i.e. biggest value of ordering at the bottom for each column. The ordering of the filling is somewhat equal as the dataset, i.e. each column has an independent order.

您可以想象它们是假数据,因此id的数量可以变化.

As you can imagine those are fake data, so the number of id can vary.

 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

当我绘制它们时,有一些我不理解的东西:

When I plot them, there is something I do not understand:

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的顺序不正确,我应该具有原始数据集的顺序.

The second and the third id are not properly ordered, I should have the order of the original dataset.

推荐答案

如果使用单独的geom_bar,则可以使顺序不同.

If you use separate geom_bars, you can make the orders different.

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"))

这篇关于堆叠的条形图,每个堆叠的独立填充顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆