You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何按Kiwi占比降序重排簇,绘制position='fill'的geom_bar百分比图?

按Kiwi占比排序Cluster的堆叠百分比图实现方法

要实现将纵轴的cluster按Kiwi占比从高到低排序,核心是先计算每个cluster中Kiwi的占比,再以此为依据重新设定cluster的因子顺序,具体操作如下:

实现步骤

  1. 计算各Cluster的Kiwi占比:
    对数据按cluster分组,统计每组中Kiwi的数量占该组总数量的比例。
  2. 重新设定Cluster的因子顺序:
    将cluster转换为因子类型,按照Kiwi占比从高到低的顺序设置因子水平。
  3. 绘制排序后的图表:
    在ggplot的x轴参数中使用重新排序后的cluster因子即可。

修改后的完整代码(使用dplyr简化操作)

library(ggplot2)
library(dplyr)

# 生成示例数据
df = data.frame(matrix(nrow=200, ncol=2))
colnames(df) <- c("cluster", "name")
df$cluster <- sample(20, size = nrow(df), replace = TRUE)
df$fruit <- sample(c("banana", "apple", "orange", "kiwi", "plum"), size = nrow(df), replace = TRUE)

# 计算Kiwi占比并重新排序cluster因子
df <- df %>%
  group_by(cluster) %>%
  mutate(kiwi_pct = sum(fruit == "kiwi") / n()) %>%
  ungroup() %>%
  mutate(cluster = factor(cluster, 
                          levels = arrange(., desc(kiwi_pct)) %>% distinct(cluster) %>% pull(cluster)))

# 绘制图表
p = ggplot(df, aes(x = cluster, fill = as.factor(fruit)))+
  geom_bar(position = 'fill') + 
  theme_classic()+
  coord_flip() +
  scale_y_continuous(labels = scales::percent) + 
  theme(axis.text.y = element_text(size = 20),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        axis.text=element_text(size=20)) +
  theme(legend.text = element_text(size = 20)) +
  xlab("Cluster")+
  ylab("水果占比") +
  labs( fill = "")
p  

关键代码解释

  • group_by(cluster) %>% mutate(kiwi_pct = sum(fruit == "kiwi") / n()):按cluster分组后,计算每组中Kiwi数量占该组总样本数的比例。
  • factor(cluster, levels = arrange(., desc(kiwi_pct)) %>% distinct(cluster) %>% pull(cluster)):将cluster转换为因子,通过arrange(desc(kiwi_pct))按Kiwi占比降序排列,提取排序后的cluster值作为因子水平,确保ggplot按此顺序展示。

无dplyr的base R实现方案

如果不想依赖dplyr包,也可以用基础R代码完成排序:

library(ggplot2)

# 生成示例数据(同前)
df = data.frame(matrix(nrow=200, ncol=2))
colnames(df) <- c("cluster", "name")
df$cluster <- sample(20, size = nrow(df), replace = TRUE)
df$fruit <- sample(c("banana", "apple", "orange", "kiwi", "plum"), size = nrow(df), replace = TRUE)

# 计算各cluster的Kiwi占比
kiwi_counts <- table(df$cluster, df$fruit)[, "kiwi"]
total_counts <- table(df$cluster)
kiwi_pct <- kiwi_counts / total_counts

# 按Kiwi占比降序排序cluster
sorted_clusters <- names(sort(kiwi_pct, decreasing = TRUE))

# 转换cluster为因子
df$cluster <- factor(df$cluster, levels = sorted_clusters)

# 绘图代码同原代码
p = ggplot(df, aes(x = cluster, fill = as.factor(fruit)))+
  geom_bar(position = 'fill') + 
  theme_classic()+
  coord_flip() +
  scale_y_continuous(labels = scales::percent) + 
  theme(axis.text.y = element_text(size = 20),
        axis.title.x = element_text(size = 20),
        axis.title.y = element_text(size = 20),
        axis.text=element_text(size=20)) +
  theme(legend.text = element_text(size = 20)) +
  xlab("Cluster")+
  ylab("水果占比") +
  labs( fill = "")
p  

内容的提问来源于stack exchange,提问作者MeganCole

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 17:45:47