This is a test version of Biostars. For the public version, visit https://www.biostars.org.
heatmap with ggplot: problem when indicating min and max value

Hi,

I am trying to create a heatmap by using ggplot but when I try to indicate the min and max values of my data, R automatically modifes the values.

I show you what I have done:

My data:

original data Showing 1 to 14 of 22 entries

library(ggplot2)
library(reshape2)
## transform  data
heatmap_data_2 = melt(heatmap_data)

this is heatmap_data_2 data after transforming

##scale colors
heatmap_data_3 <- expand.grid(ID = heatmap_data_2$ID, variable = heatmap_data_3$variable)
heatmap_data_3$value <- runif(nrow(heatmap_data_3), min = 0, max = 6)

the problem now is that r modifies my data: this is now data when indicating min and max

I am sure it is an stupid thing but I don't know what the problem is.

Any help would be more than welcome :)

ggplot heatmap scale_fill_gradient

Similar problem: Problem in running R-code

Because you are generating random data - runif.

I think you will need merge, something like res <- merge(heatmap_data_2, heatmap_data_3, by = c("ID", "variable")) or use dplyr::complete function.

1 answer

If you want to assign colors to fixed min/max values, you shouldn't actually change the values you're plotting. Instead, you can use scale_colour_gradientn to manually define the color range, this post might be helpful.

In brief, this is what the general syntax looks like:

## example! have not actually tested this!
yourPlot + scale_colour_gradientn(colours = c("red","white","darkblue"),
                         values = c(min(heatmap_data_2$value), median(heatmap_data_2$value), max(heatmap_data_2$value)) 

Log in to answer this question.