Home > Net >  Plotting points in voronoi respective to x and y coords to join an additional coordinate
Plotting points in voronoi respective to x and y coords to join an additional coordinate

Time:06-07

I am working on plotting a voronoi point between two coordinates. So that the point aligns with respective to the lower and upper values in the dataframe.

Unfortunately, when I plot the data, it linearly increases by the value of the points. This then produces the following plot:

enter image description here

Here's the script that I used:

library(ggplot2)
library(ggvoronoi)
ggplot(voronoi_data, aes(x=lower, y=upper))   stat_voronoi()  geom_point(aes(fit))

ggplot(voronoi_data, aes(x = lower, y = upper))  
  stat_voronoi(
    geom = "path",
    color = 4,
    lwd = 0.7,
    linetype = 1
  )   geom_point(aes(fit))


Some points do not lie within the voronoi diagram because they are either too large or too small. Therefore, I have thought of incrementing the upper and lower value by fit, and layering two voronoi over one another with two different colour schemes and then plotting the fit values (this is currently under process as I just had this idea.)

The update on the progress idea:


voronoi_1 <-
  mapply(function(a, b)
    a - b, voronoi_data$lower, voronoi_data$fit) %>% data.frame(lower_add =
                                                                  .)
voronoi_1 <-
  rbind(
    voronoi_1,
    mapply(function(a, b)
      a   b, voronoi_data$lower, voronoi_data$fit) %>% data.frame(lower_add =
                                                                    .)
  )

voronoi_2 <-
  mapply(function(a, b)
    a   b, voronoi_data$upper, voronoi_data$fit) %>% data.frame(upper_add =
                                                                  .)
voronoi_2 <-
  rbind(
    voronoi_2,
    mapply(function(a, b)
      a - b, voronoi_data$upper, voronoi_data$fit) %>% data.frame(upper_add =
                                                                    .)
  )

voronoi_3 <- rep(voronoi_data$fit, 2) %>% data.frame(fit = .)
voronoi_update <- cbind(voronoi_1, voronoi_2, voronoi_3)

ggplot(voronoi_data, aes(x = lower, y = upper))  
  stat_voronoi(
    geom = "path",
    color = 4,
    lwd = 0.7,
    linetype = 1
  )   geom_point(aes(fit), col = 'blue')   stat_voronoi(
    data = voronoi_update,
    aes(x = lower_add, y = upper_add),
    geom = "path",
    color = 2,
    lwd = 0.7,
    linetype = 1
  )   geom_point(col = 'green')   geom_segment(aes(
    x = lower,
    y = upper,
    xend = fit,
    yend = fit
  ))

Update produces the following picture:

enter image description here

How can I get the green-dots to connect with the blue-dots?

reproducible data:

structure(list(lower = c(-50.231394143356, -56.2551026846824, 
28.4249214917657, -72.7725910398994, -81.2658846682781, 21.6407972918016, 
-6.38857800084765, -83.9469403037355, -7.49345446155375, -9.25035611734441
), upper = c(83.3536041213786, 63.9866816320508, 145.329559457229, 
48.0937531194102, 42.462115738722, 136.959651947817, 100.752432092854, 
40.2250494139988, 110.279218627158, 107.6295802627), fit = c(16.5611049890113, 
3.86578947368421, 86.8772404744972, -12.3394189602446, -19.4018844647781, 
79.3002246198091, 47.1819270460033, -21.8609454448684, 51.3928820828022, 
49.1896120726776)), class = "data.frame", row.names = c(NA, -10L
))

CodePudding user response:

The reason why your points aren't lying in the Voronoi is that you have put aes(fit) inside geom_point. This is interpreted as the x axis value for the points, which presumably isn't what you want:

ggplot(voronoi_data, aes(x = lower, y = upper))  
  stat_voronoi(
    geom = "path",
    color = 4,
    lwd = 0.7,
    linetype = 1
  )   geom_point()

enter image description here

  • Related