繁体   English   中英

从单点颜色到区域的插值

[英]Interpolate from the color of single points to areas

我有包含一些矩形的 2D 设计,并且我有这些矩形边界的坐标。 我首先计算这些边界之间的墙到墙距离,由数据集中的nn_dist给出,然后将这些边界点绘制为热图,其中颜色代表每个边界点的nn_dist值。

这是绘制它的代码(有关如何计算nn_dist ,请参见此处)。

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)

这是输出的一部分:

在此处输入图像描述

但是,如您所见,大部分绘图看起来都是空的,并且很难从该绘图中获得任何关于矩形之间的墙到墙距离信息的直觉。 结果,我想填充边界之间的空白空间,使其从边界点的颜色中插值。

我怎样才能做到这一点? 最后,输出应该看起来像

在此处输入图像描述

这是我正在使用的数据集的一小部分(约 100,000 行):

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表示边界点的位置
  • nnX, nnY表示不在同一个矩形上的最近邻边界点,
  • nn_distX,YnnX, nnY之间的距离
  • ID每个矩形的唯一 ID
  • uid每个边界点的唯一 ID,无论它们位于哪个矩形

wlog,可以旋转绘图以获得更容易坐标的示例。 让点包含每行任何矩形的一个角点。 Voronoi 分区将创建具有相同最近边缘点的点区域。 这可用于插入边缘颜色:

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))

reprex 包于 2022-05-10 创建 (v2.0.0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM