Rows: 39,514
Columns: 2
$ eduyrs <dbl> 12, 14, 16, 14, 12, 16, 8, 17, 12, 9, 17, 14, 12, 88, 88, 11, …
$ lrscale <dbl> 5, 0, 3, 5, 2, 4, 4, 3, 5, 5, 4, 4, 77, 5, 3, 3, 3, 6, 4, 88, …
eduyrs: years of educationlrscale: left-right self-placement, 0 = left, 10 = rightmodel <- lm(lrscale_clean ~ edu, data = ess)
# coefficients with 95% confidence intervals
parameters::model_parameters(model)Parameter | Coefficient | SE | 95% CI | t(33587) | p
-------------------------------------------------------------------------
(Intercept) | 5.59 | 0.05 | [ 5.50, 5.68] | 120.95 | < .001
edu | -0.04 | 3.34e-03 | [-0.04, -0.03] | -10.66 | < .001
ess |>
ggplot(aes(x = edu, y = lrscale_clean)) +
stat_summary(fun = mean, geom = "point") +
geom_smooth(method = "lm") +
scale_x_continuous(breaks = seq(4, 24, 4)) +
labs(
x = "Years of education",
y = "Left-right (0 = left, 10 = right)"
) +
theme_minimal(base_size = 21)Each + adds one piece.
Lay the plot down…
…and pull it apart: one card per block of code.
ess |> ggplot(aes(...)): the data and the mapping, edu on x and lrscale_clean on y. This sets up the axes.
stat_summary(fun = mean, geom = "point"): one dot per year of education, the mean left-right position of everyone with that many years.
geom_smooth(method = "lm"): the regression line from step 3 (slope -0.036), fitted on all respondents, not on the dots. Grey band: its 95% confidence interval.
scale_x_continuous(): puts the x axis numbers at 4, 8, …, 24.
labs(): the axis titles.
theme_minimal(): the look. Here: no grey background, light grid lines, and text size 21.
Stack the cards again…
…and it is one plot.
ess |>
ggplot(aes(x = edu, y = lrscale_clean)) +
stat_summary(
fun.data = mean_se,
fun.args = list(mult = 1.96), # 95% CI
geom = "pointrange",
colour = "grey55"
) +
geom_smooth(method = "lm", colour = "black") +
geom_hline(yintercept = 0) +
scale_x_continuous(breaks = seq(4, 24, 4)) +
scale_y_continuous(breaks = seq(0, 10, 2)) +
coord_cartesian(ylim = c(0, 10)) +
labs(
x = "Years of education",
y = "Left-right (0 = left, 10 = right)"
) +
theme_minimal(
base_size = 21,
base_family = "serif" # optional: a Times-like font
) +
theme(
panel.grid.minor = element_blank(),
panel.grid.major.x = element_blank()
)We can make a couple of small fixes:
1. Data in the background: grey dots
2. Show how sure each mean is: a 95% confidence interval. At 24 years the mean rests on 129 people, in the middle on thousands.
3. The result in front: a black line (prints in greyscale too)
4. Less ink: only horizontal grid lines
5. The whole 0 to 10 scale, with a baseline at 0: from 4 to 24 years, the line drops only 0.7 points. Significant, but small. coord_cartesian() zooms without dropping data.
No title inside the figure: in a journal, it goes in the caption.
6. Optional: base_family = "serif" for a Times-like font, to match a paper in Times New Roman. Many journals want sans serif in figures: check their guidelines.
Figure 1. Left-right self-placement by years of education. Dots: mean per year with 95% confidence interval. Line: OLS fit with 95% confidence interval (N = 33,589). Data: European Social Survey, round 11.
# the plot from step 5 in an object,
# with print-size text (11 pt)
p <- ess |>
ggplot(aes(x = edu, y = lrscale_clean)) +
# ... the other layers from step 5 ...
theme_minimal(base_size = 11)
# for the paper: PDF (vector, sharp at any zoom),
# with the fonts embedded
ggsave(
"figure1.pdf",
p,
width = 16,
height = 11,
units = "cm",
device = cairo_pdf
)
# for Word or slides: PNG
ggsave("figure1.png", p, width = 16, height = 11, units = "cm")cairo_pdf embeds the fonts, so it looks the same on every machine and printerInstall (once per computer)
install.packages("tidyverse")Load (every session): ggplot2, dplyr, readr and more
Animation style: tidy-animations by Emil Hvitfeldt
Learn more
Choose and check a chart (Yan Holtz)
The theory