1

I have a dataframe that looks as follows:

X = c(6,6.2,6.4,6.6,6.8,5.6,5.8,6,6.2,6.4,6.6,6.8,7,7.2,7.4,7.6,7.8,8,2.8,3,3.2,3.4,3.6,3.8,4,4.2,4.4,4.6,4.8,5)
Y = c(2.2,2.2,2.2,2.2,2.2,2.6,2.6,2.6,2.6,2.6,2.6,2.6,2.6,2.6,2.6,2.6,2.6,2.6,2.8,2.8,2.8,2.8,2.8,2.8,2.8,2.8,2.8,2.8,2.8,2.8)
Value = c(0,0.00683254,0,0.007595654,0.015517884,0,0,0,0,0,0,0,0,0,0.005219395,0,0,0,0,0,0,0,0,0,0,0,0.002892342,0,0.002758141,0)
table = data.frame(X, Y, Value)

I have put together a heatmap in R, based on the following command:

  ggplot(data = table, mapping = aes(x = X, y = Y)) + 
    geom_tile(aes(fill = Value), colour = 'black') + 
    theme_void() + 
    scale_fill_gradient2(low = "white", high = "black") + xlab(label = "X") + ylab(label = "Y") 

Since there is not a value for every X and Y, it leads to plots that appear as follows.

enter image description here

I am attempting to smoothen the plot and have the following question:

As there are small white spaces between the plotted values, how could one color these white spaces to be the median intensity? Said differently, how would I first create an initial layer with non-zero median 'Value' before plotting the non-zero 'Value' on top (overlayed)?

A sample is shown below, which has been 'smoothed', which looks closer to the desired output. enter image description here

user2657817
  • 652
  • 2
  • 9
  • 18
  • 1
    Can you provide a reproducible example of your dataset ? (see: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) Right now, it is hard to answer all of your questions without testing it. Can you clarify your question 2 ? Can you show a drawing of the expected output ? – dc37 Mar 02 '20 at 04:51
  • Good points dc37. I have tried to clarify the post to address your questions. – user2657817 Mar 02 '20 at 05:17
  • 1
    @user2657817 A (mock-up) drawing of your expected output would help a lot. I'm not sure what you're looking for. You mention the terms "smoothen" and "interpolate"; it's not clear to me what you want to smoothen/interpolate. Smoothen how? Interpolate what based on what? – Maurits Evers Mar 02 '20 at 05:27
  • Clarified the questions and added a mockup – user2657817 Mar 02 '20 at 05:40
  • another possible approach is to use geom_rect() and set all four corners such that the complete heat map is filled. but as others have said its not completely clear what you want it to look like. – George Savva Mar 02 '20 at 09:19

1 Answers1

1

I'm not sure if it will totally fit your need but from my understanding you have some missing values and combination of X and Y.

So, you can use complete function from tidyr to get all different combinations of X and Y (those without values will be filled with NA) and then by using na.value argument in scale_fill_gradient2 function, you can set the values of these NA values to the same color of the midpoint value:

library(tidyr)
library(dplyr)
library(ggplot2)
table %>% complete(X,Y) %>%
  ggplot(aes(x = X, y = Y))+
  geom_raster(aes(fill = Value), interpolate = TRUE)+
  scale_fill_gradient2(low = "white", mid = "grey",high = "black",
                       na.value = "grey")

enter image description here Does it answer your question ?

dc37
  • 15,840
  • 4
  • 15
  • 32