0

The ggplot creates a heatmap based on concentration of the spatial points(lat and lon)

Wherever the points are most present the heat map will be brighter.

I use this code to do that and the image shows the result of the code.

  geom_sf(data = df, size = 1, color = "black", fill = "cyan1") +
  stat_density2d(data = df, aes(x = lon, y = lat, fill = ..density..), geom = 'tile', contour = F, alpha = .5) +
  scale_fill_viridis(option = 'inferno') +
  labs(title = str_c('Line 1\n'
                     ,'Line 2'
  )
  ,subtitle = 'Subtitle'
  ,fill = str_c('Line 1', '\nLine2')
  ) +
  theme(text = element_text(color = "#444444")
        ,plot.title = element_text(size = 22, face = 'bold')
        ,plot.subtitle = element_text(size = 12)
        ,axis.text = element_blank()
        ,axis.title = element_blank()
        ,axis.ticks = element_blank()
  )

The output looks like this

enter image description here

Note there are points(df) and I was wondering if there is a way to assign the corresponding overlay value from the legend to each point?

For example if a point is above the purple color I would expect a value between 20 and high 30's added to a new column in df called "Value" associated to that point.

It would operate in a spatial join sort of way, but i realize that my heatmap in the image is not a spatial object. Is there a way to do this?


structure(list(LATITUDE = c(40.68358, 40.69754, 40.843464, 40.692547, 
40.626457, 40.526894, 40.840775, 40.694035, 40.857365, 40.698807, 
40.71402, 40.815, 40.55079, 40.655903, 40.890076, 40.650402, 
40.79335, 40.72538, 40.75184, 40.649788, 40.686928, 40.712963, 
40.801285, 40.633976, 40.670296, 40.66423, 40.817696, 40.668495, 
40.841087, 40.70955, 40.733376, 40.700356, 40.83801, 40.66584, 
40.761436, 40.74958, 40.73197, 40.76249, 40.668507, 40.638268, 
40.696735, 40.870823, 40.574867, 40.866577, 40.775414, 40.84744, 
40.542908, 40.78468, 40.632416, 40.714207, 40.727913, 40.854485, 
40.698986, 40.841717, 40.861687, 40.691822, 40.856014, 40.83383, 
40.68781, 40.642044, 40.69814, 40.64664, 40.680897, 40.760822, 
40.74608, 40.626293, 40.767967, 40.673634, 40.579212, 40.57365, 
40.73632, 40.619396, 40.820263, 40.601864, 40.810318, 40.666306, 
40.708805, 40.826424, 40.63174, 40.727146, 40.67253, 40.702335, 
40.587894, 40.67922, 40.65047, 40.836555, 40.870056, 40.579372, 
40.805138, 40.85968, 40.605595, 40.819214, 40.827972, 40.66496, 
40.719177, 40.748825, 40.733597, 40.54048, 40.738403, 40.68039, 
40.817627, 40.751446, 40.76161, 40.689648, 40.596977, 40.63864, 
40.565254, 40.655895, 40.68821, 40.71649, 40.876785, 40.86367, 
40.835827, 40.793396, 40.84827, 40.656273, 40.693462, 40.66725, 
40.844105, 40.651707, 40.680496, 40.834415, 40.7357, 40.771038, 
40.69484, 40.785774, 40.733017, 40.709023, 40.692886, 40.620487, 
40.618595, 40.803787, 40.82319, 40.680088, 40.827927, 40.66895, 
40.879055, 40.67043, 40.875874, 40.675037, 40.767582, 40.734352, 
40.63083, 40.63532, 40.714073, 40.702194, 40.764362, 40.69496, 
40.79656, 40.805016, 40.66406, 40.7963, 40.66563, 40.680477, 
40.737785, 40.778606, 40.75868, 40.856045, 40.880257, 40.60677, 
40.695683, 40.667236, 40.8351, 40.633682, 40.698116, 40.84747, 
40.8047, 40.762676, 40.7158, 40.75584, 40.772102, 40.681602, 
40.62677, 40.707493, 40.8252, 40.854115, 40.768875, 40.629707, 
40.72654, 40.634415, 40.66937, 40.89466, 40.669067, 40.681484, 
40.82433, 40.856606, 40.65785, 40.62764, 40.58401, 40.71791, 
40.780437, 40.73973, 40.7952, 40.694794, 40.614063, 40.633152, 
40.612736, 40.70166, 40.80641, 40.762234, 40.863647, 40.576626, 
40.60118, 40.64721, 40.681145, 40.57529, 40.786, 40.601128, 40.827923, 
40.805824, 40.642776, 40.86674, 40.678375, 40.74209, 40.81228, 
40.604195, 40.84383, 40.759163, 40.652927, 40.69097, 40.718864, 
40.683174, 40.749744, 40.738316, 40.839382, 40.66806, 40.74715, 
40.663776, 40.843903, 40.836296, 40.655285, 40.70166, 40.64606, 
40.72119, 40.708363, 40.674004, 40.729176, 40.86832, 40.598515, 
40.695004, 40.72773, 40.704563, 40.66807, 40.66944, 40.684082, 
40.69349, 40.765266, 40.74613, 40.74708, 40.87482, 40.70399, 
40.649788, 40.69507, 40.788673, 40.847897, 40.68896, 40.695377, 
40.880657, 40.828114, 40.781265, 40.848736, 40.65989, 40.748436, 
40.61033, 40.752556, 40.829697, 40.718826, 40.65241, 40.852673, 
40.851555, 40.707928, 40.891876, 40.58947, 40.74668, 40.85814, 
40.708626, 40.73464, 40.62855, 40.65563, 40.687046, 40.70326, 
40.633114, 40.62046, 40.75964, 40.64254, 40.783146, 40.705452, 
40.74425, 40.75348, 40.84307, 40.620914, 40.80889, 40.78847, 
40.712776, 40.75868, 40.74661, 40.835705, 40.688404, 40.781715, 
40.730644, 40.75218, 40.731422, 40.761234, 40.668976, 40.637276, 
40.788685, 40.87356, 40.795006, 40.820095, 40.594334, 40.666306, 
40.673008, 40.583626, 40.874474, 40.633995, 40.772327, 40.704937, 
40.653873, 40.677917, 40.59857, 40.809563, 40.68836, 40.666737, 
40.713173, 40.73006, 40.652317, 40.76122, 40.588722, 40.643456, 
40.865532, 40.67612, 40.620663, 40.72166, 40.733723, 40.745686, 
40.875294, 40.803555, 40.7605, 40.661995, 40.69045, 40.658672, 
40.711227, 40.700485, 40.816555, 40.861862, 40.875793, 40.68657, 
40.654705, 40.637054, 40.6191, 40.734566, 40.714912, 40.74734, 
40.6963, 40.63598, 40.724358, 40.586277, 40.671932, 40.650703, 
40.61378, 40.727375, 40.573204, 40.671604, 40.740276, 40.684, 
40.704494, 40.845642, 40.82681, 40.681168, 40.662476, 40.64739, 
40.687138, 40.865143, 40.866673, 40.72313, 40.674934, 40.708363, 
40.739525, 40.637997, 40.750965, 40.671585, 40.694294, 40.810173, 
40.694748, 40.687103, 40.861744, 40.741074, 40.67875, 40.666943, 
40.6635, 40.827824, 40.575832, 40.730366, 40.640945, 40.784237, 
40.76803, 40.669823, 40.659336, 40.616093, 40.763546), LONGITUDE = c(-73.97617, 
-73.98312, -73.836, -73.990974, -73.918, -74.16728, -73.87246, 
-73.72679, -73.84657, -73.91837, -73.74827, -73.89402, -74.20098, 
-73.89817, -73.819855, -73.89422, -73.97275, -74.00011, -73.90358, 
-73.9622, -73.920815, -73.93647, -73.95394, -74.02211, -73.997604, 
-73.919106, -73.922615, -73.925606, -73.86447, -73.95887, -73.86665, 
-73.95732, -73.87329, -73.75551, -73.76995, -73.86541, -73.78651, 
-73.839584, -73.779625, -73.93187, -73.93481, -73.8721, -74.00069, 
-73.8722, -73.91984, -73.89968, -74.15579, -73.80911, -73.94724, 
-73.92817, -73.873245, -73.854645, -73.91671, -73.94435, -73.82435, 
-73.92223, -73.91213, -73.921234, -73.9237, -73.98124, -73.89111, 
-73.9246, -73.95118, -73.99832, -73.974945, -74.01572, -73.96822, 
-73.89294, -73.976265, -74.11252, -73.85631, -73.969574, -73.92976, 
-74.00232, -73.943634, -73.79171, -73.92577, -73.85868, -73.96793, 
-73.954735, -73.798386, -73.89073, -73.95504, -73.90405, -73.917366, 
-73.94306, -73.83222, -74.16948, -73.945244, -73.90427, -73.98404, 
-73.84662, -73.88707, -73.82226, -73.79223, -73.96984, -73.91062, 
-74.153404, -73.93864, -73.94956, -73.92366, -74.001434, -73.97076, 
-73.9184, -73.97324, -74.02245, -74.1301, -73.898224, -73.96583, 
-73.98484, -73.87446, -73.86741, -73.89068, -73.94043, -73.88312, 
-73.90731, -73.965485, -73.88799, -73.923065, -73.93121, -73.821365, 
-73.92854, -73.90377, -73.83413, -73.98391, -73.97052, -73.8852, 
-73.757835, -73.832184, -74.029305, -73.99847, -73.953896, -73.889496, 
-73.94398, -73.90094, -73.9339, -73.87439, -73.928185, -73.84989, 
-73.930534, -73.9109, -74.00849, -73.90736, -73.95033, -73.95087, 
-73.93587, -73.96162, -73.946236, -73.97226, -73.92109, -73.73846, 
-73.93829, -73.73921, -73.7921, -73.93496, -73.98163, -73.87552, 
-73.90079, -73.843864, -73.759575, -73.741875, -73.770004, -73.8825, 
-73.89868, -73.977325, -73.89134, -73.91243, -73.954346, -73.824486, 
-73.99238, -73.763954, -73.95855, -73.946884, -73.94153, -73.867714, 
-73.89091, -73.94898, -73.90486, -73.71589, -74.08535, -73.89523, 
-73.86137, -73.9878, -73.85049, -73.874374, -73.92841, -73.91648, 
-73.89022, -73.98587, -73.95341, -73.94989, -73.70625, -73.94623, 
-73.98246, -74.01393, -73.97762, -74.01172, -73.961464, -73.94227, 
-73.98987, -73.8918, -73.98478, -73.99098, -74.01531, -73.7923, 
-73.97655, -73.84574, -73.997086, -73.93483, -73.954636, -74.02003, 
-73.92873, -73.9265, -73.984985, -73.90941, -73.97218, -73.886375, 
-73.988396, -73.959335, -73.94833, -73.97483, -73.87389, -73.884315, 
-73.98773, -73.84531, -73.90394, -73.985504, -73.889915, -73.927284, 
-73.87369, -73.93658, -73.961464, -74.01648, -73.761185, -73.87203, 
-73.81881, -73.87898, -73.83317, -73.766464, -73.9525, -73.90674, 
-73.91066, -73.80789, -73.883995, -73.90864, -73.97917, -73.81517, 
-73.83605, -73.98135, -73.877, -73.85589, -73.9622, -73.80114, 
-73.97136, -73.92499, -73.93326, -73.94921, -73.877625, -73.93107, 
-73.97599, -73.93234, -73.90536, -73.984566, -73.95932, -73.92972, 
-73.91313, -73.98424, -73.9264, -73.919106, -73.952446, -73.784294, 
-73.8616, -73.80105, -73.9745, -73.895744, -73.94513, -73.87421, 
-73.952835, -73.92596, -73.792114, -73.86474, -73.94928, -74.07641, 
-73.95817, -73.87652, -73.97833, -73.78149, -73.7334, -73.980896, 
-73.848076, -73.9753, -73.95581, -73.968895, -73.90601, -73.87552, 
-73.86473, -73.88875, -73.93803, -73.823845, -73.97329, -73.85201, 
-73.94641, -73.96389, -73.90668, -73.93177, -73.94386, -73.81853, 
-73.9485, -73.955086, -73.990944, -73.79171, -73.97851, -73.98407, 
-73.90031, -73.98138, -73.94531, -73.94932, -74.008156, -73.93861, 
-73.9689, -73.92923, -73.96444, -73.90224, -73.90076, -73.710754, 
-73.92752, -73.93056, -73.960464, -73.972725, -73.86238, -73.936005, 
-74.1524, -73.888664, -73.72516, -73.97213, -73.9088, -73.91184, 
-73.95699, -73.91959, -73.959435, -73.90019, -73.72826, -73.93637, 
-73.91755, -73.91282, -73.85465, -73.776146, -74.00731, -73.98643, 
-74.159615, -73.72269, -73.94784, -73.88673, -73.97545, -73.915146, 
-73.742516, -73.9862, -73.85165, -73.920586, -74.07225, -73.90313, 
-74.09711, -73.86907, -73.92782, -73.95031, -73.81743, -73.90211, 
-73.85361, -73.92941, -73.768326, -73.92314, -73.75136, -73.87204, 
-73.90896, -73.90614, -73.80186, -73.92401, -73.92512, -74.02136, 
-73.94027, -73.99843, -73.74868, -73.95117, -73.73427, -73.89251, 
-73.911804, -73.7258, -73.794, -73.890144, -73.94276, -73.91934, 
-74.12409, -73.91388, -73.94852, -73.947075, -73.87722, -73.90981, 
-73.92726, -74.14523, -73.88209), COLLISION_ID = c(4407147L, 
4136992L, 4395664L, 4397513L, 4403773L, 4405244L, 4405914L, 4407366L, 
4407778L, 4407461L, 4407407L, 4407900L, 4407760L, 4407746L, 4408143L, 
4407638L, 4407958L, 4407885L, 4407616L, 4408038L, 4408224L, 4407392L, 
4407765L, 4407821L, 4407971L, 4408071L, 4407430L, 4408259L, 4407592L, 
4407674L, 4407708L, 4408396L, 4407152L, 4407862L, 4407636L, 4407792L, 
4407853L, 4408205L, 4407945L, 4408118L, 4408242L, 4407563L, 4408098L, 
4407169L, 4407798L, 4407797L, 4407349L, 4407994L, 4408032L, 4407478L, 
4407924L, 4408315L, 4407892L, 4408280L, 4408403L, 4407753L, 4408003L, 
4407497L, 4408229L, 4407525L, 4407817L, 4407539L, 4408306L, 4407830L, 
4407282L, 4407688L, 4407701L, 4407728L, 4408052L, 4407849L, 4407320L, 
4407291L, 4408200L, 4407649L, 4407802L, 4407345L, 4408356L, 4407245L, 
4408057L, 4408332L, 4407785L, 4407929L, 4407425L, 4408080L, 4408123L, 
4408290L, 4408412L, 4407757L, 4407770L, 4407554L, 4407873L, 4407502L, 
4408044L, 4408165L, 4407544L, 4407277L, 4407834L, 4407338L, 4407397L, 
4408109L, 4407683L, 4407829L, 4407496L, 4407609L, 4407689L, 4407861L, 
4407350L, 4407721L, 4407381L, 4407653L, 4407896L, 4407914L, 4407512L, 
4408427L, 4407532L, 4408086L, 4407856L, 4407729L, 4407789L, 4408129L, 
4407576L, 4408193L, 4407643L, 4407906L, 4407414L, 4407623L, 4407436L, 
4407952L, 4407761L, 4408063L, 4407388L, 4407766L, 4407901L, 4408104L, 
4407486L, 4408264L, 4407678L, 4407355L, 4408155L, 4408271L, 4408380L, 
4407598L, 4407866L, 4407452L, 4407809L, 4407393L, 4407453L, 4408214L, 
4407824L, 4407431L, 4407632L, 4407841L, 4407096L, 4407658L, 4407933L, 
4407976L, 4407714L, 4407373L, 4407568L, 4407707L, 4407692L, 4407865L, 
4407154L, 4407637L, 4407354L, 4407793L, 4407432L, 4407946L, 4407852L, 
4408249L, 4407987L, 4408215L, 4408027L, 4407389L, 4407562L, 4407794L, 
4407779L, 4407997L, 4407408L, 4408313L, 4407747L, 4408402L, 4407702L, 
4407762L, 4407297L, 4407321L, 4408100L, 4407505L, 4408047L, 4407805L, 
4407697L, 4407367L, 4407837L, 4407548L, 4407820L, 4408150L, 4407842L, 
4407673L, 4407278L, 4407459L, 4407514L, 4408367L, 4407313L, 4407396L, 
4407611L, 4408053L, 4407905L, 4407642L, 4407556L, 4407774L, 4407360L, 
4408139L, 4408223L, 4408075L, 4407967L, 4407875L, 4407915L, 4408258L, 
4408033L, 4408308L, 4407869L, 4407732L, 4407602L, 4407959L, 4408397L, 
4408067L, 4408279L, 4407723L, 4408198L, 4407501L, 4408119L, 4408359L, 
4407816L, 4407543L, 4407801L, 4407346L, 4407769L, 4408093L, 4407475L, 
4408113L, 4407833L, 4407923L, 4407424L, 4407741L, 4407891L, 4407647L, 
4407909L, 4407439L, 4407604L, 4407752L, 4407380L, 4408037L, 4408416L, 
4407549L, 4408002L, 4408227L, 4408262L, 4407897L, 4408187L, 4407975L, 
4408164L, 4408085L, 4408250L, 4408028L, 4407580L, 4408302L, 4407400L, 
4408128L, 4408194L, 4408289L, 4407953L, 4408391L, 4408204L, 4407870L, 
4407524L, 4408353L, 4407445L, 4408056L, 4407327L, 4407784L, 4407928L, 
4407454L, 4407372L, 4407444L, 4407756L, 4407620L, 4407412L, 4407633L, 
4407119L, 4407660L, 4408154L, 4407773L, 4407567L, 4407938L, 4407713L, 
4408208L, 4408043L, 4408233L, 4407990L, 4408079L, 4408217L, 4408331L, 
4407698L, 4407825L, 4408122L, 4407838L, 4407948L, 4407843L, 4407515L, 
4407312L, 4407314L, 4407489L, 4407656L, 4407332L, 4407506L, 4408401L, 
4407677L, 4407359L, 4408266L, 4407696L, 4407261L, 4407857L, 4407490L, 
4407788L, 4408379L, 4408103L, 4408076L, 4408049L, 4407527L, 4407836L, 
4408274L, 4407547L, 4407815L, 4408375L, 4407279L, 4407331L, 4407686L, 
4408398L, 4408066L, 4408309L, 4408068L, 4407691L, 4407353L, 4407433L, 
4407679L, 4407566L, 4408026L, 4407651L, 4407296L, 4407826L, 4407368L, 
4407804L, 4407242L, 4407858L, 4407795L, 4407347L, 4408059L, 4407715L, 
4408114L, 4408312L, 4407922L, 4407342L, 4407748L, 4407619L, 4408297L, 
4407763L, 4408005L, 4407504L, 4408231L, 4407049L, 4408106L, 4407542L, 
4407523L, 4407428L, 4408257L, 4407595L, 4407672L, 4407832L, 4407384L, 
4408387L, 4407790L, 4407612L, 4407775L, 4407864L, 4407379L, 4407156L, 
4407634L, 4408389L, 4407724L, 4407942L, 4408197L, 4407847L, 4407403L, 
4408120L, 4407703L, 4407668L, 4407570L, 4408101L, 4407800L, 4407917L
)), row.names = c(NA, 400L), class = "data.frame")

