library(ggplot2)
library(svglite)
# Flag colours, usually a shade of blue background with white diagonals
saltire_colour_background <- "#0065BD"
saltire_colour_lines <- "white"
# Flag dimensions ratio aspect ratio 5/3, will use mm as units
saltire_width <- 500 # 5cm
saltire_height <- 300 # 3cm
saltire_slope <- saltire_height / saltire_width
# "Due to a historical error, the unit of linewidth is roughly 0.75 mm."
# https://ggplot2.tidyverse.org/articles/ggplot2-specs.html
# line width on saltire should be between 1/3 and 1/5 width
# So 1/5 of saltire_width = 100, 100 * 0.75 = 75
saltire_line_width <- 75
plot_saltire <- ggplot() +
# Draw the dark background of the flag
geom_rect(
aes(
xmin = 0, xmax = saltire_width,
ymin = 0, ymax = saltire_height
),
fill = saltire_colour_background,
color = saltire_colour_background
) +
# Draw the diagonal from bottom left to top right
geom_abline(
intercept = 0,
slope = saltire_slope,
color = saltire_colour_lines,
linewidth = saltire_line_width
) +
# Draw the diagonal from top left to bottom right
geom_abline(
intercept = saltire_height,
slope = -saltire_slope,
color = saltire_colour_lines,
linewidth = saltire_line_width
) +
# Remove padding around x axis
scale_x_continuous(expand = c(0, 0)) +
# Remove padding around y axis
scale_y_continuous(expand = c(0, 0)) +
# 1:1 aspect ratio for physical scale
coord_fixed() +
# A completely empty theme.
theme_void()
# Store a PNG version
ggsave("saltire.png",
plot = plot_saltire,
device = "png",
dpi = 72,
width = saltire_width,
height = saltire_height,
units = "mm"
)
# Store a SVG version
ggsave("saltire.svg",
plot = plot_saltire,
device = "svg",
dpi = 72,
width = saltire_width,
height = saltire_height,
units = "mm"
)Programmatically create a Scottish saltire using R
Create a digital flag of Scotland
coding
R
Scotland
Keywords
flag, ggplot2, Saint Andrew, saltire, Scotland
How to draw the flag of Saint Andrew?
The national flag of Scotland is called a saltire and has a blue background with a white diagonal cross. This is the flag of our patron saint, Saint Andrew.
I want to recreate this flag using R code.
Code
Reuse
Citation
BibTeX citation:
@online{duff2025,
author = {Duff, Lesley K.},
title = {Programmatically Create a {Scottish} Saltire Using {R}},
date = {2025-04-25},
url = {https://www.dataquine.dev/blog/2025/04/25/scottish-saltire/},
langid = {en-GB}
}
For attribution, please cite this work as:
Duff, Lesley K. 2025. “Programmatically Create a Scottish Saltire
Using R.” April 25, 2025. https://www.dataquine.dev/blog/2025/04/25/scottish-saltire/.
