Little useless-useful R functions – Interactive Voronoi diagram generator using R and x11()

Yes. Finally, the Voronoi diagrams with the use of x11() function. This diagram is presentation of a plane that is partitioned every time, a user clicks on the canvas of x11. This plane is partitioned into smaller regions that are close to given set of points.

Partitioning into smaller regions or convex polygons happens in such manner that each polygon contains only one generating point and every point in a given polygon is closer to its generating point than to any other.

This is certainly very useful for many fields of computer science and programming, graphics, geoanalysis, analysis of space, meteorology and so on. And also useful for this series of useless-useful functions 🙂

With R we have many packages available, but this function is focused on end-user and clicking on the canvas that partitions the plane into smaller convex regions or polygons.

The useless R code is in gist only ggplot2 function that generates the voronoi diagram and function to navigate mouse clicks on x11() interface


# packages
library(deldir)
library(ggplot2)

dff <- data.frame(NULL,NULL,NULL)

#### Graph
voronoiGraphBoard <- function(){
  r <- ggplot(data=dff, aes(x=xl,y=yl)) +
    geom_segment( aes(x = x1, y = y1, xend = x2, yend = y2), size = 1, data = voronoi$dirsgs, linetype = 1, color= "orange") +
    geom_point( shape=21, size = 3, color="red", fill="blue") +
    theme_void()
  
 return(r)
}

### Clicking on canvas
click <- function(DefaultGraph=voronoiGraphBoard(), steps=st){
    DefaultGraph <- plot.new()
      for (n in 1:steps) {
      mouse.at <- locator(n = 1, type = "o") 
      xl <- mouse.at$x
      yl <- mouse.at$y
      distance <- sqrt((xl-100)^2 + (yl-100)^2)
      df <- data.frame(xl,yl, distance)
      dff <<- rbind(dff, df)
      if (nrow(dff)>=2){
        voronoi <<- deldir(dff$xl, dff$yl)
        DefaultGraph <- voronoiGraphBoard()
        print(DefaultGraph)
      }
  }
}

#### Start with x11 
Draw_x11 <- function(st){
  x11()
  click(steps=st)
  DefaultGraph <<- voronoiGraphBoard()
}

And the rest is just running the main function:

# Generate Voronoi with defined steps
Draw_x11(st=20)

As always, code is available in at the Github in same Useless_R_function repository.

Happy R-coding!

Tagged with: , , , , , , ,
Posted in Uncategorized, Useless R functions
2 comments on “Little useless-useful R functions – Interactive Voronoi diagram generator using R and x11()
  1. […] Tomaz Kastrun creates a Voronoi diagram: […]

    Liked by 1 person

  2. […] by data_admin [This article was first published on R – TomazTsql, and kindly contributed to R-bloggers]. (You can report issue about the content on this page […]

    Liked by 1 person

Leave a comment

Follow TomazTsql on WordPress.com
Programs I Use: SQL Search
Programs I Use: R Studio
Programs I Use: Plan Explorer
Rdeči Noski – Charity

Rdeči noski

100% of donations made here go to charity, no deductions, no fees. For CLOWNDOCTORS - encouraging more joy and happiness to children staying in hospitals (http://www.rednoses.eu/red-noses-organisations/slovenia/)

€2.00

Top SQL Server Bloggers 2018
TomazTsql

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Discover WordPress

A daily selection of the best content published on WordPress, collected for you by humans who love to read.

Revolutions

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

tenbulls.co.uk

tenbulls.co.uk - attaining enlightenment with the Microsoft Data and Cloud Platforms with a sprinkling of Open Source and supporting technologies!

SQL DBA with A Beard

He's a SQL DBA and he has a beard

Reeves Smith's SQL & BI Blog

A blog about SQL Server and the Microsoft Business Intelligence stack with some random Non-Microsoft tools thrown in for good measure.

SQL Server

for Application Developers

Business Analytics 3.0

Data Driven Business Models

SQL Database Engine Blog

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

Search Msdn

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

Tomaz doing BI and DEV with SQL Server and R, Python, Power BI, Azure and beyond

R-bloggers

R news and tutorials contributed by hundreds of R bloggers

Data Until I Die!

Data for Life :)

Paul Turley's SQL Server BI Blog

sharing my experiences with the Microsoft data platform, SQL Server BI, Data Modeling, SSAS Design, Power Pivot, Power BI, SSRS Advanced Design, Power BI, Dashboards & Visualization since 2009

Grant Fritchey

Intimidating Databases and Code

Madhivanan's SQL blog

A modern business theme

Alessandro Alpi's Blog

DevOps could be the disease you die with, but don’t die of.

Paul te Braak

Business Intelligence Blog

Sql Insane Asylum (A Blog by Pat Wright)

Information about SQL (PostgreSQL & SQL Server) from the Asylum.

Gareth's Blog

A blog about Life, SQL & Everything ...