4

I am plotting the following data using geom_tile and geom_textin ggplot2

mydf 

   Var1 Var2        dc1 bin
1     H    G 0.93333333   0
2     G    H 0.06666667   1
3     I    G 0.80000000   0
4     G    I 0.20000000   1
5     J    G 0.33333333   1
6     G    J 0.66666667   0
7     K    G 0.57894737   1
8     G    K 0.42105263   0
9     I    H 0.80000000   0
10    H    I 0.20000000   1
11    J    H 0.25000000   0
12    H    J 0.75000000   1
13    K    H 0.20000000   0
14    H    K 0.80000000   1
15    J    I 0.12500000   0
16    I    J 0.87500000   1
17    K    I 0.32000000   0
18    I    K 0.68000000   1
19    K    J 0.28571429   0
20    J    K 0.71428571   1

I am plotting 'Var1' vs 'Var2', and then using the 'bin' variable as my geom_text. Currently, I have filled each tile based upon scale_fill_gradient using the variable 'dc1'.

### Plotting
ggplot(mydf, aes(Var2, Var1, fill = dc1)) + 
  geom_tile(colour="gray20", size=1.5, family="bold", stat="identity", height=1, width=1) + 
  geom_text(data=mydf, aes(Var2, Var1, label = bin), color="black", size=rel(4.5)) +
  scale_fill_gradient(low = "white", high = "firebrick3", space = "Lab", na.value = "gray20",    
  guide = "colourbar") +
  scale_x_discrete(expand = c(0, 0)) +
  scale_y_discrete(expand = c(0, 0)) +
  xlab("") + 
  ylab("") +
  theme(axis.text.x = element_text(vjust = 1),
        axis.text.y = element_text(hjust = 0.5),
        panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        panel.border = element_rect(fill=NA,color="gray20", size=0.5, linetype="solid"),
        axis.line = element_blank(),
        axis.ticks = element_blank(), 
        axis.text = element_text(color="white", size=rel(1.5)),
        panel.background = element_rect(fill="gray20"),
        plot.background = element_rect(fill="gray20"),
        legend.position = "none"

  ) 

Which gives this:

current plot

What I am trying to do (unsuccessfully) is to make the fill conditional upon the 'bin' variable. If bin==1then I would like to fill according to 'dc1'. If bin==0 then I would like to fill with 'white'.

This would give the following which I have manually created as an example desired plot:

desired plot

I tried messing around with scale_fill_gradient to try and introduce a second fill option, but cannot seem to figure this out. Thanks for any help/pointers.

This is the dput for mydf:

structure(list(Var1 = structure(c(4L, 5L, 3L, 5L, 2L, 5L, 1L, 
5L, 3L, 4L, 2L, 4L, 1L, 4L, 2L, 3L, 1L, 3L, 1L, 2L), .Label = c("K", 
"J", "I", "H", "G"), class = "factor"), Var2 = structure(c(1L, 
2L, 1L, 3L, 1L, 4L, 1L, 5L, 2L, 3L, 2L, 4L, 2L, 5L, 3L, 4L, 3L, 
5L, 4L, 5L), .Label = c("G", "H", "I", "J", "K"), class = "factor"), 
    dc1 = c(0.933333333333333, 0.0666666666666667, 0.8, 0.2, 
    0.333333333333333, 0.666666666666667, 0.578947368421053, 
    0.421052631578947, 0.8, 0.2, 0.25, 0.75, 0.2, 0.8, 0.125, 
    0.875, 0.32, 0.68, 0.285714285714286, 0.714285714285714), 
    bin = c(0, 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 
    1, 0, 1)), .Names = c("Var1", "Var2", "dc1", "bin"), row.names = c(NA, 
-20L), class = "data.frame")
jalapic
  • 13,792
  • 8
  • 57
  • 87
  • @Henrik - whoops sorry. When making the reproducible/generalizable example, I copied over the old dataframe name. I've changed it to mydf, which is the one I'm using in this example – jalapic Nov 11 '14 at 16:44

1 Answers1

6

Perhaps replace fill = dc1 with fill = dc1 * bin? A stripped-down version of your code:

ggplot(data = mydf, aes(x = Var2, y = Var1, fill = dc1 * bin, label = bin)) + 
  geom_tile() + 
  geom_text() +
  scale_fill_gradient(low = "white", high = "firebrick3")

enter image description here

Henrik
  • 65,555
  • 14
  • 143
  • 159