library(tidyverse)
theme_set(theme_void() + theme(legend.position = "none"))
Truchet tiles
Triangle tiles
# set up the tiles data
<- tibble(x = c(0, 0, 1, 0, 1, 1, 0, 1),
tile1 y = c(0, 1, 0, 0, 0, 1, 1, 0),
g = factor(rep(1:2, each = 4)),
tile = "tile1")
<- tibble(x = c(0, 0, 1, 0, 0, 1, 1, 0),
tile2 y = c(0, 1, 1, 0, 0, 1, 0, 0),
g = factor(rep(1:2, each = 4)),
tile = "tile2")
# the next two tiles just reverse color
<- tile1 %>%
tile3 mutate(g = factor(rep(2:1, each = 4)),
tile = "tile3")
<- tile2 %>%
tile4 mutate(g = factor(rep(2:1, each = 4)),
tile = "tile4")
set.seed(20231030)
<- ggplot(tile1, aes(x, y)) +
gtile geom_polygon(aes(fill = g, group = g), color = "black", linewidth = 2) +
coord_equal() +
# sample two colors randomly
scale_fill_manual(values = sample(colors(), 2))
gtile
%+% tile2 gtile
%+% tile3 gtile
%+% tile4 gtile
Now let’s make 10 x 10 times grid with these tiles!
<- expand_grid(row = 0:9, col = 0:9) %>%
mygrid mutate(tile_id = 1:n()) %>%
rowwise() %>%
mutate(tile = sample(c("tile1", "tile2", "tile3", "tile4"), 1)) %>%
full_join(bind_rows(tile1, tile2, tile3, tile4), by = "tile") %>%
mutate(x = col + x,
y = row + y)
mygrid
# A tibble: 800 × 7
# Rowwise:
row col tile_id tile x y g
<int> <int> <int> <chr> <dbl> <dbl> <fct>
1 0 0 1 tile3 0 0 2
2 0 0 1 tile3 0 1 2
3 0 0 1 tile3 1 0 2
4 0 0 1 tile3 0 0 2
5 0 0 1 tile3 1 0 1
6 0 0 1 tile3 1 1 1
7 0 0 1 tile3 0 1 1
8 0 0 1 tile3 1 0 1
9 0 1 2 tile4 1 0 2
10 0 1 2 tile4 1 1 2
# ℹ 790 more rows
ggplot(mygrid, aes(x, y)) +
geom_polygon(aes(group = paste(tile_id, g), fill = g), color = "black", linewidth = 2) +
coord_equal() +
# sample two colors randomly
scale_fill_manual(values = sample(colors(), 2))
Arc tiles
library(ggforce)
<- tibble(x0 = 1,
tile1 y0 = 0,
r0 = 0.5,
start = 0,
end = pi/2)
ggplot(tile1, aes(x0, y0)) +
geom_arc_bar(aes(x0 = x0, y0 = y0, r0 = r0, r = 0.6, start = start, end = end))
<- data.frame(
arcs start = seq(0, 2 * pi, length.out = 11)[-11],
end = seq(0, 2 * pi, length.out = 11)[-1],
r = rep(1:2, 5)
)
# Behold the arcs
ggplot(arcs) +
geom_arc_bar(aes(x0 = 0, y0 = 0, r0 = r - 1, r = r, start = start,
end = end, fill = r)) +
coord_equal()