1. Read the data

library(tidyverse)

ess <- read_csv("data/ess.csv")

glimpse(ess)
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, …
  • European Social Survey, round 11: one row per respondent, 39,514 rows (download)
  • eduyrs: years of education
  • lrscale: left-right self-placement, 0 = left, 10 = right
  • Both use 77, 88, 99 for missing answers

2. Clean

ess <- ess |>
  mutate(
    # missing codes become NA
    lrscale_clean = if_else(
      lrscale %in% c(77, 88, 99), NA, lrscale
    ),
    # keep 4 to 24 years (the rest becomes NA)
    edu = if_else(
      between(eduyrs, 4, 24), eduyrs, NA
    )
  )

# check: smallest and largest value
range(ess$lrscale_clean, na.rm = TRUE)
[1]  0 10
range(ess$edu, na.rm = TRUE)
[1]  4 24
  • Always check after cleaning: 0 to 10, and 4 to 24
  • Outside 4 to 24 years: 1.5% of respondents. Dropping them keeps the plot readable

3. Model

model <- 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

4. Plot: how a ggplot is built

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.

5. Make it publication ready

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.

6. Export

# 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")
  • Save at the size it will be printed: one column is about 8 cm, a full page about 16 cm. Then 11 pt in R is 11 pt in the paper
  • PDF for the paper: lines and text stay sharp. cairo_pdf embeds the fonts, so it looks the same on every machine and printer
  • PNG (300 dpi by default) for Word and slides
  • Not the Export button of the Plots pane: the size depends on your window
  • Check the journal’s guidelines: some ask for TIFF or EPS

Getting started

Install (once per computer)

install.packages("tidyverse")

Load (every session): ggplot2, dplyr, readr and more

Animation style: tidy-animations by Emil Hvitfeldt