Published and tested 18 November 2026.
colour controls the fitted line and fill controls the ribbon. Add aes(fill = group) to the smoothing layer and give scale_fill_manual() the same named palette as scale_colour_manual(). Here, A is blue and B is orange. I use an opacity of 0.22 here; adjust it to keep overlapping bands readable. Official smoothing reference.
What changes in the result?
I use the band to check the fill and the fitted line to check the colour. Looking at them separately makes it easier to spot which mapping changed.


How do I keep the colours and legend consistent?
Use named palette entries, such as c(A = "#0072B2", B = "#D55E00"). Give the colour and fill scales the same title, category order and labels. The example also maps line type and point shape to the same group, so the line identity remains visible without relying on colour alone. See the manual-scale reference.
Does changing the fill change the fitted model?
The fitted results stay the same: colour already defines the two groups, and fill maps to that same factor. The code checks that all 160 evaluated fitted values and their lower and upper bounds match between versions. It independently checks the intervals with predict.lm().
With method = "lm", se = TRUE and level = 0.95, these bands show pointwise 95% confidence intervals for the mean response. The fitted values and interval bounds stay the same. Use a separate, suitable analysis when testing differences between groups.
How can I run the complete example?
Save the R script in an empty output folder, install ggplot2 if needed, and run Rscript --vanilla example.R there. The script creates its own synthetic data. Tested with R 4.5.3 and ggplot2 4.0.4. The PNG files are 1200 × 900 pixels at approximately 300 PPI; the PDF page is 101.6 × 76.2 mm. These are example dimensions. The PDF uses the default export device; inspect its fonts before submission.
These are the code, data and result files used in this article. Choose a file below to run the example or compare your results.
- example.RComplete example code to run on your computer.
- synthetic.csvThe input data used in the example; open it as a table.
- before.pngThe figure before the change, for comparison.
- after.pngThe figure after applying the settings in this article.
- after.pdfThe result in PDF format, for viewing and page layout.
For sizing, use the pixel calculator. For panel assembly, use the existing export guide.
# Original synthetic example, code and data: CC0-1.0.
library(ggplot2)
set.seed(20261118)
d <- expand.grid(x = seq(0, 10, by = 0.5), replicate = 1:2,
group = c("A", "B"))
d$group <- factor(d$group, levels = c("A", "B"))
d$y <- 3 + 0.45 * d$x + ifelse(d$group == "B", 0.8, 0) +
rnorm(nrow(d), sd = 2)
write.csv(d, "synthetic.csv", row.names = FALSE)
palette <- c(A = "#0072B2", B = "#D55E00")
make_plot <- function(match_fill = FALSE) {
p <- ggplot(d, aes(x, y, colour = group, linetype = group)) +
geom_point(aes(shape = group), size = 1, alpha = 0.45) +
scale_colour_manual(name = "Group", values = palette) +
scale_linetype_manual(name = "Group", values = c(A = "solid", B = "dashed")) +
scale_shape_manual(name = "Group", values = c(A = 16, B = 17)) +
labs(x = "Input (arbitrary units)", y = "Response (arbitrary units)",
caption = "Synthetic data; pointwise 95% mean-response intervals") +
theme_classic(base_size = 9) +
theme(legend.position = "top", plot.caption = element_text(size = 7),
plot.margin = margin(6, 7, 6, 6))
if (match_fill) {
p <- p + geom_smooth(aes(fill = group), method = "lm", formula = y ~ x,
se = TRUE, level = 0.95, alpha = 0.22, linewidth = 0.65) +
scale_fill_manual(name = "Group", values = palette)
} else {
p <- p + geom_smooth(method = "lm", formula = y ~ x,
se = TRUE, level = 0.95, alpha = 0.22, linewidth = 0.65)
}
p
}
before <- make_plot(FALSE)
after <- make_plot(TRUE)
# The second layer is the smoother; style edits must preserve its estimates.
b <- ggplot_build(before)$data[[2]]
a <- ggplot_build(after)$data[[2]]
stopifnot(isTRUE(all.equal(b[c("x", "y", "ymin", "ymax", "group")],
a[c("x", "y", "ymin", "ymax", "group")],
check.attributes = FALSE)),
length(unique(b$fill)) == 1L, length(unique(a$fill)) == 2L,
all(a$alpha == 0.22), nrow(d) == 84L)
# Independently verify ggplot's pointwise mean confidence intervals with predict.lm.
for (id in 1:2) {
rows <- a[a$group == id, ]
fit <- lm(y ~ x, data = d[d$group == levels(d$group)[id], ])
ci <- predict(fit, data.frame(x = rows$x), interval = "confidence", level = .95)
stopifnot(max(abs(rows$y - ci[, "fit"])) < 1e-9,
max(abs(rows$ymin - ci[, "lwr"])) < 1e-9,
max(abs(rows$ymax - ci[, "upr"])) < 1e-9)
}
ggsave("before.png", before, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.png", after, width = 1200, height = 900, units = "px", dpi = 300, bg = "white")
ggsave("after.pdf", after, width = 101.6, height = 76.2, units = "mm", bg = "white")
write.csv(a[c("x", "y", "ymin", "ymax", "group", "colour", "fill", "alpha")],
"computed-smooth.csv", row.names = FALSE)
capture.output(sessionInfo(), file = "session-info.txt")
cat("PASS: 84 synthetic observations; 2 group models; 160 evaluated rows; unchanged fits and bounds; independent predict.lm matches.\n")
Where did the question and example data come from?
A Reddit question dated 12 September 2026 prompted this answer. The example data and code were created for this tutorial.
The original script, synthetic data and generated PNG/PDF figures are released under CC0-1.0 for reuse, including commercial work. ggplot2 retains its MIT license and R its GPL-2 or GPL-3 license; install the required packages separately to run the code.
Learn R

