Interpolate from the color of single points to areas

Viewed 47

I have 2D design containing some rectangles, and I have the coordinates of the boundaries of those rectangles. I'm first calculating the wall-to-wall distance between these boundaries, given by nn_dist in the dataset, and then plotting these boundary points as a heatmap, where the color represents the nn_dist value of each boundary point.

Here is the code to plot that (see here for how nn_dist is calculated).

ggplot(dt, aes(x=Y, y=X, z=wall_to_wall_dist)) +
  stat_summary_hex(fun=min, bins=500)+
  #geom_raster(aes(fill = wall_to_wall_dist), interpolate = TRUE)+
  #geom_tile()+
  xlim(c(0,10000))+
  ylim(c(0,3800))+
  scale_fill_gradientn(colors = myPalette, limits = c(100,300))+
  theme_default(s_aspectRatio=3800/10000, text_font_size = fontSize)

Here a section from the output:

enter image description here

However, as you can see, most of the plot look empty and it is hard get any intuition from this plot about the wall-to-wall distance information between the rectangles. As a result, I want to fill the empty space between the boundaries in a way that it is interpolated from the color of the boundary points.

How can I do this? In the end the output should, sort of, look like

enter image description here

Here is a tiny part of the dataset (~100,000 rows) that I'm using:

structure(list(ID = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), X = c(318L, 317L, 316L, 
315L, 314L, 313L, 312L, 311L, 310L, 309L, 1273L, 1272L, 1271L, 
1270L, 1269L, 1268L, 1267L, 1266L, 1265L, 1264L), Y = c(1L, 2L, 
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L), t = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1, 1, 1, 1, 1), uid = c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 
10L, 1271L, 1272L, 1273L, 1274L, 1275L, 1276L, 1277L, 1278L, 
1279L, 1280L), nnX = c(1264L, 1264L, 1264L, 1264L, 1264L, 1264L, 
1264L, 1264L, 1264L, 1264L, 318L, 318L, 318L, 318L, 318L, 318L, 
318L, 318L, 318L, 318L), nnY = c(10L, 10L, 10L, 10L, 10L, 10L, 
10L, 10L, 10L, 10L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 
    wall_to_wall_dist = c(946.042810870629, 947.033790315847, 
    948.025843529595, 949.018967144493, 950.013157803617, 951.008412160481, 
    952.004726879021, 953.002098633576, 954.000524108871, 955, 
    955, 954.000524108871, 953.002098633576, 952.004726879021, 
    951.008412160481, 950.013157803617, 949.018967144493, 948.025843529595, 
    947.033790315847, 946.042810870629)), row.names = c(NA, -20L
), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x7fce30813ce0>)
  • X,Y represent the location of the boundary points
  • nnX, nnY represent the nearest neighbour boundary point that is not on the same rectangle, see
  • nn_dist is the distance between X,Y and nnX, nnY
  • ID a unique id for each rectangle
  • uid a unique id for each boundary point, regardless of the rectangle they are on
1 Answers

w.l.o.g., one can rotate the plot to have an example with easier coordinates. Let points contain one corner point of any rectangle per row. The Voronoi partition will create areas of points with the same the nearest edge point. This can be used to interpolate the edge colors:

remotes::install_github("garretrc/ggvoronoi")

library(ggvoronoi)
#> Loading required package: ggplot2
library(tidyverse)

# corner points of the rectangles
points <- tribble(
  ~x, ~y, ~rect,
  0, 0, "A",
  1, 0, "A",
  1, 1, "A",
  0, 1, "A",
  2, 0, "B",
  3, 0, "B",
  2, 1, "B",
  3, 1, "B"
)

rects <-
  points %>%
  group_by(rect) %>%
  summarise(
    xmin = min(x),
    xmax = max(x),
    ymin = min(y),
    ymax = max(y)
  )
rects
#> # A tibble: 2 × 5
#>   rect   xmin  xmax  ymin  ymax
#>   <chr> <dbl> <dbl> <dbl> <dbl>
#> 1 A         0     1     0     1
#> 2 B         2     3     0     1

points %>%
  mutate(rect = rect %>% factor()) %>%
  ggplot() +
  geom_voronoi(aes(x, y, fill = rect)) +
  geom_rect(
    data = rects,
    mapping = aes(xmin = xmin, ymin = ymin, xmax = xmax, ymax = ymax),
    fill = "white"
  ) +
  geom_point(aes(x, y))

Created on 2022-05-10 by the reprex package (v2.0.0)

Related