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

Bland-Altman图标注需求:为LoA与均值添加文字及数值标识

给Bland-Altman图的一致性界限与均值线添加文字+数值标注

以下是修改后的完整代码,实现同时标注文字标识("+1.96 SD"、"Mean"、"-1.96 SD")和对应数值的需求:

# 加载依赖包
library(BlandAltmanLeh)
library(ggplot2)

# 导入测试数据
data <- structure(list(HF_Uhr = c(66L, 62L, 62L, 63L, 70L, 62L, 69L, 62L, 60L, 62L, 68L, 61L, 72L, 74L, 79L, 55L, 71L, 70L, 85L, 84L, 64L, 69L, 69L, 74L, 79L, 63L, 77L, 79L, 85L, 73L), HF_VNS = c(65.3, 61.4, 60.8, 62.9, 69.4, 61.1, 68.4, 62.6, 59.5, 62.9, 68.4, 60.1, 78.2, 73.1, 78.8, 58.8, 68.5, 70.6, 84.4, 84.7, 61.3, 69.4, 67.4, 82.2, 72.8, 62.6, 77.4, 78.8, 87.2, 73.2)), row.names = c(NA, 30L), class = "data.frame")

# 生成基础Bland-Altman图
HF_plot1 <- bland.altman.plot(data$HF_Uhr, 
                              data$HF_VNS, graph.sys = "ggplot2",
                              mode = 1)

# 调整线条颜色:上下界限蓝线,均值黑线
HF_plot1$layers[[2]]$aes_params$colour <- c('blue', 'black', 'blue')

# 构建标注文本:将文字标识与数值拼接
label_df <- data.frame(
  y = HF_plot1$layers[[2]]$data$yintercept,
  label = c(
    paste("+1.96 SD: ", round(HF_plot1$layers[[2]]$data$yintercept[1], 2)),
    paste("Mean: ", round(HF_plot1$layers[[2]]$data$yintercept[2], 2)),
    paste("-1.96 SD: ", round(HF_plot1$layers[[2]]$data$yintercept[3], 2))
  )
)

# 绘制最终图表(含中文标注)
HF_plot1 + 
  ggtitle("心率的Bland-Altman图")+
  theme(plot.title = element_text(size=15, face = "bold", hjust = 0.5)) +
  labs(x="Garmin Venu 2与VNS分析的心率均值 [bpm]\n(Garmin Venu 2 + VNS分析)/2",
       y ="Garmin Venu 2与VNS分析的心率差值 [bpm]\n(Garmin Venu 2 - VNS分析)")+
  theme(axis.title.x = element_text(size=9, hjust = 0.5))+
  theme(axis.title.y = element_text(size=8, hjust = 0.5, lineheight=1)) +
  geom_text(data = label_df,
            aes(x = 100, y = y, label = label),
            hjust = 0, vjust = -0.4, size = 3)

关键修改说明

  • 新增label_df数据框,手动拼接每条线的文字标识和计算得到的数值,确保标注内容完整
  • 替换原geom_text的标签数据源,直接使用拼接好的文本内容
  • 将图表标题、坐标轴标签全部翻译成中文,适配中文使用场景

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.25 21:06:30