|
| 1 | +#' Stack |
| 2 | +#' |
| 3 | +#' This function generates an sf object. |
| 4 | +#' |
| 5 | +#' @param x0 x-coordinate of middle of rectangle. |
| 6 | +#' @param y0 y-coordinate of middle of rectangle. |
| 7 | +#' @param min_height minimum height. |
| 8 | +#' @param max_height maximum height. |
| 9 | +#' @param min_width minimum width. |
| 10 | +#' @param max_width maximum width. |
| 11 | +#' @return An sf object. |
| 12 | + |
| 13 | +stack <- function(x0, y0, |
| 14 | + min_height, max_height, |
| 15 | + min_width, max_width) { |
| 16 | + width <- stats::runif(1, min_width, max_width) |
| 17 | + height <- stats::runif(1, min_height, max_height) |
| 18 | + x_vals <- c(x0 - (width / 2), x0 + (width / 2), x0 + (width / 2), x0 - (width / 2), x0 - (width / 2)) |
| 19 | + y_vals <- c(y0 - (height / 2), y0 - (height / 2), y0 + (height / 2), y0 + (height / 2), y0 - (height / 2)) |
| 20 | + square_m <- matrix(c(x_vals, y_vals), byrow = FALSE, ncol = 2) |
| 21 | + square_sf <- sf::st_polygon(list(square_m)) |
| 22 | + sf_polygon <- sf::st_sf(geometry = sf::st_sfc(square_sf)) |
| 23 | + return(sf_polygon) |
| 24 | +} |
| 25 | + |
| 26 | + |
| 27 | + |
| 28 | +#' Stackture |
| 29 | +#' |
| 30 | +#' This function generates a coloured generative art ggplot object using |
| 31 | +#' overlapping semi-transparent circles. |
| 32 | +#' |
| 33 | +#' @param n_x Number of columns in grid. Default 8. |
| 34 | +#' @param n_y Number of rows in grid. Default 8. |
| 35 | +#' @param min_height minimum height. |
| 36 | +#' @param max_height maximum height. |
| 37 | +#' @param min_width minimum width. |
| 38 | +#' @param max_width maximum width. |
| 39 | +#' @param interpolate Boolean indicating if colours should be interpolated. Default TRUE. |
| 40 | +#' @param col_palette Vector of colours. Default `c("#A053A1", "#DB778F", "#E69F52", "#09A39A", "#5869C7")`. |
| 41 | +#' @param bg_col Background colour. Default "#004B67". |
| 42 | +#' @param s Seed value. Default 1234. |
| 43 | +#' @return A ggplot object. |
| 44 | +#' @export |
| 45 | +stackture <- function(n_x = 8, |
| 46 | + n_y = 8, |
| 47 | + min_height = 1, max_height = 1.5, |
| 48 | + min_width = 1, max_width = 1.5, |
| 49 | + interpolate = TRUE, |
| 50 | + col_palette = c("#A053A1", "#DB778F", "#E69F52", "#09A39A", "#5869C7"), |
| 51 | + bg_col = "#004B67", |
| 52 | + s = 1234) { |
| 53 | + set.seed(s) |
| 54 | + # data generation |
| 55 | + plot_grid <- expand.grid(x = 1:n_x, y = 1:n_y) |
| 56 | + all_data <- purrr::map2( |
| 57 | + .x = plot_grid$x, |
| 58 | + .y = plot_grid$y, |
| 59 | + .f = ~ stack( |
| 60 | + x0 = .x, |
| 61 | + y0 = .y, |
| 62 | + min_height = min_height, |
| 63 | + max_height = max_height, |
| 64 | + min_width = min_width, |
| 65 | + max_width = max_width |
| 66 | + ) |
| 67 | + ) |> |
| 68 | + dplyr::bind_rows() |
| 69 | + all_data <- dplyr::slice_sample(all_data, n = nrow(all_data)) |
| 70 | + if (interpolate) { |
| 71 | + all_data$col <- sample(grDevices::colorRampPalette(col_palette)(n_x * n_y)) |
| 72 | + } else { |
| 73 | + all_data$col <- sample(col_palette, size = n_x * n_y, replace = TRUE) |
| 74 | + } |
| 75 | + # plot |
| 76 | + ggplot2::ggplot(data = all_data) + |
| 77 | + ggplot2::geom_sf( |
| 78 | + mapping = ggplot2::aes(fill = col), |
| 79 | + colour = bg_col |
| 80 | + ) + |
| 81 | + ggplot2::scale_fill_identity() + |
| 82 | + ggplot2::theme_void() + |
| 83 | + ggplot2::theme( |
| 84 | + plot.background = ggplot2::element_rect( |
| 85 | + fill = bg_col, colour = bg_col |
| 86 | + ), |
| 87 | + panel.background = ggplot2::element_rect( |
| 88 | + fill = bg_col, colour = bg_col |
| 89 | + ), |
| 90 | + plot.margin = ggplot2::margin(0, 0, 0, 0) |
| 91 | + ) |
| 92 | +} |
0 commit comments