ggplot2 builds figures from a grammar: you state what the data is, which columns map to which aesthetics (x, y, colour), and what geometry draws them. Once that clicks, every ggplot2 chart is the same three decisions. This lesson makes a scatter plot with a trend line.

Step 1 — Open the R mode

In Figure Studio, open the Code tab and pick R. Write the script, press Run, and the plot renders in the preview.

Step 2 — Data, aesthetics, geometry

Start with a small data frame and the three-part pattern:

library(ggplot2)

df <- data.frame(
  dose     = c(1, 2, 4, 8, 16, 32),
  response = c(3.1, 5.0, 8.7, 14.9, 26.2, 44.8)
)

ggplot(df, aes(x = dose, y = response)) +
  geom_point(size = 2)

aes() maps columns to axes; geom_point() says “draw them as points”. Every layer after this is added with +.

Step 3 — Add a trend line

Layers stack — a smoother on top of the points:

ggplot(df, aes(x = dose, y = response)) +
  geom_point(size = 2) +
  geom_smooth(method = "lm", se = TRUE)

method = "lm" fits a straight line; the shaded band is its confidence interval (se = FALSE removes it).

Step 4 — Label it

labs() names everything in one place:

ggplot(df, aes(x = dose, y = response)) +
  geom_point(size = 2) +
  geom_smooth(method = "lm") +
  labs(x = "Dose (mg)", y = "Response", title = NULL)

Papers usually carry the title in the LaTeX \caption, not on the image — leaving title = NULL keeps the figure clean.

Step 5 — Export or insert

Export / Insert as with every code figure: a vector PDF for LaTeX, or straight into the current paper with a \label. The R script is what gets saved, so the figure reopens here for its next revision.

Tip: Grouped data is one aesthetic away — map a category column with aes(colour = group) and ggplot2 splits the points and builds the legend for you. The next lesson turns that and theming into a print-ready figure.