Exercise: Ribbons (2)
- 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