user35131
  • 1,105
  • 6
  • 18
  • Like this? https://slowkow.com/notes/ggplot2-color-by-density/ or this: https://datavizpyr.com/ggpointdensity-plot-a-ggplot-extension-combining-scatter-plot-with-density-plot/ – Jon Spring Nov 17 '21 at 21:11
  • Can't open first link but if its similar to first then I already did that. I was able to create the visual as shown in the picture, but I want to join the results of that image into the dataframe. I guess the proper way of saying is I want to have the image turned into an object, or just immediately merge the object to the points based on the values available in the legend. – user35131 Nov 17 '21 at 21:15
  • 1
    The `ggpointdensity` package demonstrated in the 2nd link sounds like it might do what you want. – Jon Spring Nov 17 '21 at 21:23
  • how do I extract the points with the value with a new column that includes the value in it? – user35131 Nov 17 '21 at 21:40
  • I can't run your code without some example data, could you please make your question reproducible? Otherwise I can just say to try again that first link, or look on SO for "ggplot_build" for examples of extracting mapped data from an existing ggplot object. – Jon Spring Nov 17 '21 at 22:22
  • I added a working dataset. Removed a lot of the columns since I was getting a very large structure. – user35131 Nov 17 '21 at 22:30
  • 1
    I'm afraid it will be difficult to get the plotted colors back. What I would try, is rather rasterize the set of points (see; `terra::rasterize`, `raster::rasterize`) where the raster cell value will correspond to number of points in that cell, or calculate the shortest distance to neighbor and assign it to the dataframe. You can check `FNN` (https://stackoverflow.com/questions/66806374/get-k-nearest-neighbors-based-on-latitude-and-longitude/66806929#66806929) – Grzegorz Sapijaszko Jan 31 '22 at 20:31
  • @GrzegorzSapijaszko dzięki. – user35131 Feb 18 '22 at 19:18

0 Answers0