锚点与ggplot geom_smooth

时间:2017-01-30 04:09:55

标签: r ggplot2

我正在用let username = nameTextField.text制作一个简单的情节,我想添加一个平滑的线,在第一点锚定(固定)。我使用了here描述的技巧,但看起来我需要通过添加差异ggplot2来重新调整拟合值。我该怎么做呢?还有更好的方法吗?

y[1] - predict(lm, data.frame(y=5))

Result

1 个答案:

答案 0 :(得分:2)

试试这个,它应该有效:

m <- lm(I(y-y[1]) ~ 0 + poly(x-x[1],4), data=d) # model without intercept

ggplot(d, aes(x, y)) +
  geom_point() +
  geom_smooth(method='lm', formula = y ~ poly(x,4), se=F) +
  geom_line(data=data.frame(x=d$x, 
  # the intercept is y[1] - poly(x)[1,]*coeff (it's not computed by lm)
  # y = prediction by the model    + the intercept
  y = poly(d$x,4)%*%m$coefficients + d$y[1]-as.numeric(poly(d$x,4)[1,]%*%m$coefficients)), 
  aes(x,y), color='red')

enter image description here