I have (x,y) data in a text file (data.csv) I would like to make into a heat map.
X Y
-60 -18
60 -62
7 14
-22 -60
59 58
29 22
-58 -18
60 -61
7 14
-
There's a whole CRAN Spatial view with plenty of goodies, including 2d kernels. This is an example with splancs
package.
data(bodmin)
plot(bodmin$poly,asp=1,type="n")
image(kernel2d(as.points(bodmin),bodmin$poly, h0=2,nx=100,ny=100),
add=TRUE, col=terrain.colors(20))
pointmap(as.points(bodmin),add=TRUE)
polymap(bodmin$poly,add=TRUE)
bodmin.xy<-coordinates(bodmin[1:2])
apply(bodmin$poly,2,range)
grd1<-GridTopology(cellcentre.offset=c(-5.2,-11.5),cellsize=c(0.2,0.2),cells.dim=c(100,100))
k100<-spkernel2d(bodmin.xy,bodmin$poly,h0=1,grd1)
k150<-spkernel2d(bodmin.xy,bodmin$poly,h0=1.5,grd1)
k200<-spkernel2d(bodmin.xy,bodmin$poly,h0=2,grd1)
k250<-spkernel2d(bodmin.xy,bodmin$poly,h0=2.5,grd1)
if(.sp_lt_0.9()){
df<-AttributeList(list(k100=k100,k150=k150,k200=k200,k250=k250))
} else{
df<-data.frame(k100=k100,k150=k150,k200=k200,k250=k250)
}
kernels<-SpatialGridDataFrame(grd1,data=df)
spplot(kernels,checkEmptyRC=FALSE,col.regions=terrain.colors(16),cuts=15)
Here's one more example:
kern.obj <- structure(c(-161.913250909479, 154.013482116162, 31.6474639061300,
17.7340639366637, -102.170823111156, 17.6809699563749, 90.505728795223,
143.854796792441, -70.1806511117134, 230.600354761065, 133.500211485414,
-225.74140063979, 220.599384351733, -55.5956512970632, 128.631103577179,
-36.9382693513206, 86.1151116370548, -67.9572171234925, 138.313636950703,
59.4122360493993, -128.418347257186, 28.4313444162254, -253.438542232118,
-2.62936998134802, 96.6705573949275, 126.350347596454, -76.3053490233138,
-98.1667749493097, -132.615954657406, -239.003804126569, -32.052834858324,
152.055005227299, -171.132473363859, -96.0272921226682, -91.4859761718545,
172.662664785850, 92.3258005260648, -9.33884441249779, -24.4260189034222,
-171.435971200881, 84.9052731383744, -171.768339197942, -13.5871193263486,
-51.839925496188, -193.00283491136, 57.1126055897217, -40.890549093622,
83.600134171797, 6.66515671609591, -261.487889322599, 138.624659821426,
158.911075756538, 111.598989561161, 62.6150728399137, -155.366548557697,
95.9501552130317, -32.0820888905296, -85.4929337702259, -178.010310820340,
100.526315864149, -190.431234842843, 223.959168312304, -10.693030515916,
-155.820490522984, 87.7527496146106, 293.991051801515, -69.1568338969259,
77.0440461941863, -137.088789092018, -284.434533670747, -52.9437134391306,
129.855822783810, 147.208098412254, -144.394565933009, 11.1193096498363,
-26.6883210946328, 36.3402764034715, -27.5111672678245, 161.017920279498,
133.961438546933, -139.924061267615, -194.861248844460, -138.902485043792,
-59.6746738747854, -193.856125217724, 58.9319665388044, -151.870347293954,
185.500357832384, 77.8198201646078, 217.406148533358, 125.978806993972,
-96.8970637852723, 85.2079461295587, -71.5845844358825, 90.0263897196243,
-3.85398693321446, -233.945188963933, -252.371240484100, -152.282817449886,
-175.448833834566, 74.8285138048232, 218.884530197829, -65.9526397939771,
113.776709279045, -69.4176647812128, -196.919950610027, 268.779812799767,
119.294722331688, 272.239590017125, -161.720151454210, -16.8415614869446,
-13.6117741931230, -96.0124779492617, 157.184316962957, 188.061125110835,
-214.437550725415, 121.667246008292, 89.747676299885, -4.44232751615345,
-106.699166027829, -261.718519963324, -42.1719799283892, -78.4863225650042,
204.811030067503, 265.774235548452, 38.5583057999611, -239.476124290377,
231.875250348821, 135.243163537234, -42.7497774828225, -59.7301519475877,
-2.99901310354471, -240.498538082466, -109.713196987286, 172.524304641411,
113.648047484457, -221.150079695508, 131.948393024504, 62.1528406161815,
-8.31053741276264, -76.1619768105447, 157.933613704517, -42.225355328992,
208.729289704934, 10.0781018380076, 98.7709498498589, -74.8700814787298,
-215.313404565677, -87.6694556325674, -139.495075587183, -28.3679623156786,
-76.2799751479179, -138.629644783214, -164.171522296965, 16.3864661939442,
-109.221789333969, -49.0070185158402, -23.0688956100494, 54.3438952881843,
-145.427243504673, -18.4494345914572, 14.391646720469, -200.727640092373,
187.278914311901, -75.3078812733293, 4.16369824670255, -191.299003595486,
169.710802193731, -103.791763912886, 32.9403738956898, -91.6615933645517,
-222.505887318403, 49.3231621105224, -151.363900210708, -23.9421324804425,
-207.101033208892, 169.309269497171, -250.131661305204, 11.1456824932247,
-193.683278560638, -66.6569401044399, -139.672750141472, -115.024601574987,
-198.41345124878, -205.971520487219, 104.227339709178, 162.442225730047,
-167.216443363577, -100.033209286630, 152.823372976854, -191.260906308889,
-234.539421927184, 213.049413822591, 130.761165590957, -234.716210095212,
6.07512393034995, -49.286244995892, -56.5862323623151, -50.971424812451,
-168.812829069793), .Dim = c(100L, 2L), .Dimnames = list(NULL,
c("x", "y")))
circpol <- structure(c(37.674311717588, 75.1999401385825, 112.428788751435,
149.213932298913, 185.410196624968, 220.874731610807, 255.467574939044,
289.052204461029, 321.496076987398, 352.671151375484, 382.454393849214,
410.728263557213, 437.381176452847, 462.307945665474, 485.410196624968,
506.596755301209, 525.784008026318, 542.896231479612, 557.865891532951,
570.633909777092, 581.149896677179, 589.372350437213, 595.268820788687,
598.816037056963, 600, 598.816037056963, 595.268820788687, 589.372350437213,
581.149896677179, 570.633909777092, 557.865891532951, 542.896231479612,
525.784008026318, 506.596755301209, 485.410196624968, 462.307945665474,
437.381176452847, 410.728263557213, 382.454393849214, 352.671151375484,
321.496076987398, 289.052204461029, 255.467574939043, 220.874731610807,
185.410196624968, 149.213932298913, 112.428788751435, 75.1999401385824,
37.6743117175879, -1.92977144680695e-13, -37.674311717588, -75.1999401385826,
-112.428788751435, -149.213932298913, -185.410196624969, -220.874731610807,
-255.467574939044, -289.052204461029, -321.496076987398, -352.671151375484,
-382.454393849214, -410.728263557213, -437.381176452847, -462.307945665474,
-485.410196624968, -506.596755301209, -525.784008026318, -542.896231479612,
-557.865891532951, -570.633909777092, -581.149896677179, -589.372350437213,
-595.268820788687, -598.816037056963, -600, -598.816037056963,
-595.268820788687, -589.372350437213, -581.149896677179, -570.633909777092,
-557.865891532951, -542.896231479612, -525.784008026318, -506.596755301209,
-485.410196624968, -462.307945665473, -437.381176452847, -410.728263557213,
-382.454393849214, -352.671151375484, -321.496076987398, -289.052204461029,
-255.467574939043, -220.874731610807, -185.410196624968, -149.213932298913,
-112.428788751435, -75.1999401385823, -37.6743117175880, -1.46952762458685e-13,
37.674311717588, 598.816037056963, 595.268820788687, 589.372350437213,
581.149896677179, 570.633909777092, 557.865891532951, 542.896231479612,
525.784008026318, 506.596755301209, 485.410196624968, 462.307945665473,
437.381176452847, 410.728263557213, 382.454393849214, 352.671151375484,
321.496076987398, 289.052204461029, 255.467574939044, 220.874731610807,
185.410196624968, 149.213932298913, 112.428788751435, 75.1999401385825,
37.674311717588, -9.64885723403475e-14, -37.6743117175880, -75.1999401385826,
-112.428788751435, -149.213932298913, -185.410196624969, -220.874731610807,
-255.467574939044, -289.052204461029, -321.496076987398, -352.671151375484,
-382.454393849214, -410.728263557213, -437.381176452847, -462.307945665474,
-485.410196624968, -506.596755301209, -525.784008026318, -542.896231479612,
-557.865891532951, -570.633909777092, -581.149896677179, -589.372350437213,
-595.268820788687, -598.816037056963, -600, -598.816037056963,
-595.268820788687, -589.372350437213, -581.149896677179, -570.633909777092,
-557.865891532951, -542.896231479612, -525.784008026318, -506.596755301209,
-485.410196624968, -462.307945665473, -437.381176452847, -410.728263557213,
-382.454393849214, -352.671151375484, -321.496076987398, -289.052204461029,
-255.467574939043, -220.874731610807, -185.410196624969, -149.213932298913,
-112.428788751435, -75.1999401385822, -37.6743117175879, -1.10214571844014e-13,
37.6743117175882, 75.1999401385825, 112.428788751435, 149.213932298913,
185.410196624968, 220.874731610807, 255.467574939044, 289.052204461029,
321.496076987398, 352.671151375484, 382.454393849214, 410.728263557213,
437.381176452847, 462.307945665474, 485.410196624969, 506.596755301209,
525.784008026318, 542.896231479612, 557.865891532951, 570.633909777092,
581.149896677179, 589.372350437213, 595.268820788687, 598.816037056963,
600, 598.816037056963), .Dim = c(101L, 2L), .Dimnames = list(
NULL, c("x", "y")))
grd <- GridTopology(cellcentre.offset = c(-600, -600), cellsize = c(1, 1), cells.dim = c(1200, 1200))
obj <- kernel2d(pts = kern.obj, poly = circpol, h0 = 100, nx = 600, ny = 600, kernel='quartic')
plot(kern.obj[, "x"], kern.obj[, "y"], xlim = c(-600, 600), ylim = c(-600, 600))
image(obj, add = TRUE, col = terrain.colors(20))
If you are looking at a density plot, where the color represents distribution of the points in the plane, you can use, for example, the kde2d
function from the MASS
library followed by filled.contour
.
A reproducible example:
d <- structure(list(X = c(-60L, 60L, 7L, -22L, 59L, 29L, -58L, 60L,
7L, -21L, 61L, 29L, -57L, -22L, 59L, 29L, -56L, 61L, 8L, -20L,
62L, 30L), Y = c(-18L, -62L, 14L, -60L, 58L, 22L, -18L, -61L,
14L, -59L, 59L, 22L, -18L, -59L, 60L, 24L, -17L, -60L, 16L, -58L,
60L, 23L)), .Names = c("X", "Y"), class = "data.frame", row.names = c(NA,
-22L))
require(MASS)
dens <- kde2d(d$X, d$Y, h=75, n=50) #overrode default bandwidth
filled.contour(dens)
There are lots of other functions that will make you a plot given the density.