Skip to content

Instantly share code, notes, and snippets.

@timelyportfolio
Forked from jalapic/gana.csv
Last active Aug 29, 2015
Embed
What would you like to do?
Heatmap of touches (in rbokeh)

@jalapic did these really nice set of touch heatmap charts. On Twitter, there was a discussion how to do this interactively with d3.js or some other JavaScript. I definitely have some ideas how to accomplish this, but for now let's see how we can kind of do it in rbokeh. There is just a small issue with palette selection in Bokeh that does not let us allow a no color that prevents us from getting really close.

## https://gist.github.com/jalapic/3616c0197ece24060e99
library(rbokeh)
library(dplyr)

gana <- read.csv("https://gist.githubusercontent.com/jalapic/3616c0197ece24060e99/raw/2bc2361c5fceb9be80dece9b7ffa14d129a3edfb/gana.csv")

figure() %>%
  ly_image(
    ggb$data[[1]][,c("x","y","density")] %>%
      mutate( z = round( density * 1e6 ) ) %>%
      select( x, y, z ) %>%
      spread( x, z ) %>%
      as.matrix
    ,
  )

grDevices:::.smoothScatterCalcDensity(gana,nbin=200) %>>%
  (ss~
     ly_image(
       figure( width = 694, height = 460, xlim = c(0,100), ylim = c(0,100) )
       ,ss$fhat
       ,y = min(ss$x1)
       ,x = min(ss$x2)
       ,dh = max(ss$x1) - min(ss$x1)
       ,dw = max(ss$x2) - min(ss$x2)
     )
  ) %>>%
  ly_rect( xleft = 0, xright = 100, ybottom = 0, ytop = 100, line_color = "#000000", fill_alpha = 0 ) %>>%
  ly_rect( xleft = 0, xright = 50, ybottom = 0, ytop = 100, line_color = "#000000", fill_alpha = 0 ) %>>%
  ly_rect( xleft = 17, xright = 0, ybottom = 21, ytop = 79, line_color = "#000000", fill_alpha = 0 ) %>>%
  ly_rect( xleft = 83, xright = 100, ybottom = 21, ytop = 79, line_color = "#000000", fill_alpha = 0 ) %>>%
  ly_rect( xleft = 0, xright = 6, ybottom = 36.8, ytop = 63.2, line_color = "#000000", fill_alpha = 0 ) %>>%
  ly_rect( xleft = 100, xright = 94, ybottom = 36.8, ytop = 63.2, line_color = "#000000", fill_alpha = 0 )
x y
2 90
7 15
15 3
18 58
20 20
19 33
36 4
36 5
37 19
38 23
39 24
43 25
44 75
46 59
47 37
47 27
48 27
48 90
49 85
46 85
52 80
54 75
56 55
59 70
55 40
58 9
65 7
60 37
59 40
66 40
64 41
62 50
66 55
67 78
69 74
70 70
71 55
72 56
74 88
75 88
76 85
72 65
70 55
75 50
78 85
95 95
81 21
82 5
95 20
@timelyportfolio
Copy link
Author

timelyportfolio commented Aug 24, 2015

image

@jalapic
Copy link

jalapic commented Aug 24, 2015

Very nice! I like how with this rbokeh density you can really get much more specificity as to which areas have high vs low vs zero touches. As for the d3, I was thinking of doing something maybe with leaflet.heat that is often used with maps - e.g. http://bl.ocks.org/d3noob/8973028 Will be interested to see if you come up with anything ! Awesome.

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