Question

I am having a strange situation when I am trying to plot a heatmap on a dataset that I have which can be found here.

I am using the following code to plot the heat map:

xaxis<-c('density')
midrange<-range(red[,xaxis])
xaxis <- c(xaxis,'quality')
molten<-melt(red[,xaxis],'quality')

p <- ggplot(molten, aes(x = value, y = quality))
p <- p + geom_tile(aes(fill = value), colour = "white")
p <- p + theme_minimal()

# turn y-axis text 90 degrees (optional, saves space)
p <- p + theme(axis.text.y = element_text(angle = 90, hjust = 0.5))

# remove axis titles, tick marks, and grid
p <- p + theme(axis.title = element_blank())
p <- p + theme(axis.ticks = element_blank())
p <- p + theme(panel.grid = element_blank())

p <- p + scale_y_discrete(expand = c(0, 0)) 
# optionally remove row labels (not useful depending on molten)
p <- p + theme(axis.text.x = element_blank()) 

# get diverging color scale from colorbrewer
# #008837 is green, #7b3294 is purple
palette <- c("#008837", "#b7f7f4", "#b7f7f4", "#7b3294")


if(midrange[1] == midrange[2]) {
  # use a 3 color gradient instead
  p <- p + scale_fill_gradient2(low = palette[1], mid = palette[2], high = palette[4], midpoint = midrange[1]) +
    xlim(midrange[1],midrange[2])
}else{
  # use a 4 color gradient (with a swath of white in the middle)
  p <- p + scale_fill_gradientn(colours = palette, values = c(0, midrange[1], midrange[2], 1)) +
    xlim(midrange[1],midrange[2])
}
p

I am trying to plot the heat map on the variable Density and would like to use the variable quality as separation in my heat map. When I use the above code, I get the following plot:

enter image description here

It can be clearly seen that it is a blank image. This is happening because the range of the variable Density is very low, it doesn't happen if I change the variable to the one having a wider range (pH for example). Should ggplot automatically adjust to this? If not, how can I get ggplot to show the real plot?

Any help in this regard will be much appreciated.

Was it helpful?

Solution

So there are (at least) two problems here.

First, you have almost 1600 tiles in the x-direction, so specifying color="white" for the outline means that all you see is the outline, hence, white. Try taking this out.

Second, in your values=c(...) argument to scale_fill_gradientn(...) you seem to expect the midrange[1] and midrange[2] to be between (0,1), but midrange[2] = 1.003.

After taking out color="white" from the call to geom_tile(...), I get this:

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top