Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Forked from ramnathv/concaveman.R
Last active August 18, 2019 17:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save timelyportfolio/c29ce1093ff9327014b33fc2c8250050 to your computer and use it in GitHub Desktop.
Save timelyportfolio/c29ce1093ff9327014b33fc2c8250050 to your computer and use it in GitHub Desktop.
Using Concaveman in R using V8 with beeswarm (fork)
license: mit
height: 100

Just for fun, I thought I would fork Ramnath Vaidyanathan's concaveman in R example and extend to use with the beeswarm package. I don't think there is any good use case for it, but perhaps somebody might like.

concaveman <- function(d){
  library(V8)
  ctx <- v8()
  ctx$source('https://www.mapbox.com/bites/00222/concaveman-bundle.js')
  jscode <- sprintf(
    "var points = %s;var polygon = concaveman(points);", 
    jsonlite::toJSON(d, dataframe = 'values')
  )
  ctx$eval(jscode)
  setNames(as.data.frame(ctx$get('polygon')), names(d))
}

library(beeswarm)
library(ggplot2)
library(dplyr)

# example from beeswarm
data(breast)
# play with method - square, hex, center
bsw = beeswarm(time_survival ~ ER, data = breast, method="square")

# transfrom with dplyr
bsw_cvman <- bsw %>%
  data.frame(row.names = NULL) %>%
  group_by(x.orig) %>%
  summarise(
    cvman = list(concaveman(data.frame(x,y)))
  )

Reduce(
  function(left, right) {
    left + 
      geom_path(data=right, aes(x,y))
  },
  bsw_cvman$cvman,
  init = ggplot(bsw, aes(x = x, y = y)) +
    scale_x_discrete(limits = unique(bsw$x.orig))
)
concaveman <- function(d){
library(V8)
ctx <- v8()
ctx$source('https://www.mapbox.com/bites/00222/concaveman-bundle.js')
jscode <- sprintf(
"var points = %s;var polygon = concaveman(points);",
jsonlite::toJSON(d, dataframe = 'values')
)
ctx$eval(jscode)
setNames(as.data.frame(ctx$get('polygon')), names(d))
}
library(beeswarm)
library(ggplot2)
library(dplyr)
# example from beeswarm
data(breast)
# play with method - square, hex, center
bsw = beeswarm(time_survival ~ ER, data = breast, method="square")
# transfrom with dplyr
bsw_cvman <- bsw %>%
data.frame(row.names = NULL) %>%
group_by(x.orig) %>%
summarise(
cvman = list(concaveman(data.frame(x,y)))
)
Reduce(
function(left, right) {
left +
geom_path(data=right, aes(x,y))
},
bsw_cvman$cvman,
init = ggplot(bsw, aes(x = x, y = y)) +
scale_x_discrete(limits = unique(bsw$x.orig))
)
<img src = "https://gist.githubusercontent.com/timelyportfolio/c29ce1093ff9327014b33fc2c8250050/raw/7cdde7b2e6301c5f47a6c008779b40b402709663/thumbnail.png"></img>
@yonicd
Copy link

yonicd commented May 2, 2017

Error in eval(expr, envir, enclos) : object 'y' not found
# need to add variable names to data.frame
bsw_cvman <- bsw %>%
  data.frame(row.names = NULL) %>%
  group_by(x.orig) %>%
  summarise(
    cvman = list(concaveman(data.frame(x=x,y=y))) #<---
  )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment