在ggplot中创建预测间隔(点和带状宝石的不同数据)

时间:2017-05-20 13:48:40

标签: r ggplot2

我正在尝试使用ggplot2()创建预测区间图。我希望只在原始数据框中绘制预测区间之外的点,并绘制在另一个数据框中创建的x值序列的预测间隔功能区,该数据框覆盖最小值和最大值x原始数据框中使用的值。下面是一个展示细节的mwe:

library(ggplot2) 

dat <- data.frame(qsec=mtcars$qsec, wt=mtcars$wt)
m <- lm(wt ~ qsec, data = dat) 
mpi <- cbind(dat, predict(m, interval = "prediction"))
# Keep only points that are outside the prediction interval
plotPoints <- mpi[which(!(mpi$wt > mpi$lwr & mpi$wt < mpi$upr)),]

# Create prediction interval data frame with upper and lower lines corresponding to sequence covering minimum and maximum of x values in original dataset
newx <- seq(min(mpi$qsec), max(mpi$qsec), by=0.05)
pred_interval <- predict(m, newdata=data.frame(qsec=newx), interval="prediction", level = 0.95)
pred_interval <- as.data.frame(pred_interval)

# Below are three different attempts to plot the prediction upper and lower lines as ribbons and the points outside the prediction interval as points. Each attempt gives an error which is also commented.

# Error: Object 'qsec' not found
ggplot(data=plotPoints, aes(x = qsec, y = wt)) + geom_point() + 
  geom_ribbon(data=pred_interval, aes(ymin = lwr, ymax = upr), fill = "blue", alpha = 0.2) 

# Error: Object 'wt' not found
ggplot(data=plotPoints, aes(x = qsec)) + geom_point(y = wt) + 
  geom_ribbon(data=pred_interval, aes(ymin = lwr, ymax = upr), fill = "blue", alpha = 0.2) 

# Error: geom_ribbon requires the following missing aesthetics: x
ggplot(data=plotPoints) + geom_point(aes(x = qsec, y=wt)) + 
  geom_ribbon(data=pred_interval, aes(ymin = lwr, ymax = upr), fill = "blue", alpha = 0.2) 

非常感谢任何关于如何实现此类情节的建议。

1 个答案:

答案 0 :(得分:0)

在排除故障方面做得很好。答案是在最后一个错误中:

Error: geom_ribbon requires the following missing aesthetics: x

geom_ribbon需要x轴的一些变量。您在主qsec调用中指定了ggplot,但此列不在pred_interval数据框中,因此geom_ribbon会丢失。尝试:

pred_interval$qsec = newx
ggplot(data=plotPoints, aes(x = qsec)) + geom_point(aes(y = wt)) + 
  geom_ribbon(data=pred_interval, aes(ymin = lwr, ymax = upr), fill = "blue", alpha = 0.2)