I have matrix data and would like to create heat map using geom_tile(). geom_tile() successfully functioned, but the limit of "value" seemed to be lengthout. (For example, over 100 value is the same color breaks) . I would like to compare the result with other dataset, so I think the same color breaks among graphs is better.
Var1 <- c(0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162)
Var2 <- c(0,0,0,0,0,3,3,3,3,3,31,31,31,31,31,316,316,316,316,316, 3162, 3162, 3162, 3162, 3162)
value <- c(67,79,90,20, 5,10,88,82,11, 1, 2,17, 107,41, 4, 3,15,81,66,12, 1, 3,14,16,23)
longData <- data.frame(cbind(Var1, Var2, value))
ggplot(longData, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
scale_fill_steps(low="grey90", high="red", breaks=c(0, 10, 25, 50, 100, 150, 200, 300)) +
labs(x="2000", y="2007", title="Matrix")+
geom_text(aes(label = value))
-----update-----
I would like to compare these two graphs, but the indicated color is different depending on the value.
Var1 <- c(3, 31,316,3162,3,31,316,3162,3,31, 316,3162,3,31,316, 3162, 31,316,3162)
Var2 <- c(0,0,0,0,3,3,3,3,31,31,31,31,316,316,316,316,3162,3162,3162)
value <- c(36,34,9,4,129,59,12,2,37,277,50,10,3,23,106,22,5,9, 31)
longData2 <- data.frame(cbind(Var1, Var2, value))
ggplot(longData2, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
scale_fill_steps(low="grey90", high="red", breaks=c(0, 10, 25, 50, 100, 150, 200, 300)) +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))
This does seem to be harder than I would have thought. It seems you need to also take care of the rescaler. To keep both the same, you can define the scale once, for example
my_fill <- scale_fill_steps(low="grey90", high="red",
breaks=c(0, 10, 25, 50, 100, 150, 200, 300),
rescale=function(x, ...) scales::rescale(x, from=c(0, 300)),
limits=c(0,300))
And then use that for both plots
ggplot(longData, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
my_fill +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))
ggplot(longData2, aes(x = as.character(Var1), y = as.character(Var2))) +
geom_tile(aes(fill=value)) +
my_fill +
labs(x="2007", y="2012", title="Matrix")+
geom_text(aes(label = value))