Heatmap in r with geom_raster

1.1k Views Asked by At

I am trying to plot a heat map from data with three variables. I am using ggplot with geom_raster, but doesn't seem to work. I am unable to see what's going wrong.

library(tidyverse)

p <- read.csv("Rheatmaptest.csv", header = TRUE);
p

      xdir   ydir Category.1 Category.2 Category.3 Category.4
1  -10.731 10.153    0.61975     3.2650    0.19025      13.00
2  -21.462  9.847    1.77000     3.2475    0.56325      16.70
3  -32.193  9.847    1.65500     2.9900    0.51325     176.00
4  -42.924 10.000    1.34500     3.1800    0.41350     177.00
5  -16.770 20.000    0.69600     3.4975    0.22150     174.00
6  -33.541 20.000    0.68700     3.4275    0.20250       4.24
7  -50.311 20.000    0.77350     3.1575    0.24250     177.00
8  -67.082 20.000    1.09600     3.5350    0.34600     163.00
9  -18.689 30.000    0.54250     3.5875    0.18100     160.00
10 -37.378 30.000    0.63075     3.7125    0.19300     158.00
11 -56.067 30.000    0.71975     3.5425    0.22225       2.26
12 -74.756 30.000    0.79100     3.3750    0.23000       8.24
13 -20.000 40.000    0.76650     3.7200    0.24375     167.00
14 -40.000 40.000    0.68325     3.5300    0.21350     155.00
15 -60.000 40.000    0.81075     3.3400    0.25325     145.00
16 -80.000 40.000    0.68800     3.6375    0.21350     146.00
17 -19.521 50.000    0.67900     3.7150    0.21700     167.00
18 -39.043 50.000    0.69500     3.7950    0.21225     109.00
19 -58.564 49.847    0.68300     3.5575    0.20700     166.00
20 -78.085 50.000    0.67375     3.5325    0.21975     163.00
21 -17.562 60.000    0.64350     3.7025    0.19475     140.00
22 -35.585 60.000    0.56650     3.5250    0.17775      34.30
23 -54.067 60.000    0.82350     3.7700    0.24525     129.00
24 -72.090 60.000    0.85450     3.6675    0.28225     156.00
25 -15.522 70.000    0.59100     3.3475    0.18875     144.00
26 -31.044 69.847    0.56200     3.7975    0.17250     159.00
27 -46.566 70.000    0.79375     3.5350    0.24975     145.00
28 -62.088 70.000    0.64275     3.6100    0.20375     132.00
29 -11.040 80.000    0.75875     3.7450    0.23925     138.00
30 -22.081 80.000    0.81900     3.3875    0.25975     144.00
31 -33.121 80.000    0.72725     3.5825    0.22175     132.00
32 -44.161 80.000    0.83300     3.5550    0.27000     177.00
33  -4.522 90.000    1.77500     3.1250    0.57200      16.30
34  -9.440 90.000    0.96925     3.7200    0.31000     163.00
35 -13.106 90.000    0.76975     3.6600    0.23800       3.50
36 -18.089 90.000    0.86050     3.6750    0.26650      80.50
ggplot(p, aes(x = xdir, y = ydir)) +
    geom_raster(aes(fill = Category.1), interpolate = TRUE) +
    scale_fill_gradient2(limits=c(0.5,2), low="blue", mid="yellow", high="red", midpoint=1)

I am able to see points when I use geom_point instead of geom_raster. Even with geom_raster, I just see very tiny points at the corresponding locations. Interpolate doesn't seem to work.

Am I missing something?

Graph with geom_raster

1

There are 1 best solutions below

1
On

The implied precision of your data is causing your rasters to be plotted so small they are barely visible.

By reducing your precision, you can at least see your raster plot though it is still probably not very useful. Posting this I see I came to the same solution as @tifu.

db %>%
 ggplot(aes(x = round(xdir/2), y = round(ydir), fill = Category.1)) +
 geom_raster(aes(fill = Category.1)) +
 scale_fill_gradient2(limits=c(0.5,2), low="blue", mid="yellow", high="red", midpoint=1)

enter image description here