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

如何结合geom_richtext与geom_text_repel?gridtext不支持<ol>标签求解

解决方案

问题根源

报错核心并非geom_text_repel,而是你处理后的文本中仍存在gridtext(geom_richtext依赖包)不支持的<ol>标签——你仅替换了<b>标签,未清理其他HTML标签,导致解析失败。

替代方案

1. 精准清理不支持的HTML标签

先移除gridtext不兼容的标签(如<ol>、</ol>、<li>、</li>等),再保留并转换<b>为Markdown粗体格式:

library(dplyr)
library(stringr)

items.to.plot <- items.to.plot %>%
  mutate(
    plot_group = as.factor(plot_group),
    # 移除列表类不支持标签
    text.clean = str_replace_all(`Item Text`, "<ol>|</ol>|<li>|</li>", ""),
    # 转换<b>标签为Markdown粗体
    text.tmp = str_replace(text.clean, "<b>", "**"),
    text.final = str_replace(text.tmp, "</b>", "**")
  )

若文本中还有其他不支持标签(如<ul>),可直接在str_replace_all的匹配模式中追加。

2. 稳健处理所有非粗体HTML标签

如果不确定文本中存在哪些不支持标签,可直接移除除<b>/</b>外的所有HTML标签:

library(dplyr)
library(stringr)

items.to.plot <- items.to.plot %>%
  mutate(
    plot_group = as.factor(plot_group),
    # 移除所有非粗体的HTML标签
    text.clean = str_replace_all(`Item Text`, "<(/?)[^b>]+>", ""),
    # 批量转换<b>标签为Markdown粗体
    text.final = str_replace_all(text.clean, "<(/?)b>", "\\1**")
  )

3. 拆分适配不同geom的文本格式

geom_text_repel不支持Markdown/HTML格式,可单独给它传入纯文本标签,避免格式冲突:

ggplot(items.to.plot, aes(x=plot_group, y=measure, color=as.factor(extra))) +
  geom_point() +
  # 传入带Markdown格式的文本给geom_richtext
  geom_richtext(aes(label=wrap_strings(text.final, 16)), fill=NA, label.color=NA, hjust=-.3, size=6) +
  # 传入移除粗体标记的纯文本给geom_text_repel
  geom_text_repel(aes(label=wrap_strings(str_remove_all(text.final, "\\*\\*"), 16)), size=2)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 00:43:11