在UpSet图中对集合进行排序

编程语言 2026-07-12

我想把集合大小按从大到小排序,但似乎不起作用。我也尝试了自定义集合,但也不行。下面是我的代码:

UpSetR::upset(
  modality_matrix_upset,
  nsets = 7,
  sets = c("AB (10000)","ND (9000)", "US (8000)","CA (7500)", 
           "RF (7000)", "XA (6500)","NM (5000)"),
  #number.angles = 30,
  order.by = "freq",
  #decreasing = TRUE,
  keep.order = TRUE,
  mainbar.y.label = "Number of school",
  sets.x.label = "Number of participant schools",
  )

示例数据:

set.seed(123)

# create example dataset
modality_matrix_upset <- data.frame(
  "AB (10000)" = sample(0:1, 200, replace = TRUE),
  "ND (9000)"  = sample(0:1, 200, replace = TRUE),
  "US (8000)"  = sample(0:1, 200, replace = TRUE),
  "CA (7500)"  = sample(0:1, 200, replace = TRUE),
  "RF (7000)"  = sample(0:1, 200, replace = TRUE),
  "XA (6500)"  = sample(0:1, 200, replace = TRUE),
  "NM (5000)"  = sample(0:1, 200, replace = TRUE),
  check.names = FALSE
)

我希望按降序排序,数值最大的排在最上面,最小的排在最下面。

解决方案

keep.order = TRUE 会保留你传入集合中的顺序,但集合大小的条形(左下角)始终基于你传入 sets 参数的顺序来绘制。

sets_ordered <- names(sort(colSums(modality_matrix_upset)))

UpSetR::upset(
  modality_matrix_upset,
  nsets = 7,
  sets = sets_ordered,
  order.by = "freq",
  keep.order = TRUE,
  mainbar.y.label = "Number of school",
  sets.x.label = "Number of participant schools"
)

创建于2026-03-06,使用 reprex v2.1.1

站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。

相关文章