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

如何结合expression()与paste0()在R中显示R²上标?

解决ggplot中R²上标显示及动态公式整合问题

先修正代码顺序错误

你的代码中pred_df的生成在mod定义之前,这会导致predict(mod)报错,因为mod还未创建。需要将mod的定义移到pred_df之前。

解决R²上标与动态数值结合的问题

直接用paste0生成纯文本无法解析上标格式,单独用expression无法插入动态计算的参数(如a、Rsquared等),以下是两种可行方案:


方案一:使用bquote()

bquote()允许在表达式中通过.(变量名)插入动态值,结合atop()实现换行:

# 加载必要包
library(ggplot2)
library(Metrics)

# 数据集
ISIDOR <- structure(list(Pos_heliaphen = c("W30", "X41", "Y27", "Z24", 
                                       "Y27", "W30", "W30", "X41", "Y27", "W30", "X41", "Z40", "Z99"
), traitement = c("WW", "WW", "WW", "WW", "WW", "WW", "WW", "WW", 
                  "WW", "WW", "WW", "WW", "WW"), Variete = c("Isidor", "Isidor", 
                                                             "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", "Isidor", 
                                                             "Isidor", "Isidor", "Isidor", "Cali"), FTSW_apres_arros = c(0.462837958498518, 
                                                                                                                         0.400045032939416, 0.352560790392534, 0.377856799586057, 0.170933345859364, 
                                                                                                                         0.315689846065931, 0.116825600914318, 0.0332444780173884, 0.00966070114456602, 
                                                                                                                         0.0871102539376406, 0.0107280083093036, 0.195548432729584, 1), 
NLE = c(0.903498791068124, 0.954670066942938, 0.970762905436272, 
        0.873838605282389, 0.647875257025359, 0.53056603773585, 0.0384548155916796, 
        0.0470924009989314, 0.00403163281128882, 0.193696514297641, 
        0.0718450645564359, 0.295346695941639, 1)), class = c("tbl_df", 
                                                              "tbl", "data.frame"), row.names = c(NA, -13L))

# 调整顺序:先定义模型
mod = nls(NLE ~ 2/(1+exp(a*FTSW_apres_arros))-1, start = list(a=1), data = ISIDOR)
ISIDOR$pred = predict(mod, ISIDOR)

# 生成预测数据框
pred_df <- data.frame(FTSW_apres_arros = seq(min(ISIDOR$FTSW_apres_arros), 
                                         max(ISIDOR$FTSW_apres_arros),
                                         length.out = 100))
pred_df$NLE <- predict(mod, newdata = pred_df)

# 计算评估指标
a = coef(mod)
RMSE = rmse(ISIDOR$NLE, ISIDOR$pred)
MSE = mse(ISIDOR$NLE, ISIDOR$pred)
Rsquared = summary(lm(ISIDOR$NLE~ ISIDOR$pred))$r.squared

# 绘图:使用bquote构建subtitle
ggplot(ISIDOR, aes(FTSW_apres_arros, NLE)) + 
  geom_point(aes(color = Variete), pch = 19, cex = 3) +
  geom_line(data = pred_df) +
  scale_color_manual(values = c("red3","blue3"))+
  scale_y_continuous(limits = c(0, 1.0)) +
  scale_x_continuous(limits = c(0, 1)) +
  labs(title = "Isidor", 
       y = "Expansion folliaire totale relative",
       x = "FTSW",
       subtitle = bquote(atop(y == 2/(1 + exp(.(round(a,3)) * x)) - 1,
                              R^2 == .(round(Rsquared,3)) ~~ RMSE == .(round(RMSE,3)) ~~ MSE == .(round(MSE,3)))))

方案二:使用parse(text = ...)

先拼接包含表达式语法的字符串,再用parse()解析为可渲染的表达式:

# 生成带表达式语法的字符串
subtitle_str <- paste0(
  "atop(y == 2/(1 + exp(", round(a,3), "* x)) - 1,",
  "R^2 == ", round(Rsquared,3),
  "~~ RMSE == ", round(RMSE,3),
  "~~ MSE == ", round(MSE,3), ")"
)

# 绘图:使用parse解析字符串
ggplot(ISIDOR, aes(FTSW_apres_arros, NLE)) + 
  geom_point(aes(color = Variete), pch = 19, cex = 3) +
  geom_line(data = pred_df) +
  scale_color_manual(values = c("red3","blue3"))+
  scale_y_continuous(limits = c(0, 1.0)) +
  scale_x_continuous(limits = c(0, 1)) +
  labs(title = "Isidor", 
       y = "Expansion folliaire totale relative",
       x = "FTSW",
       subtitle = parse(text = subtitle_str))

关键说明

  • ==:在表达式中用于显示等号(用=会被识别为赋值,显示异常)
  • ~~:在表达式中添加空格,避免指标挤在一起
  • atop():实现多行文本的垂直排列,替代换行符

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.29 09:28:15