stat_smooth - 可变长度不同

Jør*_*ers 2 r ggplot2

我想使用 ggplot 将两个变量相互绘制。稍后我想使用非线性拟合,但我遇到了一个我不完全理解的错误消息的问题。我可以看到其他人也有类似的问题,但我可能不够聪明,无法理解答案

我有一个包含 4 个变量的数据集 ost。数据集中没有NA。

使用 ggplot2 我想用回归线绘制数据。为简单起见,我从简单的线性回归开始

library(ggplot2)

qt_int <- c(404, 402, 426, 392, 418, 410)
rr <- c(1000, 958, 982, 752, 824, 844)
gender <- c('male','female','female','female','female','female')
deltnr <- c(10445, 1022, 9122, 60, 246, 306)
df = data.frame(deltnr, gender, qt_int, rr)   

p <- ggplot(df, aes(rr, qt_int))
p <- p + geom_point (size = 2)
p <- p + stat_smooth(method = "lm", formula = qt_int ~ rr)
p
Run Code Online (Sandbox Code Playgroud)

我收到以下警告消息:

警告消息:1:'newdata' 有 80 行,但发现的变量有 6702 行 2:计算失败stat_smooth():arguter antyder forskelligt antal rækker:80, 6 [En:参数建议不同的行数:80, 6]

奇怪的是,如果我省略公式,它就可以工作(但我以后想进行非线性拟合,所以我必须让它工作)

我错过了什么?

sco*_*coa 7

在公式中stat_smooth()应使用审美对象的名称(xycolour等),而不是变量的名称。见help("stat_smooth")

公式:用于平滑函数的公式,例如。'y ~ x', 'y ~ poly(x, 2)', 'y ~ log(x)'

OP写道:

p <- p + stat_smooth(method = "lm", formula = qt_int ~ rr)
Run Code Online (Sandbox Code Playgroud)

但正确的公式写法是:

p <- p + stat_smooth(method = "lm", formula = y ~ x)
Run Code Online (Sandbox Code Playgroud)

产生预期的输出:

在此处输入图片说明