scratch/Veroni Cuttouts.R

library(tidyverse)
library(ggforce)


circle <- function(points = 10000, r = 30) {
  tibble(angle = seq(0, 2*pi, length.out = points), x = r*cos(angle), y = r*sin(angle)) %>%
    mutate(id = 1:nrow(.)) %>%
    select(id, x, y)
}


df <- ambient::long_grid(c(3,5,8), c(1,2,3))



df <- tribble(
  ~x,  ~y,  ~fill,     ~group,
   3,   1,    "Red",    -1L,
   3,   2,    "Green",  -1L,
   3,   3,    "Red",   -2L,
   5,   1,    "Blue",   -2L,
   5,   2,    "Red",    -1L,
   5,   3,    "Green",  -1L,
   8,   1,    "Blue",   -2L,
   8,   2,    "Red",    -1L,
   8,   4,    "Green",  -1L,
   4,   1,    "Red",    -1L,
   2,   2,    "Green",  -1L,
   3.5, 3,    "Blue",   -2L,
   7,   1,    "Red",   -2L,
   5,   2.5,  "Red",    -1L,
   5.5, 2,    "Green",  -1L,
   6,   .5,   "Red",   -2L,
   3,   2.5,  "Red",    -1L,
   4,   4.25, "Green",  -1L

)


clear <- expand.grid(
  x=seq(3.5, 7.5, .01),
  y=seq(1.5, 2.5, .01)
) %>%
mutate(fill = "Blue", group = 1L)

set.seed(42)
clear <- sample_n(clear, 100)

df %>%
  rbind(clear)%>%
  ggplot( aes(x,y, group = -1L))+
  geom_voronoi_tile(aes(fill = fill),
                    expand = unit(-.5, 'mm'), radius = unit(2, 'mm'))+
  theme_canvas()+
  delabj::scale_color_delabj()+
    delabj::scale_fill_delabj()-> plot

  plot$coordinates
plot_grob <- ggplotGrob(plot)


ggplot_build(plot) %>% .$data %>% .[[1]] -> brounds

layer_data(plot, 1L) %>%
  filter(fill== "#83B692") %>%
  select(group) %>%
  distinct()


ploygon_maybe_1 <- layer_data(plot, 1L) %>%
  filter(group == "1:1")

ploygon_maybe_2 <- layer_data(plot, 1L) %>%
  filter(group == "10:1")

ploygon_maybe_3 <- layer_data(plot, 1L) %>%
  filter(group == "17:1")

#######################
val_in_1 <- sp::point.in.polygon(plot_data_4$x,
                     plot_data_4$y,
                     pol.x = ploygon_maybe_1$x, pol.y = ploygon_maybe_1$y, )
val_in_2 <- sp::point.in.polygon(plot_data_4$x,
                                 plot_data_4$y,
                                 pol.x = ploygon_maybe_2$x,
                                 pol.y = ploygon_maybe_2$y )

val_in_3 <- sp::point.in.polygon(plot_data_4$x,
                                 plot_data_4$y,
                                 pol.x = ploygon_maybe_3$x,
                                 pol.y = ploygon_maybe_3$y )

#############
plot_data_4 %>%
  filter(val_in_1==1) -> test_data1

plot_data_4 %>%
  filter( val_in_2==1) -> test_data2

plot_data_4 %>%
  filter( val_in_3==1) -> test_data3
################

  ggplot()+
    geom_voronoi_tile(
      data=df%>%
        rbind(clear),
      aes(x, y, group = -1L, fill = fill),
      expand = unit(-.5, 'mm'),
      radius = unit(2, 'mm'))+
    geom_point(
      data = test_data1,
      aes(x,y),
      color="#1E1E1E",
      alpha=.05,
      shape=46)+
  geom_point(
    data = test_data2,
    aes(x,y),
    color="#1E1E1E",
    alpha=.05,
    shape=46)+
  geom_point(
    data = test_data3,
    aes(x,y),
    color="#1E1E1E",
    alpha=.05,
    shape=46)+
    geom_polygon(data = test_data2,
                 aes(x,y),
                 alpha =.75,
                 fill = "#5B3758")+
    geom_polygon(data = test_data3,
                 aes(x,y),
                 alpha =.75,
                 fill = "#5B3758")+
    geom_polygon(
      aes(x,y),
      data= test_data1,
      alpha =.75,
      fill = "#5B3758")+

    theme_canvas()+
    delabj::scale_color_delabj()+
    delabj::scale_fill_delabj()
    scale_fill_manual(values = c("#721121", "#2660A4", "white"))->plot2

      ggplot_build(plot)%>% .$data %>% .[[1]] %>%
        filter(fill == "#83B692")->polygon_df

      sfheaders::sf_polygon(polygon_df, x="x", "y", polygon_id = "group")->polygons

      ?sf::st_crop()

      ggplot() +
        geom_voronoi_tile(
          data=df%>%
            rbind(clear),
          aes(x, y, group = -1L, fill = fill),
          expand = unit(-.5, 'mm'),
          radius = unit(2, 'mm'))+
        geom_sf(data=polygons, fill = "white", color = NA)+
        theme_canvas()


plot_data_4 %>%
  sf::st_geometry()
  sf::st_as_sf(plot_data_4)





readr::read_csv("demo2.csv", na = "NA")-> test_veroni


test_veroni %>%
  na.omit() %>%
  ggplot()+
  aes(X,-Y, color = Group, fill = Group)+
  ggalt::geom_encircle( na.rm = TRUE,
                      expand = unit(-.25, 'mm')
                      )+
  #geom_point()+
  theme_canvas()+
  delabj::scale_fill_delabj()+
  delabj::scale_color_delabj()-> outlines


ggplot_build(outlines) %>% .$data %>% .[[1]] -> bounds

bounds %>%
  filter(fill == "#721121") -> shape1


shape1 %>%
  ggplot(aes(x,y, group = fill))+
  geom_point()+
  geom_voronoi_tile()
delabj/genArt documentation built on March 25, 2021, 11:56 p.m.