heatmap with ggplot: problem when indicating min and max value
1
0
Entering edit mode
5.1 years ago
luzglongoria ▴ 50

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 • 4.0k views
ADD COMMENT
1
Entering edit mode

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.

ADD REPLY
4
Entering edit mode
5.1 years ago

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)) 
ADD COMMENT

Login before adding your answer.

Traffic: 1546 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6