1

Reproducible example:

    set.seed(123)
    x <- sample(1:30,9)
    y <- sample(1:30,9)
    points <- data.frame(x, y,
                         distance = sample(c("apples","pears","banana"), 9, replace = T))

# Plot
    ggplot(points) +
      geom_voronoi(aes(x=x,y=y,fill=distance)) +
      stat_voronoi(aes(x=x,y=y),geom = "path") +
      geom_point(aes(x=x,y=y))

makes:

great

I wonder if I can limit size of the Voronoi cell, to have a maximum distance from the x,y it is using to plot. I knocked this up in paint:

some

Some may call that art.

Where the cells can only be so big if they do not intersect with neighboring cells. Any ideas.

Why? I am using GPS data to create cells, then coloring these by a factor. The GPS coordinates are not consistent spatially and I do not want to give the reader false impression. In my real data some of the cells are rather large.

Jim
  • 558
  • 4
  • 13

1 Answers1

2

It seems like you're using a slightly different version ggforce, but in 0.3.1 you can set the size with the max_radius parameter:

library(ggforce)
#> Warning: package 'ggforce' was built under R version 3.6.2
#> Loading required package: ggplot2
library(ggplot2)
set.seed(123)
x <- sample(1:30,9)
y <- sample(1:30,9)
points <- data.frame(x, y,
                     distance = sample(c("apples","pears","banana"), 9, replace = T))

# Plot
ggplot(points, aes(x, y, group = -1L)) +
  geom_voronoi_tile(aes(fill=distance),
                    max.radius = 10,
                    colour = "black") +
  geom_point(aes(x=x,y=y))

Created on 2020-04-14 by the reprex package (v0.3.0)

teunbrand
  • 33,645
  • 4
  • 37
  • 63
  • Ah, I had not come across the `ggforce` package! seems like it is useful for a few other plots too. thanks! Jim – Jim Apr 14 '20 at 13:02
  • Could be a separate question, but would you not how to convert to Manhattan distance? – Jim Apr 14 '20 at 13:11
  • To get rectangular outer edges you mean? No sorry I'm afraid I wouldn't know of an argument in ggforce that would set that. – teunbrand Apr 14 '20 at 13:25
  • Sure. Worth a pop. I have extensively looked into and without going converting them to polygons and messing around with smoothing etc. Many thanks for your help! – Jim Apr 14 '20 at 13:34