## 13.6 Plotly: Ribbons (confidence intervalls)

fit <- lm(Fertility ~ Catholic, data = swiss)
broom::augment(fit) %>%
plot_ly(x = ~Catholic, showlegend = FALSE) %>%
add_markers(y = ~Fertility, color = I("black")) %>%
add_ribbons(ymin = ~.fitted - 1.96 * .se.fit,
ymax = ~.fitted + 1.96 * .se.fit, color = I("black")) %>%
add_lines(y = ~.fitted, color = I("black"))

### 13.6.1 Exercise: Ribbons (1)

1. Generate 4 scatterplots with Fertility as outcome variable and Agriculture, Examination, Education, Catholic as explanatory variables (see dataset swiss) and visualize them as small multiples. Make the graph publicaton ready! As below but better!

### 13.6.2 Exercise: Ribbons (2)

1. See the graph below. An attempt to plot ribbons on top of each other. The code is below. Can you repair it? :-S
fit <- lm(Fertility ~ Catholic, data = swiss)
fit.broomed <- broom::augment(fit)
p <-  plot_ly(type = "scatter", data = fit.broomed,
x = fit.broomed$Catholic, y = fit.broomed$Fertility, color = I("black"), name = "Catholic")
p <- add_ribbons(p, ymin = fit.broomed$.fitted - 1.96 * fit.broomed$.se.fit,
ymax = fit.broomed$.fitted + 1.96 * fit.broomed$.se.fit, color = I("black"), name = "Catholic")  %>%
add_lines(y = fit.broomed$.fitted, color = I("black"), name = "Catholic") fit2 <- lm(Fertility ~ Agriculture, data = swiss) fit2.broomed <- broom::augment(fit2) p <- add_trace(p, x = fit2.broomed$Agriculture, name = "Agriculture") %>%
add_markers(y = fit2.broomed$Fertility, color = I("blue"), name = "Agriculture") %>% add_ribbons(ymin = fit2.broomed$.fitted - 1.96 * fit2.broomed$.se.fit, ymax = fit2.broomed$.fitted + 1.96 * fit2.broomed$.se.fit, color = I("blue"), name = "Agriculture") %>% add_lines(y = fit2.broomed$.fitted, color = I("blue"), name = "Agriculture")

fit3 <- lm(Fertility ~ Education, data = swiss)
fit3.broomed <- broom::augment(fit3)
p <-  add_trace(p, x = fit3.broomed$Education, name = "Education") %>% add_markers(y = fit3.broomed$Fertility, color = I("red"), name = "Education") %>%
add_ribbons(ymin = fit3.broomed$.fitted - 1.96 * fit3.broomed$.se.fit,
ymax = fit3.broomed$.fitted + 1.96 * fit3.broomed$.se.fit, color = I("red"), name = "Education") %>%
add_lines(y = fit3.broomed\$.fitted, color = I("red"), name = "Education")
p