Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot heatmap failing to fill tiles

Tags:

r

ggplot2

heatmap

This (minimal, self-contained) example is broken:

require(ggplot2) 

min_input = c(1, 1, 1, 2, 2, 2, 4, 4, 4)
input_range = c(4, 470, 1003, 4, 470, 1003, 4, 470, 1003)
density = c(
    1.875000e-01,
    5.598958e-04,
    0.000000e+00,
    1.250000e-02,
    3.841146e-04,
    0.000000e+00,
    1.250000e-02,
    1.855469e-04,
    0.000000e+00)       

df = data.frame(min_input, input_range, density)

pdf(file='problemspace.pdf')
ggplot(df, aes(x=min_input, y=input_range, fill=density)) +
    geom_tile()
dev.off()

Producing:

gapped tiles

Why are there big gaps?

like image 662
Reinderien Avatar asked May 12 '15 06:05

Reinderien


1 Answers

There are gaps because you don't have data for all of the tiles. If you want to try to fill them in, your only option is to interpolate (assuming you don't have access to additional data). In theory, geom_raster() (a close relative of geom_tile()) supports interpolation. However, according to this github issue, that feature is not currently functional.

As a workaround, however, you can use qplot, which is just a wrapper around ggplot:

qplot(min_input, input_range, data=df, geom="raster", fill=density, interpolate=TRUE)

If there is too much space between the points that you have data for, you will still end up with blank spaces in your graph, but this will extend the range that you can estimate values for.

EDIT:

Based on the example that you posted, this will be the output

enter image description here

As you can see, there is a vertical band of white running through the middle, due to the lack of data points between 2 and 4.

like image 165
seaotternerd Avatar answered Sep 30 '22 18:09

seaotternerd