## 6.2 Graph: Coefficient plots

• Here we will work with the ‘famous’ swiss data set (?swiss)
• Figure 6.1 below provides a simple coefficient plot.
• Questions:
• What does the graph show? What are the underlying variables (and data)?
• How many scales/mappings does it use? Could we reduce them?
• What do you like, what do you dislike about the figure? What is good, what is bad?
• What kind of information could we add to the graph (if any)?
• How would you approach a replication of the graph? Figure 6.1: Coefficient plot

### 6.2.1 Lab: Data & code

• Learning objectives
• How to plot the results of statistical models
• How to use the broom package and the tidy function
• How to plot different confidence intervals

We start by preparing the data, i.e., estimating the model and extracting the coefficients of interest. Importantly, the table you see below is all you need as a basis for the ggplot functions.

fit <- lm(Fertility ~ Catholic + Agriculture + Education, data = swiss) # see ?swiss
results <- tidy(fit)
fit_cis_95 <- confint_tidy(fit, conf.level = 0.95, func = stats::confint) %>%
rename_all(function(x){paste(x, "_95", sep="")})
fit_cis_90 <- confint_tidy(fit, conf.level = 0.90, func = stats::confint) %>%
rename_all(function(x){paste(x, "_90", sep="")})
results <- bind_cols(results,
fit_cis_95,
fit_cis_90) %>%
rename(Variable = term,
Coefficient = estimate,
SE = std.error) %>%
filter(Variable != "(Intercept)")
results %>% kable("html") %>% kable_styling(font_size = 10)
Variable Coefficient SE statistic p.value conf.low_95 conf.high_95 conf.low_90 conf.high_90
Catholic 0.1452013 0.0301466 4.816504 0.0000184 0.0844049 0.2059978 0.0945227 0.1958800
Agriculture -0.2030377 0.0711516 -2.853591 0.0066235 -0.3465286 -0.0595467 -0.3226486 -0.0834268
Education -1.0721468 0.1558018 -6.881477 0.0000000 -1.3863512 -0.7579425 -1.3340607 -0.8102329

Subsequently, we feed those estimates into the ggplot function.

• geom_point(), geom_linerange(): Plot estimates and intervals
• geom_hline(): Plot 0 line
• coord_flip(): Flip the plot
ggplot(results, aes(x = Variable, y = Coefficient)) +
geom_hline(yintercept = 0, colour = gray(1/2), lty = 2) +
geom_point(aes(x = Variable,
y = Coefficient)) +
geom_linerange(aes(x = Variable,
ymin = conf.low_90,
ymax = conf.high_90),
lwd = 1) +
geom_linerange(aes(x = Variable,
ymin = conf.low_95,
ymax = conf.high_95),
lwd = 1/2) +
ggtitle("Outcome: Fertility") +
coord_flip() 