Search code examples
rggplot2scaleheatmap

How do I adjust the scale of a geom_tile in ggplot2?


I am trying to adjust the colour scale of a geom_tile plot.

A short version of my data (in data.frame format) is:

mydat <-

Sc  K   n   minC
A   2   1   NA
A   2   2   37.453023
A   2   3   23.768316
A   2   4   17.628376
A   3   1   NA
A   3   2   12.693124
A   3   3   8.884226
A   3   4   7.436250
A   10  1   2.128121
A   10  2   2.116539
A   10  3   2.737923
A   10  4   3.509773
A   20  1   1.104592
A   20  2   1.840195
A   20  3   2.717198
A   20  4   3.616501
B   2   1   NA
B   2   2   25.090085
B   2   3   15.924186
B   2   4   11.811022
B   3   1   NA
B   3   2   8.827183
B   3   3   6.179484
B   3   4   5.175331
B   10  1   2.096934
B   10  2   2.064984
B   10  3   2.662373
B   10  4   3.407246
B   20  1   1.096871
B   20  2   1.802418
B   20  3   2.649153
B   20  4   3.517776

My code to prepare the data to plot is the following:

mydat$Sc <- factor(mydat$Sc, levels =c("A", "B"))
mydat$K <- factor(mydat$K, levels =c("2", "3","10","20"))
mydat.m <- melt(pmydat,id.vars=c("Sc","K","n"), measure.vars=c("minC"))

I want to plot with geom_tile the value of minC with K and n as axis and different facets for Sc with the following:

mydat.m.p <- ggplot(mydat.m, aes(x=n, y=K)) 
mydat.m.p + 
  geom_tile(data=mydat.m, aes(fill=value)) +
  scale_fill_gradient(low="palegreen", high="lightcoral") +
  facet_wrap(~ Sc, ncol=2)

This gives me a plot for each Sc factor. However, the colour scale does not reflect want I want to portray, because a few high values making low values all equal.

I want to adjust to a relevant scale in 4 breaks, i.e., 1-2, 2-3, 3-5, >5.

Looking at other questions there was a suggestion to use the cut function and scale fill manual as:

mydat.m$value1 <- cut(mydat.m$value, breaks = c(1:5, Inf), right = FALSE)

Then use the following in geom_tile:

scale_fill_manual(breaks = c("\[1,2)", "\[2, 3)", "\[3, 5)", "\[5, Inf)"),
values = c("darkgreen", "palegreen", "lightcoral", "red"))

However, I am not sure how this can be applied to a data.frame with other factors and in long format.


Solution

  • You're almost there. Simply use cut before melting:

    mydat$minC.cut <- cut(mydat$minC, breaks = c(1:3, 5, Inf), right = FALSE)
    mydat.cut <- melt(mydat, id.vars=c("Sc", "K", "n"), measure.vars=c("minC.cut"))
    

    Now, you don't need to specify breaks since we took care of that already.

    ggplot(mydat.cut, aes(x=n, y=K)) + 
      geom_tile(aes(fill=value)) +
      facet_wrap(~ Sc, ncol=2) +
      scale_fill_manual(values = c("darkgreen", "palegreen", "lightcoral", "red"))
    

    enter image description here