1

I created below heatmap

heatmap

for which I used this code:

library(ggplot2)
library(reshape2)

winners_min_expd0 <- matrix(c(NA, 2, 2, 3, NA, 3, 2, 2, NA), nrow=3, ncol=3, byrow = T)
colnames(winners_min_expd0) <- c("homophily","heterophily","preferential")
rownames(winners_min_expd0) <- c("homophily","heterophily","preferential")
melted_min_expd0 <- melt(winners_min_expd0)
colnames(melted_min_expd0) <- c("winning", "looses", "times")
p_min_expd0 <- ggplot(melted_min_expd0, aes(looses, winning)) +
   geom_tile(aes(fill = times), colour = "white") +
   scale_fill_gradient(low = "white", high = "red") +
   ggtitle("Min function, exp decay off") +
   scale_y_discrete(limits=c("preferential", "heterophily", "homophily")) +
   xlab("(x) looses against (y)") + ylab("(y) winning over (x)")
p_min_expd0

I would like to:

  • rescale "times" bar to range <0:5>, I understand there are no {0,1,4,5} values, but I'd like to have this range
  • make the "times" heat scale discrete, which means one solid color for one number, e.g. 5 = hot red, 4 = red, etc. (possible duplicate: ggplot2 heatmap with colors for ranged values), ranges like <0,1) are acceptable, but I prefer single natural numbers on the scale (instead of value ranges)
  • add a thin grid on the heatmap, so all tiles (including white ones) are separated by it
  • change the gray color of NA values to some lighter gray

Help appreciated, as well as shorter code.
Thanks!

Community
  • 1
  • 1
oski86
  • 855
  • 1
  • 13
  • 35

1 Answers1

6

Here are some modifications

p_min_expd0 <- ggplot(melted_min_expd0, aes(looses, winning)) +
   geom_tile(aes(fill = cut(times, breaks=0:5, labels=1:5)), colour = "grey") +
   ggtitle("Min function, exp decay off") +
   scale_y_discrete(limits=c("preferential", "heterophily", "homophily")) +
   scale_fill_manual(drop=FALSE, values=colorRampPalette(c("white","red"))(5), na.value="#EEEEEE", name="Times") + 
   xlab("(x) looses against (y)") + ylab("(y) winning over (x)")

Since you want a discrete scale, you can take care of your first two "wishes" by doing the conversion yourself with cut(). This makes a discrete variable from a continuous one. Since we are using a deiscrete scale now, we must set the color ramp ourselves with the scale_fill_manual. The "grid" color is set with the colour= parameter in geom_tile. The NA color is set with scale_fill_maual(na.value=).

enter image description here

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • I noticed my question was downvoted. Do you think I didn't made enough research, or maybe I used inappropriate style of writing? I'd like to become a better SO user. Thx for suggestions. – oski86 Jul 03 '15 at 14:49
  • 2
    You basically just posted a laundry list of edits to make to your plot and asked someone to do them for you. Each of those tasks probably have an answer already here on SO. It's just to ask just one, clear focused question at a time and show that you made an effort to answer it yourself. but you did do a good job including a reproducible example which is very helpful. – MrFlick Jul 03 '15 at 15:08