用颜色梯度填充Voronoi多边形?

发布于 2025-01-21 18:48:49 字数 2099 浏览 0 评论 0原文

我一直在尝试用 raster grob 对象填充Voronoi多边形/瓷砖,但是我无法找到一种方法。 This is a sample of the code for the Voronoi ggplot

library(ggplot2) 
library(grid)
library(RColorBrewer)
library(ggmap)
library(ggvoronoi)
library(raster)

    # Get the location of the city in the map
    bcn_map <- get_googlemap(center = "Plaza España, Barcelona",zoom = 14,key="xxx")
    bounds <- as.numeric(attr(bcn_map,"bb"))
    set.seed(500)
    num <- geocode("Plaza España, Barcelona")
   # Create a dummy data frame with random values 
    df <- round(data.frame(
      x = jitter(rep(num$lon,200), amount = .03),
      y = jitter(rep( num$lat,200), amount = .03)
    ), digits = 5)
    
    # Plot the map with the Voronoi layer
    ggmap(bcn_map,base_layer = ggplot(data=df,aes(x,y))) +
      coord_map(ylim=bounds[c(1,3)],xlim=bounds[c(2,4)]) +
      theme_minimal() +
      theme(axis.text=element_blank(),
            axis.title=element_blank())+
      geom_path(stat="voronoi",alpha=0.2 ,size=2,colour = "pink") +
      geom_point(color="blue",size=.25)

And this is the raster object I want to include, fitting the tiles in the Voronoi diagram:

make_gradient <- function(deg = 45, n = 100, cols = blues9) {
  cols <- colorRampPalette(cols)(n + 1)
  rad <- deg / (180 / pi)
  mat <- matrix(
    data = rep(seq(0, 1, length.out = n) * cos(rad), n),
    byrow = TRUE,
    ncol = n
  ) +
    matrix(
      data = rep(seq(0, 1, length.out = n) * sin(rad), n),
      byrow = FALSE,
      ncol = n
    )
  mat <- mat - min(mat)
  mat <- mat / max(mat)
  mat <- 1 + mat * n
  mat <- matrix(data = cols[round(mat)], ncol = n)
  grid::rasterGrob(
    image = mat,
    width = unit(1, "npc"),
    height = unit(1, "npc"), 
    interpolate = TRUE
  )
}

#Set pallete
cc <- palette(c("#333C83","#F24A72","white","#FDAF75"))

# Create gradient with the colors defined before
raster.grob <- make_gradient(
  deg = 90, n = 100, cols = cc
)

I don't know if this is possible in R, but thanks in advance for the help.

I have been trying to fill Voronoi polygons/tiles with raster grob objects, but I have not been able to find a way to do it. This is a sample of the code for the Voronoi ggplot

library(ggplot2) 
library(grid)
library(RColorBrewer)
library(ggmap)
library(ggvoronoi)
library(raster)

    # Get the location of the city in the map
    bcn_map <- get_googlemap(center = "Plaza España, Barcelona",zoom = 14,key="xxx")
    bounds <- as.numeric(attr(bcn_map,"bb"))
    set.seed(500)
    num <- geocode("Plaza España, Barcelona")
   # Create a dummy data frame with random values 
    df <- round(data.frame(
      x = jitter(rep(num$lon,200), amount = .03),
      y = jitter(rep( num$lat,200), amount = .03)
    ), digits = 5)
    
    # Plot the map with the Voronoi layer
    ggmap(bcn_map,base_layer = ggplot(data=df,aes(x,y))) +
      coord_map(ylim=bounds[c(1,3)],xlim=bounds[c(2,4)]) +
      theme_minimal() +
      theme(axis.text=element_blank(),
            axis.title=element_blank())+
      geom_path(stat="voronoi",alpha=0.2 ,size=2,colour = "pink") +
      geom_point(color="blue",size=.25)

And this is the raster object I want to include, fitting the tiles in the Voronoi diagram:

make_gradient <- function(deg = 45, n = 100, cols = blues9) {
  cols <- colorRampPalette(cols)(n + 1)
  rad <- deg / (180 / pi)
  mat <- matrix(
    data = rep(seq(0, 1, length.out = n) * cos(rad), n),
    byrow = TRUE,
    ncol = n
  ) +
    matrix(
      data = rep(seq(0, 1, length.out = n) * sin(rad), n),
      byrow = FALSE,
      ncol = n
    )
  mat <- mat - min(mat)
  mat <- mat / max(mat)
  mat <- 1 + mat * n
  mat <- matrix(data = cols[round(mat)], ncol = n)
  grid::rasterGrob(
    image = mat,
    width = unit(1, "npc"),
    height = unit(1, "npc"), 
    interpolate = TRUE
  )
}

#Set pallete
cc <- palette(c("#333C83","#F24A72","white","#FDAF75"))

# Create gradient with the colors defined before
raster.grob <- make_gradient(
  deg = 90, n = 100, cols = cc
)

I don't know if this is possible in R, but thanks in advance for the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文