Making crochet pattern design decisions with ggplot2
Sometimes, you and your significant other are attending a virtual statistics conference (eCOTS) and she decides to continue crocheting a Chicago flag-inspired baby blanket for her cousin’s baby shower (Ravelry “field of daisies” pattern linked here). Clearly, this is a common occurrence.
She’s made 27 granny squares, all with a red circle. She wants to make a six square by nine square blanket, so needs 54 squares and isn’t confident on how to proceed, design-wise. Here are two exemplar squares:
And overall progress at this point:
You think that 54 red dots (every square) might not look that good, but are having a hard time making your case. Obviously, you need to create a ggplot2
so that she can compare the two pattern ideas!
First, you need to find the colors of the Chicago flag:
Then, create a tibble
(extension of data.frame
) with the data needed to make the plot. inner*
is the color of the center circle in the granny square, mid
and outer
are alternating colors, and x
and y
are the locations at which to plot the squares.
flag_colors = tibble(
inner1 = rep(red, 54),
mid = rep(c(lblue, white), 27),
outer = rep(c(white, lblue), 27),
x = rep(1:6, each = 9),
y = rep(1:9, times = 6),
# 2 - 4 not shown
inner2 = rep(c(red, lblue, white, red), times = 14)[1:54],
inner3 = rep(c(white, red, red, lblue), times = 14)[1:54],
picker4 = sample(rep(c(1, 2), times = 27)),
inner4 = if_else(picker4 == 1, outer, red),
# I learned mapping was "up and down"
# "cleverly" used matrix byrow parameter to swap to "left to right"
picker5 = rep(c(red, red, "a", "a"), times = 14)[1:54] |>
matrix(nrow = 9, ncol = 6, byrow = TRUE) |>
as.vector(),
inner5 = if_else(picker5 == "a", outer, red)
)
Then, you write a function to plot any established inner*
pattern. With a little help from Wolfram’s page on Regular Pentagons (equations 5 - 8), you are able to create a relatively high fidelity “daisy” shape.
patch_plot = function(data, inner, cons = 2.15, radius = 0.25){
c1 = 0.25*(sqrt(5) - 1)*radius
c2 = 0.25*(sqrt(5) + 1)*radius
s1 = 0.25*(sqrt(10 + 2*sqrt(5)))*radius
s2 = 0.25*(sqrt(10 - 2*sqrt(5)))*radius
ggplot(data = {{data}}, aes(x = x, y = y, color = mid)) +
geom_point(aes(color = outer), size = 16*cons, shape = 15) +
geom_point(aes(y = y + radius), size = 5*cons) +
geom_point(aes(x = x + s1, y = y + c1), size = 5*cons) +
geom_point(aes(x = x + s2, y = y - c2), size = 5*cons) +
geom_point(aes(x = x - s2, y = y - c2), size = 5*cons) +
geom_point(aes(x = x - s1, y = y + c1), size = 5*cons) +
geom_point(aes(color = {{inner}}), size = 4*cons) +
scale_color_identity() +
coord_fixed() +
xlim(c(0.5, 6.5)) +
ylim(c(0.5, 9.5)) +
theme_void()
}
Finally, you want to compare two options – one with all red dots, and one with about half red dots.
Alas, your significant other was not convinced. BUT you had fun making a ggplot
function in the process, she was impressed with your ability to represent the design with ggplot
, and she was more confident in her decision to complete the pattern as planned.
R for the Rest of Us gave this post a shout-out on LinkedIn in their newsletter, including an announcement about Posit’s new IDE, Positron (I’ll need to check this IDE out – I’m still using RStudio). And with that, I’ll include a couple of bonus images for those curious about how this all turned out! I do think the red dots worked out nicely after all.