I am recently exploring R and I am trying to do a geom_tile plot in ggplot where rather than set up the scale of high to low between all range of values, a comparison is made at each level of a variable for 4 factor levels.
An example of the data is:
data <- read.table(text = "
cam k1 k2 k3
n1 342232.6 112964 56589.85
n2 159472.8 54713.9 29480.88
n3 102048.4 38358.95 23376.48
n4 75924.33 32455.58 22504.05
", sep = "", header = TRUE)
I have written this simple code to get a colour map that uses the entire range of values.
library(reshape2)
library(ggplot2)
datam <- melt(data)
p <- ggplot(datam, aes(cam,variable))
(p + geom_tile(aes(fill=value), colour = "white") +
scale_fill_gradient(low="green",high="red"))
However, I would like to get a gradient fill scale that compares the range of values (i.e. 4 values) between factors cam but within each level of k. Basically to highlight the lowest value at each level of k.*
I have many scenarios to plot, so a facet for each level of k is not an option.
Any suggestions would be highly appreciated. Thanks