编辑:

x = c(324, 219, 406, 273, 406, 406, 406, 406, 406, 168, 406, 273, 168, 406, 273, 168, 219, 324, 324, 406, 406, 406, 273, 273, 324, 324, 219, 273, 219, 273, 273, 324, 273, 324, 324, 406, 219, 406, 273, 273, 406, 219, 324, 273, 324, 406, 219, 324, 219, 324, 324, 406, 406, 406, 324, 273, 273, 219, 219, 324, 273, 324, 324, 219, 324, 219, 324, 219, 219, 324, 273, 406, 406, 273, 324, 273, 273, 219, 406, 273, 273, 324, 324, 324, 324, 324, 406, 324, 273, 406, 406, 219, 219, 324, 273, 406, 324, 324, 324, 324)
y = c(68,121,NA,87,NA,17,20,15,17,146,25,91,141,24,88,143,120,63,62,16,21,20,83,88,65,63,124,88,120,91,85,65,91,63,69,23,115,23,87,90,20,120,65,90,65,20,120,60,110,60,17,20,20,20,68,80,87,124,121,65,85,67,60,115,60,120,66,121,117,68,90,17,23,90,61,80,88,121,NA,91,88,62,60,70,60,60,27,76,96,23,20,113,118,60,91,23,60,60,65,70)

data = data.frame(x,y)

我使用ggplot2和函数geom_smooth()创建以下图形。我使用了代码:
g = ggplot(data, aes(x,y)) +
  geom_point() +
  geom_smooth(method="loess") +
  geom_smooth(method="lm", col="red")

我的数据包含变量x(只有9个值)和y(公制)。现在,我想添加使用该代码计算出的loess方法的投影点:
loes = loess(data$y ~ data$x)
RR = sort(unique(predict(loes)), decreasing=TRUE) # y coordinates
LL = unique(x, fromLast=TRUE) # x coordinates

现在,我将这些投影点添加到绘图中。
  g + geom_point(aes(y=RR[1], x=LL[1]), col="blue", size=2, shape=18) +
  geom_point(aes(y=RR[2], x=LL[2]), col="blue", size=2, shape=18) +
  geom_point(aes(y=RR[3], x=LL[3]), col="blue", size=2, shape=18) +
  geom_point(aes(y=RR[4], x=LL[4]), col="blue", size=2, shape=18) +
  geom_point(aes(y=RR[5], x=LL[5]), col="blue", size=2, shape=18)

为什么ggplot中的蓝点不在蓝黄线上? R中的loess-方法使用的代码与标准loess -function是否不同?

信息:对于我的原始数据(包含8000多个观测值),没有伪逆警告,但问题是相同的。

Example Image

最佳答案

错误在以下几行中:

loes = loess(y ~ x, data = data)
RR = sort(unique(predict(loes)), decreasing=TRUE) # y coordinates
LL = unique(x, fromLast=TRUE) # x coordinates

预测是使用相同的功能进行的,但顺序混乱。您应该使用newdata将预测与预测变量适当地匹配。
g = ggplot(data, aes(x,y)) +
  geom_smooth(method="loess", color = "red")

RR <- predict(loes, newdata = data.frame(x = unique(x)))

g + annotate("point", x = unique(x), y = RR)

显示位于平滑线上的点:
r - ggplot2 geom_smooth中黄土的基本设置是什么?-LMLPHP

关于r - ggplot2 geom_smooth中黄土的基本设置是什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39785413/

10-12 17:14