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

如何按变量最大值从高到低重新排序多组棒棒糖图?

问题:多组棒棒糖图按变量最大值排序

我希望将多组棒棒糖图按变量的最大值从高到低排序。此前尝试创建新变量并使用forcats包的fct_inorder函数,但未成功。难点在于多个设施共用同一茎的布局(而非分面展示)。

以下是我的数据、绘图代码:

数据代码

Kappa_viz <- structure(list(vname = c("Variable 1", "Variable 2", "Variable 3", 
                         "Variable 4", "Variable 5", "Variable 6", "Variable 7", "Variable 8", 
                         "Variable 9", "Variable 10", "Variable 11", "Variable 12", "Variable 13", 
                         "Variable 14", "Variable 15", "Variable 16", "Variable 1", "Variable 2", 
                         "Variable 3", "Variable 4", "Variable 5", "Variable 6", "Variable 7", 
                         "Variable 8", "Variable 9", "Variable 10", "Variable 11", "Variable 12", 
                         "Variable 13", "Variable 14", "Variable 15", "Variable 16", "Variable 1", 
                         "Variable 2", "Variable 3", "Variable 4", "Variable 5", "Variable 6", 
                         "Variable 7", "Variable 8", "Variable 9", "Variable 10", "Variable 11", 
                         "Variable 12", "Variable 13", "Variable 14", "Variable 15", "Variable 16"), 
               facname = c("Facility 1", "Facility 1", "Facility 1", "Facility 1", 
                           "Facility 1", "Facility 1", "Facility 1", "Facility 1", "Facility 1", 
                           "Facility 1", "Facility 1", "Facility 1", "Facility 1", "Facility 1", 
                           "Facility 1", "Facility 1", "Facility 2", "Facility 2", "Facility 2", 
                           "Facility 2", "Facility 2", "Facility 2", "Facility 2", "Facility 2", 
                           "Facility 2", "Facility 2", "Facility 2", "Facility 2", "Facility 2", 
                           "Facility 2", "Facility 2", "Facility 2", "Facility 3", "Facility 3", 
                           "Facility 3", "Facility 3", "Facility 3", "Facility 3", "Facility 3", 
                           "Facility 3", "Facility 3", "Facility 3", "Facility 3", "Facility 3", 
                           "Facility 3", "Facility 3", "Facility 3", "Facility 3"), 
               values = c(0.71, 
                          0.22, 0.13, 0.6, 0.22, 0.12, 0.16, 0.1, 0.97, 0.84, 0.53, 0.04, 
                          1, 0.92, 0.55, 0.1, 0.09, 0.84, 0.4, 0.25, 0.53, 0.49, 0.62, 
                          0.55, 0.52, 1, 0.96, 0.9, 0.55, 1, 0.21, 0.51, 0.81, 0.1, 0.63, 
                          0.92, 0.07, 0.55, 0.84, 0.81, 0.27, 0.93, 0.7, 0.05, 0.43, 0.78, 
                          0.47, 0.92)), class = "data.frame", row.names = c(NA, -48L))

绘图代码

library(plyr)
library(dplyr)
library(tidyr)
library(ggplot2)

p <- ggplot(Kappa_viz, 
            aes(y = vname, x = values, fill = facname)) +
  theme(axis.text = element_text(size = 15)) +
  theme(legend.text = element_text(size = 15)) +
  theme(legend.title = element_text(size = 20))+
  ggtitle("Correlation by Facility")+
  theme(plot.title = element_text(size = 20,hjust=0.5))+
  geom_segment(aes(x = 0, y = vname, xend = values, yend = vname), 
               color = "grey50", size=1) +
  geom_point(aes(color=facname), size = 5, alpha=0.7) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "bottom") +
  scale_color_discrete(name = "Kappa Interpretation")+
  scale_fill_discrete(guide = "none")

print(p)

我也曾尝试创建Order变量来指定排序顺序,但因多组结构问题未生效,现寻求有效的排序方法。


解决方案

要实现按变量最大值排序,核心是先计算每个vname的最大值,再基于这个最大值重新定义vname的因子水平顺序:

library(dplyr)
library(ggplot2)

# 计算每个变量的最大值,并按最大值降序提取变量名
var_order <- Kappa_viz %>%
  group_by(vname) %>%
  summarize(max_val = max(values)) %>%
  arrange(desc(max_val)) %>%
  pull(vname)

# 将vname转换为有序因子,指定排序顺序
Kappa_viz$vname <- factor(Kappa_viz$vname, levels = var_order)

# 绘制排序后的棒棒糖图
p <- ggplot(Kappa_viz, 
            aes(y = vname, x = values, fill = facname)) +
  theme(axis.text = element_text(size = 15)) +
  theme(legend.text = element_text(size = 15)) +
  theme(legend.title = element_text(size = 20))+
  ggtitle("Correlation by Facility (Sorted by Max Value)")+
  theme(plot.title = element_text(size = 20,hjust=0.5))+
  geom_segment(aes(x = 0, y = vname, xend = values, yend = vname), 
               color = "grey50", size=1) +
  geom_point(aes(color=facname), size = 5, alpha=0.7) +
  theme(axis.title.x = element_blank(),
        axis.title.y = element_blank(),
        legend.position = "bottom") +
  scale_color_discrete(name = "Kappa Interpretation")+
  scale_fill_discrete(guide = "none")

print(p)

关键说明

  • 先通过group_by+summarize计算每个变量的最大值,再用arrange(desc(max_val))按最大值降序排列,提取变量名得到排序后的水平列表var_order
  • 把vname转换成因子时指定levels = var_order,ggplot就会按照这个顺序渲染y轴
  • 原代码中的plyr包可移除,dplyr已足够完成数据处理

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 08:49:55