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

如何在ggplot2的geom_text_repel标签中将百分比文本加粗?

在ggplot2中使用ggrepel实现标签内部分文本加粗

可以实现该需求,核心是借助ggtext包的富文本渲染能力,结合ggrepel的防重叠标签功能。具体实现步骤如下:

1. 安装并加载必要的包

除已有的包外,需新增ggtext来解析带样式的标签:

install.packages("ggtext")
library(ggtext)

2. 重构标签文本,添加加粗标记

将需要加粗的百分比部分用HTML的<b>标签包裹,生成带样式标记的标签字符串:

right_label <- paste0(Dati$Lotto, ", ", round(Dati$VixME), " mPa s, <b>", sprintf("%.1f", Dati$increase), "%</b>")

3. 替换文本几何对象为富文本版本

把原geom_text_repel替换为ggtext::geom_text_repel_richtext,它会自动解析HTML样式标记,同时保留ggrepel的防重叠特性:

# 替换原右侧标签的geom_text_repel代码
ggtext::geom_text_repel_richtext(label=right_label, y=Dati$VixME, x=rep(2, NROW(Dati)), hjust=-0.1, size=2.5, fill=NA, label.color=NA)

注:设置fill=NA和label.color=NA是为了移除richtext默认的背景框与边框,让标签样式和原效果一致。

完整修改后的代码

# 创建数据框
Dati <- data.frame(
  Prodotto = c("PU 115k", "PU 115 std", "PU 115k", "PU 115FI", "PU 115FI", "PU 115FI"),
  VixME = c(28350, 28800, 31250, 25500, 36900, 28850),
  Ref = c("PU 204", "PU 204", "PU 204", "PU 204", "PU 204", "PU 204"),
  Lotto = c("lt. 103694", "lt. 76823", "lt. 77763", "lt. 106156", "lt. 103703", "lt. 76093"),
  VixLab = c(25700, 27950, 29050, 25300, 35500, 27250),
  class = c("green", "blue", "green", "red", "red", "red"),
  increase = c(10.3, 3.0, 7.6, 0.8, 3.9, 5.9)
)

library(scales)
library(ggrepel)
library(ggtext) # 加载富文本支持包
theme_set(theme_classic())

# 生成标签文本
left_label <- paste0(Dati$Lotto, ", ", round(Dati$VixLab), " mPa s")
# 重构右侧标签,添加加粗标记
right_label <- paste0(Dati$Lotto, ", ", round(Dati$VixME), " mPa s, <b>", sprintf("%.1f", Dati$increase), "%</b>")

p <- ggplot(Dati) + 
  geom_segment(aes(x=1, xend=2, y=VixLab, yend=VixME, col=class), size=.75, show.legend=TRUE) + 
  geom_vline(xintercept=1, linetype="dashed", size=.1) + 
  geom_vline(xintercept=2, linetype="dashed", size=.1) +
  scale_color_manual(
    labels = c("a", "b", "c"), 
    values = c("green" = "#0D1BBe", "red" = "#BE0D1B", "blue" = "#438C2A")
  ) +
  labs(x="", y="viscosity (mPa s)") +  
  xlim(.5, 2.5) + ylim(min(Dati$VixLab, Dati$VixME), (1.1 * max(Dati$VixLab, Dati$VixME)))

p <- p + 
  geom_text_repel(label=left_label, y=Dati$VixLab, x=rep(1, NROW(Dati)), hjust=1.1, size=2.5) +
  # 使用富文本版本的防重叠标签
  ggtext::geom_text_repel_richtext(label=right_label, y=Dati$VixME, x=rep(2, NROW(Dati)), hjust=-0.1, size=2.5, fill=NA, label.color=NA) +
  geom_text(label="Viscosity CQ", x=1, y=1.1*(max(Dati$VixLab, Dati$VixME)), hjust=1.2, size=3.5) +
  geom_text(label="Viscosity after test", x=2, y=1.1*(max(Dati$VixLab, Dati$VixME)), hjust=-0.1, size=3.5) +
  theme(
    panel.background = element_blank(), 
    panel.grid = element_blank(),
    axis.ticks = element_blank(),
    axis.text.x = element_blank(),
    panel.border = element_blank(),
    plot.margin = unit(c(1, 2, 1, 2), "cm"),
    legend.title = element_blank(),
    legend.position = "bottom", 
    legend.box.spacing = unit(-0.5, 'cm')
  )

p

效果说明

运行上述代码后,标签中由sprintf("%.1f", Dati$increase)生成的百分比数值会以加粗样式显示,其余文本保持常规字体,同时标签仍会自动避免重叠。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.15 15:05:00