# [r] How can I plot data with confidence intervals?

If I have 10 values, each of which has a fitted value `F`, and an upper and lower confidence interval `U` and `L`:

``````set.seed(0815)
F <- runif(10, 1, 2)
L <- runif(10, 0, 1)
U <- runif(10, 2, 3)
``````

How can I show these 10 fitted values and their confidence intervals in the same plot like the one below in R?

This question is related to `r` `plot` `intervals`

Here is a plotrix solution:

``````set.seed(0815)
x <- 1:10
F <- runif(10,1,2)
L <- runif(10,0,1)
U <- runif(10,2,3)

require(plotrix)
plotCI(x, F, ui=U, li=L)
``````

And here is a ggplot solution:

``````set.seed(0815)
df <- data.frame(x =1:10,
F =runif(10,1,2),
L =runif(10,0,1),
U =runif(10,2,3))

require(ggplot2)
ggplot(df, aes(x = x, y = F)) +
geom_point(size = 4) +
geom_errorbar(aes(ymax = U, ymin = L))
``````

UPDATE: Here is a base solution to your edits:

``````set.seed(1234)
x <- rnorm(20)
df <- data.frame(x = x,
y = x + rnorm(20))

plot(y ~ x, data = df)

# model
mod <- lm(y ~ x, data = df)

# predicts + interval
newx <- seq(min(df\$x), max(df\$x), length.out=100)
preds <- predict(mod, newdata = data.frame(x=newx),
interval = 'confidence')

# plot
plot(y ~ x, data = df, type = 'n')