0

My data consists of three variables: x, y, and intensity. I can create a heat map just fine with the base heatmap or ggplot's geom_tile/geom_raster (shown below). However, my x and y data is very granular (about 2000x2000), so the corresponding heat map looks quite sparse and disconnected. Is there a way to bin the x- and y- variables while retaining the intensity information, or to otherwise decrease the resolution of the heat map without discarding values?

I think I am barking up a tree something like this (How to change interpolation / smoothing in ggplot2 geom_raster), but unfortunately, that question has also gone unanswered.

    ggplot(avgS4) + 
        geom_tile(aes(x = variable, y = V1, fill = value)) +
        scale_fill_viridis(option = "magma", direction = 1, limits = c(58000,63000),
              oob = squish) +
        scale_y_reverse()

Sample data is shown below.

    variable (x) = c(1,1,1,1,1,2,2,2,2,2,3,3,3,3,3,4,4,4,4,4,5,5,5,5,5)
    V1 (y) = c(1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5,1,2,3,4,5)
    value (intensity) = c(3,4,4,5,1,5,5,6,7,3,2,3,2,2,5,5,7,5,4,3,2,3,3,3,3)

Right now, the heat map gives each point an individual tile/square (i.e. (1,1) has an intensity of 3, (1,2) has an intensity of 4...). Is it possible to bin this data so only one square/tile on the heat map represents the four points (1,1), (1,2), (2,1), and (2,2) with the corresponding intensity of 17 (3+4+5+5)?

  • Perhaps you could convert your data to a `raster` object, and then take advantage of the many functionalities of working with `raster` data (https://gis.stackexchange.com/questions/79062/how-to-make-raster-from-irregular-point-data-without-interpolation/79074#79074). You could find the centre of each tile, choose your resolution and select an appropriate interpolation method between tiles (https://www.rspatial.org/raster/analysis/4-interpolation.html). – hugh-allan Aug 24 '21 at 23:55
  • Thanks for the suggestion. I essentially did a similar version of this, using a Gaussian kernel on the raw data to convolve, and then plotting a heatmap per usual. This seemed to work for my purposes, although I am still trying to make sure that this doesn't misleadingly distort my data. – MonkeyArrow Aug 27 '21 at 01:46

0 Answers0