有条件地在ggplot2中应用scale_fill_gradient

时间:2014-11-11 16:22:38

标签: r ggplot2

我正在geom_tile

中使用geom_textggplot2绘制以下数据
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

我正在绘制'Var1'与'Var2',然后使用'bin'变量作为我的geom_text。目前,我已使用变量“dc1”基于scale_fill_gradient填充每个磁贴。

### 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"

  ) 

这给出了这个:

current plot

我想要做的(失败的)是使填充条件为'bin'变量。如果bin==1,那么我想根据'dc1'填写。如果bin==0那么我想填写'白色'。

这将给出我手动创建的以下示例:

desired plot

我尝试使用scale_fill_gradient来尝试引入第二个填充选项,但似乎无法解决这个问题。感谢您的帮助/指示。

这是mydf的dput

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")

1 个答案:

答案 0 :(得分:5)

或许将fill = dc1替换为fill = dc1 * bin?代码的简化版本:

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