1

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

Sandy Muspratt
  • 31,719
  • 12
  • 116
  • 122
  • Transform your data (using dplyr or data.table) so it matches what you want to plot, then plot it. – Gregor Thomas May 14 '15 at 16:28
  • 1
    Scale each set of values to be between 0-1 and then just use a single fill scale. – joran May 14 '15 at 16:28
  • [This](http://stackoverflow.com/questions/30040420/heat-map-per-column-with-ggplot2/30040582#30040582) and [this](http://stackoverflow.com/questions/30078074/rescaling-with-plyr-ddply-in-r) might be of help. – eipi10 May 14 '15 at 16:34
  • thanks! previous posts worked..... – Nicolás Gálvez May 15 '15 at 10:03
  • Possible duplicate of [Heat map per column with ggplot2](https://stackoverflow.com/questions/30040420/heat-map-per-column-with-ggplot2) – byouness May 07 '18 at 08:26

0 Answers0