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

如何在分面ggplot2中自动放置多个模型评估参数?

解决方法

你只需要调整垂直偏移量vertical_adjustment的判断逻辑,给新增的NSE也分配对应的垂直位置即可,两种实现方式可选:

方式1:基于现有代码直接修改

你已经把NSE加入了df.annotations,只需要把原来二分支的ifelse改成多分支判断,给三个指标分别设置垂直偏移:

# 三个指标分别指定vjust值,数值越大标注位置越靠下
vertical_adjustment = case_when(
  grepl("Rsq", df.annotations$label) ~ 1.5,
  grepl("RMSE", df.annotations$label) ~ 3,
  grepl("NSE", df.annotations$label) ~ 4.5
)

# 后续绘图代码不变
p + geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
              hjust = -0.1, vjust = vertical_adjustment, size=3.5)

如果未加载dplyr用不了case_when,也可以嵌套ifelse实现:

vertical_adjustment = ifelse(grepl("Rsq",df.annotations$label),1.5,
                             ifelse(grepl("RMSE",df.annotations$label),3,4.5))

方式2:更简洁的长表生成标注数据(推荐)

不用手动逐个rbind添加指标,直接把统计结果summ转成长格式自动生成标注,后续新增指标不用改拼接逻辑:

library(tidyr)
# 直接从统计结果生成标注数据框,自动包含所有指标
df.annotations <- summ %>% 
  pivot_longer(cols = -Species, names_to = "indicator", values_to = "value") %>% 
  mutate(label = paste(indicator, value, sep = " = "))

# 按指标顺序设置垂直偏移,顺序和summ里的指标顺序一致即可
vertical_adjustment = case_when(
  df.annotations$indicator == "Rsq" ~ 1.5,
  df.annotations$indicator == "RMSE" ~ 3,
  df.annotations$indicator == "NSE" ~ 4.5
)

# 绘图逻辑不变
p + geom_text(data=df.annotations,aes(x=-Inf,y=+Inf,label=label),
              hjust = -0.1, vjust = vertical_adjustment, size=3.5)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.18 17:27:01