2

I have a table composed by the following data

frame,X,Y

which is the resulting data from several eye tracking analysis. Now I would like to create a Heatmap using R, like the following enter image description here

I tried several script found online, none of them gave me that result.

How can I do?


Here some sample data Ignore the first two columns

task,visualization,frame,X,Y
1,b,1,383,221
1,b,1,632,356
1,b,1,947,663
1,b,1,546,206
1,b,1,488,272    
1,b,1,578,752
1,b,1,415,261
1,b,1,693,158
1,b,1,684,528
1,b,1,592,67
1,b,1,393,180
1,b,1,1033,709
1,b,1,1080,739
1,b,1,711,523
1,b,1,1246,49
1,b,1,742,69
1,b,1,601,370
1,b,10,902,684
1,b,10,517,241
1,b,10,583,86
1,b,10,582,754
1,b,10,426,257
1,b,10,575,229
1,b,10,697,150
1,b,10,379,520
1,b,10,390,286
1,b,10,618,396
1,b,10,710,143
1,b,10,383,188
1,b,10,1026,713
1,b,10,1078,625
1,b,10,713,521
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
user1384636
  • 481
  • 6
  • 17
  • 1
    Please make your situation reproducible, i.e. provide us with the data and the code needed to mimic your situation. See http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example for more tips on how to do this. – Paul Hiemstra Apr 20 '13 at 09:11
  • You somehow need to "georeference" your image, and it's "simple" kerneling from there. – Roman Luštrik Apr 20 '13 at 09:27

1 Answers1

8

You can get this type of plot quite easily using stat_bin2d from ggplot2:

library(ggplot2)
ggplot(dat, aes(x = X, y = Y)) + stat_bin2d(bins = 10)

enter image description here

This does simple binning, as @RomanLustrik suggested you could also perform some kind of kernel smoothing. This can also be done using ggplot2:

ggplot(dat, aes(x = X, y = Y)) + 
 stat_density2d(geom = "tile", aes(fill = ..density..), contour = FALSE) + 
 geom_point()

enter image description here Note that dat is the example data you gave, geting your data into a data.frame:

dat = read.table(textConnection("task,visualization,frame,X,Y
    1,b,1,383,221
    1,b,1,632,356
    1,b,1,947,663
    1,b,1,546,206
    1,b,1,488,272    
    1,b,1,578,752
    1,b,1,415,261
    1,b,1,693,158
    1,b,1,684,528
    1,b,1,592,67
    1,b,1,393,180
    1,b,1,1033,709
    1,b,1,1080,739
    1,b,1,711,523
    1,b,1,1246,49
    1,b,1,742,69
    1,b,1,601,370
    1,b,10,902,684
    1,b,10,517,241
    1,b,10,583,86
    1,b,10,582,754
    1,b,10,426,257
    1,b,10,575,229
    1,b,10,697,150
    1,b,10,379,520
    1,b,10,390,286
    1,b,10,618,396
    1,b,10,710,143
    1,b,10,383,188
    1,b,10,1026,713
    1,b,10,1078,625
    1,b,10,713,521"), header = TRUE, sep = ",")
Paul Hiemstra
  • 59,984
  • 12
  • 142
  • 149
  • 1
    The 1st solution can be slightly improved by including the image, as such (will require ggpubr): ggplot(dt, aes(x = world_gazeX, y = world_gazeY)) + background_image(bgimg) + stat_bin2d(bins = 30) – GuillaumeL May 31 '19 at 14:53