微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么我的ggplot点对点而不是绘制回归线?

如何解决为什么我的ggplot点对点而不是绘制回归线?

我希望绘制一条回归线,显示草食性损害(%)与距过渡带距离的函数。但是正如您所看到的,它将所有数据点连接在一起。

这是情节:

Plot

代码如下:

#plotting herbivory as a function of distance from the ecotone
# Get fitted values for our model 
model_fit <- predict(object = herb.mod,se.fit = T)
# Add these predictions to our original data frame,in a column called fit
leaf.data$fit <- model_fit$fit
# We can then work out the upper and lower bounds of our confidence intervals,adding them to separate columns
leaf.data$upper <- model_fit$fit  + 2 * model_fit$se
leaf.data$lower <- model_fit$fit  - 2 * model_fit$se

ggplot(data=leaf.data)+
  geom_point(aes(x = distance.from.ecotone,y = mean.herbivory,col=transect))+
  # add a line for model fit
  geom_line(aes(x = distance.from.ecotone,y = mean.herbivory),size=1.0)+
  # add a ribbon showing the CIs
  geom_ribbon(aes(x = distance.from.ecotone,ymin = lower,ymax = upper),alpha=0.25)+
  # add a title 
  ggtitle("Herbivorous damage as a Function of distance from an Ecotone")+
  theme_light()

解决方法

您想通过均值而不是单个点画一条线,因此可以使用geom_smooth()来绘制预测平均值:

set.seed(111)
leaf.data = data.frame(distance.from.ecotone=rep(seq(5,22.5,by=2.5),each=5))
leaf.data$mean.herbivory = -3*leaf.data$distance.from.ecotone + rnorm(nrow(leaf.data),3) + 80
leaf.data$transect = rep(c("One","Two"),each=5,times=4)

herb.mod = lm(mean.herbivory~distance.from.ecotone,data=leaf.data)
model_fit <- predict(object = herb.mod,se.fit = T)
leaf.data$fit <- model_fit$fit
leaf.data$upper <- model_fit$fit  + 2 * model_fit$se
leaf.data$lower <- model_fit$fit  - 2 * model_fit$se

这将起作用:

ggplot(data=leaf.data)+
geom_point(aes(x = distance.from.ecotone,y = mean.herbivory,col=transect))+
geom_line(aes(x = distance.from.ecotone,y = fit),size=1.0)+
geom_ribbon(aes(x = distance.from.ecotone,ymin = lower,ymax = upper),alpha=0.25)

enter image description here

请注意,您的置信区间也会重复:

tail(leaf.data)
   distance.from.ecotone mean.herbivory transect      fit    upper     lower
35                  20.0      13.202012      One 19.05008 20.68734 17.412828
36                  22.5      15.988981      Two 11.55215 13.57185  9.532456
37                  22.5      12.151535      Two 11.55215 13.57185  9.532456
38                  22.5      13.502768      Two 11.55215 13.57185  9.532456
39                  22.5      10.637426      Two 11.55215 13.57185  9.532456
40                  22.5       8.570465      Two 11.55215 13.57185  9.532456

为预测创建单独的data.frame可能更有意义,例如:

pred = data.frame(distance.from.ecotone = 5:23)
model_fit <- predict(herb.mod,pred,se.fit = T)
pred$fit <- model_fit$fit
pred$upper <- model_fit$fit  + 2 * model_fit$se
pred$lower <- model_fit$fit  - 2 * model_fit$se

ggplot(data=leaf.data)+
geom_point(aes(x = distance.from.ecotone,col=transect))+
geom_line(data=pred,aes(x = distance.from.ecotone,size=1.0)+
geom_ribbon(data=pred,alpha=0.25)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。