我有以下数据和代码:

# Load libraries and data.

library(tidyverse)

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

# Organise data for plotting.

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

# Plot data.

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

它会产生输出:

Plot

Y轴标签包括Category和Sub_Category数据(例如布朗和面包).我希望他们只包括Sub_Category数据(即只包括棕色或白色或全麦等,与图例给出的类别数据).

How can I adjust the y-axis labels to remove the category information and keep the sub-category information?

理想情况下,解决方案应该是程序化的(而不是手动编辑y标签),因为我的实际数据具有比本例多得多的类别和子类别.

推荐答案

在数据准备代码中创建交互并手动编辑结果,而不是在绘图代码中创建交互.下面我创建一个新的辅助变量yaxis,以便更清楚地说明这一点.这迫使我还要编辑y轴标签.

suppressPackageStartupMessages(
  library(tidyverse)
)

# Load libraries and data.

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

# Organise data for plotting.

proportions <- df %>%
  group_by(Category, Sub_Category) %>%
  summarise(Proportion = mean(Fibre == TRUE) * 100, .groups = 'drop') %>%
  mutate(yaxis = interaction(Sub_Category, Category),
         yaxis = sub("\\..*$", "", yaxis),
         yaxis = factor(yaxis, levels = yaxis))

# Plot data.

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

创建于2024-03-20,共reprex v2.1.0

R相关问答推荐

如何在弹性表中为类别值的背景上色

将一个载体的值相加,直到达到另一个载体的值

如果行和列名以相同的开头,将矩阵值设置为0

在特定列上滞后n行,同时扩展框架的长度

使用ggplot将平滑线添加到条形图

行式dppr中的变量列名

如何使用R中的dhrr函数将李克特量表的因子列从长转换为宽?

在不安装软件包的情况下测试更新

如何动态更新selectizeInput?

Rplotly中的Sankey Diagram:意外连接&

如何编辑gMarginal背景以匹配绘图背景?

在使用bslb和bootstrap5时,有没有办法更改特定dt行的 colored颜色 ?

从多个可选列中选取一个值到一个新列中

如何在R中改变fviz_pca_biplot中圆的边界线的 colored颜色 ?

如何提取R中其他字符串和数字之间的字符串?

计算Mean by分组和绑定到R中的数据集

如何使投篮在R中保持一致

如何修改GT表中组名行的 colored颜色 ?

子样本间系数检验的比较

按顺序将地块添加到列表