nik
nik

Reputation: 2584

how to plot a binary with a given range of values

I have asked a question here how to make a binary heat map several times on a data frame without getting any feedback which I believe it is not that easy. I tried to break down the problem in order to get some answer

so I consider having only one data in my data structure as follows

df1<- structure(list(time = c(1L, 1L, 1L, 1L, 1L, 1L), level = structure(1:6, .Label = c("B", 
"C", "D", "E", "F", "G"), class = "factor"), X2 = c(266L, 480L, 
396L, 501L, 481L, 542L), X3 = c(356L, 587L, 491L, 320L, 883L, 
422L), X4 = c(452L, 406L, 521L, 300L, 582L, 506L), X5 = c(549L, 
368L, 548L, 528L, 673L, 701L), X6 = c(414L, 398L, 526L, 593L, 
639L, 484L)), .Names = c("time", "level", "X2", "X3", "X4", "X5", 
"X6"), class = "data.frame", row.names = c(NA, -6L))

at first I get the range of it as follows:

min(df1[3:length(df1)])
max(df1[3:length(df1)])

it means that the data is distributed between 266 and 883.

now I want to set the one closer to lower to be red and the one closer to higher value to be yellow

I tried to plot it with reshape2as follows but I could not figure out how to set the values and other stuff

library(reshape2)
melted_cormat <- melt(df1)
ggplot(data = melted_cormat, aes(x=level, y=variable, fill=value))

Upvotes: 1

Views: 132

Answers (1)

Punintended
Punintended

Reputation: 737

Using something similar to your code (note inclusion of "id" variable)...

melted_cormat <- melt(df1, id = c("time", "level"))
ggplot(data = melted_cormat, aes(x=level, y=variable, fill=value)) +
       geom_tile() + scale_fill_gradient(low = "red", high = "yellow")

made this plot... enter image description here

Note that you can substitute hex codes for "red" and "yellow" in scale_fill_gradient, if you want a particular hue or two.

Upvotes: 1

Related Questions