Here I experiment with creating contours of Truchet tiles after seeing some really cool examples (see: https://twitter.com/_sayo_y/status/1488504770755440640).
I will use this packages:
library(metR) # For contouring library(rayshader) library(sf) library(tidyverse) library(truchet)
Mosaic:
xlim <- c(0, 6) ylim <- c(0, 6) # Create a data frame with the spots for tiles container <- expand.grid(x = seq(xlim[1], xlim[2], 1), y = seq(ylim[1], ylim[2], 1)) %>% filter(x != 0 | y != 0) %>% mutate(tiles = sample(c("+", "+.", "x."), n(), replace = TRUE), scale_p = 1)
Assemble mosaic:
mosaic <- st_truchet_ms(df = container)
Create plot:
ggplot() + geom_sf(data = mosaic, aes(fill = factor(color)), color = NA) + scale_fill_manual(values = c("black", "white")) + theme_void() + theme(legend.position = "none")
Summarize by color to give individual pieces made of compact segments of mosaic by color:
mosaic_2 <- mosaic %>% dplyr::group_by(.data$color) %>% dplyr::summarize(color = max(.data$color)) sf::st_agr(mosaic_2) <- "constant" # Obtain the difference of mosaics of color 1 with respect to 2 mosaic_3 <- mosaic_2[1,] %>% sf::st_difference(mosaic_2[2,]$geometry) %>% sf::st_set_agr("constant") %>% sf::st_cast(to = "POLYGON") %>% dplyr::mutate(area = sf::st_area(.data$geometry)) # Cast the multipolygon of the opposite color to individual polygons mosaic_4 <- mosaic_2[2,] %>% sf::st_set_agr("constant") %>% sf::st_cast(to = "POLYGON") %>% dplyr::mutate(area = sf::st_area(.data$geometry)) # Bind both colors mosaic <- rbind(mosaic_3, mosaic_4)
Create gg object:
ggmosaic <- ggplot() + geom_sf(data = mosaic, aes(fill = -(color - 1)), color = "white") + scale_fill_gradient(low = "lightgray", high = "black") + theme(legend.position = "none", panel.background = element_rect(fill = NA), text = element_blank(), panel.grid = element_blank()) ggmosaic
Birds-eye view:
pdf("rayrendered-tiles.pdf") plot_gg(ggmosaic, width = 5, height = 5, offset_edges = TRUE, raytrace = TRUE, preview = TRUE) dev.off()
plot_gg(ggmosaic, width = 5, height = 5, multicore = TRUE, scale = 100, zoom = 0.7, theta = 45, phi = 45, windowsize = c(800, 800)) Sys.sleep(0.2) render_snapshot(clear = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.