r - ggmap 中带有自定义颜色代码的 Voronoi 曲面分割?

标签 r ggplot2 ggmap voronoi ggforce

我一直在尝试在 ggmap 中绘制 voronoi 曲面分割,其中每个 block 的颜色将给出十六进制代码,例如 #FFCC00。到目前为止我想出的代码如下:

library(ggmap)
library(ggforce)

b <- get_map(c(2.09174, 50.52550, 7.36819, 53.68320),
             maptype = "toner",
             source = "stamen",
             zoom = 8)

lon <- c(3.76779, 5.31313, 3.48031, 3.90727, 4.15682)
lat <- c(51.2219, 52.0808, 50.7684, 51.2684, 50.9502)
hex_col <- c("#5A586E", "#47967F", "#4EB22E", "#9E82C5", "#ADCFAD")
to_plot <- data.frame(lon, lat, hex_col)


ggmap(b, base_layer = ggplot(data = to_plot,
                             aes(x = lon,
                                 y = lat))) +
  geom_voronoi_tile(aes(fill = hex_col)) +
  scale_fill_identity() +
  geom_voronoi_segment()

但是,当我添加 fill = hex_col 参数时,会出现错误警告:

Warning message:
Computation failed in `stat_voronoi_tile()`:
There is at most one point, data or dummy, inside
the given rectangular window. Thus there are
insufficiently many points to triangulate/tessellate. 

我不确定如何修复,因为在添加参数之前, map 显示没有错误。因此我的问题是:如何将自定义颜色编码的 voronoi 曲面分割添加到 ggmap 上?

提前致谢!

最佳答案

问题可能是 geom_voronoi_tile 期望 voronoi 多边形是闭合的,并且您的数据集缺少外部边界。 一个快速的替代方法是回退到 ggvoronoi::geom_voronoi()。

library(ggmap)
library(ggforce)
library(ggvoronoi)
library(tidyverse)

b <- get_map(c(2.09174, 50.52550, 7.36819, 53.68320),
             maptype = "toner",
             source = "stamen",
             zoom = 8)

lon <- c(3.76779, 5.31313, 3.48031, 3.90727, 4.15682)
lat <- c(51.2219, 52.0808, 50.7684, 51.2684, 50.9502)
hex_col <- c("#5A586E", "#47967F", "#4EB22E", "#9E82C5", "#ADCFAD")
to_plot <- data.frame(lon, lat, hex_col)

ggmap(b, base_layer = ggplot(data = to_plot,
                             aes(x = lon,
                                 y = lat))) +
  geom_voronoi(mapping = aes(fill = hex_col), alpha = 0.5) +
  scale_fill_identity() +
  geom_voronoi_segment()

enter image description here

关于r - ggmap 中带有自定义颜色代码的 Voronoi 曲面分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66019446/

相关文章:

python - Plotly 中条形图的单独标记条

r - 多个图例的标题与 ggplot2 中的位置 ="bottom"不对齐

R ggplot 循环内带有变量

r - 为什么地理编码总是返回错误的地址,但谷歌地图工作正常

r - ggplot : labeling x axis in lineplot

r - 如何在ggplot直方图中添加均值和众数?

R:大向量的高效迭代子集和过滤

r - "Unitless",定性或相对轴刻度ggplot2

r - 向 ggmap 添加指北针和比例尺的简约方法

r - 使用点、分面和每个分面适当缩放来创建 ggmap?