使用 Rsquare 和方程绘制散点图

Plotting a scatter plot with Rsquare and equation

我想使用 ggpubr 绘制带有回归方程和 R 平方的散点图。但我收到以下错误消息“无法在数据中找到 y 元素”。

这是一个可重现的例子:

    x <- c(841.564927857936, 841.564927857936, 841.424690409534, 841.284499691372, 
420.782463928968, 420.88768585043, 420.432103842432, 420.572177840048, 
420.712345204767, 420.782463928968, 420.747401645497)
y <- c(43692.05, 51561, 34637.8270288285, 36198.5838053982, 36909.925, 
30733.6584146036, 32350.3164029975, 31906.371814093, 30367.0638226962, 
32410.975, 31970.2108157654)
df <- data.frame(x,y)

ggscatter(df,x, y, add = "reg.line") +
  stat_cor(label.x = 3, label.y = 34) +
  stat_regline_equation(label.x = 3, label.y = 32)

谢谢

您需要将 x 和 y 的名称放在引号中,例如:

library(ggpubr)
ggscatter(df,x="x", y="y", add = "reg.line") +
  stat_cor(label.x = 3, label.y = 3000) +
  stat_regline_equation(label.x = 3, label.y = 5000)