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

如何在ggplot中按值对分组数据排序并保留分组结构

解决方案:ggplot气泡图分组内按值排序

问题分析

当前气泡图的y轴使用带格式的myPalette文本,默认按字母顺序排列,无法直接通过fct_reorder实现分组内按contribution.scaled降序排序。需要先对数据按分组和目标值排序,再将y轴变量转换为有序因子来保留排序结构。

解决步骤

  • 按Bioprocess分组,在每个分组内按contribution.scaled从大到小排序
  • 将myPalette转换为有序因子,顺序对应排序后的结果
  • 用处理后的有序因子作为y轴变量绘图

修改后的完整代码

library(ggplot2)
library(viridis)
library(forcats)
library(tidyverse)
library("ggtext")

# 原始数据
erg <- structure(list(name = c("B", "C", "A", "E", "D", "F", "G", "H"
), diff.1 = c(0.054442494, -0.000557423, 0.052649513, 0.046030676, 0.00337372, 
              0.05238731, -0.009539911,  -0.008215683), myPalette = c("<span style=\"color: #0D0887FF\">B</span>", 
                                                                      "<span style=\"color: #0D0887FF\">C</span>", "<span style=\"color: #0D0887FF\">A</span>", 
                                                                      "<span style=\"color: #0D0887FF\">E</span>", "<span style=\"color: #0D0887FF\">D</span>", 
                                                                      "<span style=\"color: #F0F921FF\">F</span>", "<span style=\"color: #F0F921FF\">G</span>", 
                                                                      "<span style=\"color: #F0F921FF\">H</span>"), contribution.scaled = c(0.054442494, 
                                                                                                                                           0.046030676, 0.052649513, 0.057581535, 0.05238731, 0.082962111,  
                                                                                                                                           0.064853576, 0.101192828), Bioprocess = structure(c(2L, 2L, 2L, 
                                                                                                                                                                                               2L, 2L, 1L, 1L, 1L), .Label = c("Angiogenesis", "Hypertrophy"
                                                                                                                                                                                               ), class = "factor")), row.names = c(NA, -8L), class = "data.frame")

# 数据预处理:分组排序并转换为有序因子
ert_processed <- erg %>%
  # 按Bioprocess分组,组内按contribution.scaled降序排列
  group_by(Bioprocess) %>%
  arrange(desc(contribution.scaled), .by_group = TRUE) %>%
  # 将myPalette转换为有序因子,顺序为排序后的顺序
  mutate(myPalette_ordered = fct_inorder(myPalette)) %>%
  ungroup()

# 绘图:使用有序因子作为y轴
ggplot(data=ert_processed, aes(x=diff.1, y=myPalette_ordered, size = contribution.scaled, text=name, color=Bioprocess)) +
  geom_point(alpha=0.5) +
  scale_size(range = c(1.4, 18), name="Activity") +
  scale_color_viridis(discrete=T,option = "C", direction = -1) +
  theme(legend.position="right") +
  theme(axis.text.y=ggtext::element_markdown(size=10)) +
  guides(color = guide_legend(override.aes = list(size = 10))) +
  xlim(-0.1, 0.1) +
  labs(title="Comparison of activated pathways",
       x ="<- activated in cc          activated in tt ->   ", y = "pathways")+ 
  theme(plot.title = element_text(hjust="0.5"))+
  theme(text=element_text(size=16.5)) 

关键说明

  • group_by(Bioprocess) %>% arrange(desc(contribution.scaled), .by_group = TRUE)确保每个分组内的条目按目标值降序排列
  • fct_inorder(myPalette)将排序后的myPalette转换为有序因子,ggplot会按照因子的顺序渲染y轴
  • 已修正原始数据中H的myPalette标签错误(缺失闭合</span>)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 03:01:00