我有以下数据集:

df <- data.frame(
    Product = c(755, 728, 417, 355, 913, 634, 385, 208, 204, 696, 968, 816, 279, 869, 823, 646, 986, 674, 806, 335, 731, 734, 107, 105, 512, 859, 159, 113, 353, 105, 205, 919, 243, 717, 838, 408, 423, 357, 408, 464, 724, 643, 943, 648, 623, 451, 449, 135, 842, 711),
    Category = c("Bread", "Cheese", "Bread", "Cheese", "Bread", "Cheese", "Bread", "Bread", "Bread", "Cheese", "Bread", "Bread", "Cheese", "Bread", "Cheese", "Bread", "Bread", "Bread", "Cheese", "Cheese", "Cheese", "Bread", "Bread", "Cheese", "Bread", "Cheese", "Bread", "Bread", "Bread", "Cheese", "Cheese", "Bread", "Bread", "Cheese", "Cheese", "Bread", "Bread", "Bread", "Bread", "Cheese", "Cheese", "Cheese", "Cheese", "Cheese", "Cheese", "Cheese", "Cheese", "Bread", "Bread", "Cheese"),
    Sub_Category = c("White", "Camembert", "White", "Camembert", "White", "Brie", "Brown", "Wholemeal", "White", "Brie", "Wholemeal", "Wholemeal", "Gouda", "Wholemeal", "Camembert", "Brown", "White", "Wholemeal", "Gouda", "Gouda", "Brie", "Wholemeal", "Wholemeal", "Camembert", "White", "Brie", "Wholemeal", "Wholemeal", "Brown", "Brie", "Brie", "White", "Wholemeal", "Camembert", "Gouda", "White", "Wholemeal", "Brown", "White", "Camembert", "Brie", "Brie", "Camembert", "Brie", "Camembert", "Gouda", "Camembert", "Wholemeal", "White", "Camembert"),
    Fibre = c(TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE, TRUE, TRUE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, TRUE)
)

我有以下脚本来制作一个情节:

library(tidyverse)

proportions <- df %>%
  group_by(Category, Sub_Category) %>%
  summarise(Proportion = mean(Fibre == TRUE) * 100, .groups = 'drop')

ggplot(data = proportions, aes(x = Proportion, y = Sub_Category, fill = Category)) +
  geom_col(position = "dodge") +
  labs(fill = "Category")

Bar Chart

这与我所期望的输出非常接近.

然而,我希望按类别对条块进行分组(所以所有的面包条都应该放在一起,然后再把cheese 条放在一起),并在分组后按降序排序.因此,从上到下依次为白色、全麦、棕色、布里干酪、卡门贝干酪、豪达干酪.

How can I reorder my bars so that they are grouped both by category and ordered by descending value?

推荐答案

您可以使用interaction():

ggplot(data = proportions, 
       aes(x = Proportion, 
           y = interaction(Sub_Category, Category), 
           fill = Category)
       ) +
  geom_col(position = "dodge") +
  scale_y_discrete(limits = rev) + 
  labs(fill = "Category")

You will get adjusted labels for y axis as well: enter image description here

Correction

因为我注意到你需要按比例排列,你可以用这个:

proportions %>%
mutate(Sub_Category2 = factor(Sub_Category, 
                                ordered = T, 
                                levels = arrange(.,  rev(Category), rank(Proportion, ties.method = "random")) %>% 
                                  pull(Sub_Category)
                                )
  )  %>% 
  ggplot(aes(x = Proportion, y = Sub_Category2, fill = Category)) +
  geom_col(position = "dodge") +
  labs(fill = "Category")

enter image description here

R相关问答推荐

在值和NA的行顺序中寻找中断模式

按R中的组查找相邻列的行累积和的最大值

根据R中两个变量的两个条件删除带有dspirr的行

根据模式将一列拆分为多列,并在R中进行拆分

为什么在ggplot2中添加geom_text这么慢?

如何调整曲线图中的y轴标签?

汇总数据表中两个特定列条目的值

用关联字符串替换列名的元素

使用列/行匹配将两个不同维度的矩阵相加

在嵌套列表中查找元素路径的最佳方法

R Select()可以测试不存在的子集列

根据纬度和距离连接两个数据集

使用不同的定性属性定制主成分分析中点的 colored颜色 和形状

手动指定从相同数据创建的叠加图的 colored颜色

如何在PrePlot()中将多个元素设置为斜体

`-`是否也用于数据帧,有时使用引用调用?

根据r中每行中的日期序列,使用列名序列创建新列

长/纬点继续在堪萨斯-SF结束,整齐的人口普查

R try Catch in the loop-跳过缺少的值并创建一个DF,显示跳过的内容

在不重复主题的情况下重新排列组