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

如何调整R语言eulerr包韦恩图标签位置,使百分比显示在计数下方

解决eulerr韦恩图计数与百分比上下显示的问题

要实现计数在上、百分比在下的显示效果,你需要自定义quantities的标签生成逻辑,通过换行符\n将两者拼接。同时修正原代码中的参数冗余问题,以下是调整后的完整代码:

library(eulerr)
library(viridis)

# 构造欧拉图对象
eu_obj <- euler(
  c( "LUTS"=98, 
     "Symptoms of UTI"=46, 
     "Positive urine culture"=39, 
     "Symptoms of UTI&LUTS"=33, 
     "LUTS&Positive urine culture"=24, 
     "Symptoms of UTI&Positive urine culture"=22, 
     "Symptoms of UTI&LUTS&Positive urine culture"=18),
  input = "union", 
  shape = "ellipse"
)

# 绘制图形,自定义quantities标签
plot(
  eu_obj, 
  key = TRUE, 
  counts = FALSE,  # 关闭默认计数显示,改用自定义quantities
  quantities = list(
    fun = function(x) {
      counts <- x$counts
      pct <- round(x$percent, 2)
      paste0(counts, "\n", pct, "%")  # 用换行符拼接计数和百分比
    },
    font = 3, 
    cex = 0.8
  ), 
  fills = list(fill = plasma(n = 3)), 
  alpha = 0.3, 
  edges = list(lty = 1, col = c("#1957FF", "#FF750C", "#FF220C")),  # 整合边缘颜色参数
  factor_names = TRUE, 
  labels = list(font = 2, cex = 1), 
  legend = FALSE
)

关键调整说明:

  • 关闭counts = TRUE,避免与自定义quantities内容重复显示
  • 利用quantities$fun自定义标签生成逻辑,通过换行符\n将计数和百分比上下拼接
  • 整合冗余参数:将边缘颜色指定到edges$col,移除重复的alpha参数

这样生成的韦恩图中,每个区域的计数会显示在上方,百分比显示在下方,符合需求。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.03 19:40:33