如何调整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
相关产品推荐
相关产品推荐

