有条件地在ggplot2中应用scale_fill_gradient [英] Applying scale_fill_gradient in ggplot2 conditionally

查看:2360
本文介绍了有条件地在ggplot2中应用scale_fill_gradient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 ggplot2 geom_tile geom_text 绘制以下数据c $ c

$ $ $ $ $

$ b $ Var1 Var2 dc1 bin
1 HG 0.93333333 0
2 GH 0.06666667 1
3 IG 0.80000000 0
4 GI 0.20000000 1
5 JG 0.33333333 1
6 GJ 0.66666667 0
7 KG 0.57894737 1
8 GK 0.42105263 0
9 IH 0.80000000 0
10 HI 0.20000000 1
11 JH 0.25000000 0
12 HJ 0.75000000 1
13 KH 0.20000000 0
14 HK 0.80000000 1
15 JI 0.12500000 0
16 IJ 0.87500000 1
17 KI 0.32000000 0
18 IK 0.68000000 1
19 KJ 0.28571429 0
20 JK 0.71428571 1

我正在绘制'Var1'和'Var2',然后使用'bin'变量作为我的 geom_text 。目前,我已根据 scale_fill_gradient 使用变量'dc1'填充每个图块。

  ###绘制
ggplot(mydf,aes(Var2,Var1,fill = dc1))+
geom_tile(color =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)),

plot.background = element_rect(fill =gray20),
legend.position =none


给出这个:



我试图做的(失败)是为了使'bin'变量满足条件。如果 bin == 1 那么我想根据'dc1'来填充。如果 bin == 0 那么我想填写'white'。



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



我试着用 scale_fill_gradient 尝试引入第二个填充选项,但似乎无法弄清楚。感谢您的帮助/指示。



这是mydf的 dput

 结构(列表(Var1 =结构(c(4L,5L,3L,5L,2L,5L,1L,
5L,3L,4L, 2L,4L,1L,4L,2L,3L,1L,3L,1L,2L)。标签= c(K,
J,I,H,G) ,等级=因子),Var2 =结构(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.6666666666666667,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,$ ),.Names = c(Var1,Var2,dc1,bin),row.names = c(NA,
-20L),class =data.frame)


解决方案

花边 fill = dc1 with fill = dc1 * bin ?精简版的代码:

$ g $ p $ g $ g $ ggplot(data = mydf,aes(x = Var2,y = Var1,fill =
geom_text()+
scale_fill_gradient(low =white,high =firebrick3)


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:

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:

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

解决方案

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

这篇关于有条件地在ggplot2中应用scale_fill_gradient的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