geom_tile单色为0,然后是色阶 [英] geom_tile single color as 0, then color scale

查看:318
本文介绍了geom_tile单色为0,然后是色阶的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想生成一张热图,其中的颜色托盘为绿色至红色,但值为0时为白色。我开始使用
但是我希望等于零的值为白色。因此,这部分内容会在这里出现:

  ggplot(df,aes(x = Var1,y = Var2,fill = z)) + 
geom_tile()+
scale_fill_gradient(low =green,high =red,limits = c(1,10))



绿色变红:

  ggplot(df,aes(x = Var1,y = Var2,fill = z))+ 
geom_tile()+
scale_fill_gradient(low =white,high =red)



我根本无法使用啤酒秤(尽管我认为我很缺乏

  ggplot(df,aes(x = Var1,y = Var2,fill = z) )+ 
geom_tile()+
scale_fill_brewer(Greens)



错误:连续提供给离散量表的值



我应该用0代替NA吗?任何帮助,将不胜感激。

解决方案

您可以使用 scale_fill_gradientn()

  ggplot(df,aes(x = Var1,y = Var2,fill = z))+ 
geom_tile()+
scale_fill_gradientn(colors = c(white,green,red),values = c(0,0.1,1))


I want to produce a heat map where with a color pallet of green to red, but values of 0 are in white. I got started with geom_tile heatmap with different high fill colours based on factor and others on SO but can't quite get what I need. For example, with the following database:

df <- data.frame(expand.grid(1:10,1:10))
df$z <- sample(0:10, nrow(df), replace=T)

I can create this plot:

ggplot(df,aes(x = Var1,y = Var2,fill = z)) + 
  geom_tile() + 
  scale_fill_gradient(low = "green", high = "red")

But I want the values equal to zero to be white. So this gets part way there:

ggplot(df,aes(x = Var1,y = Var2,fill = z)) + 
  geom_tile() + 
  scale_fill_gradient(low="green", high="red", limits=c(1, 10))

And this gets 0 as white but I lose the green to red:

ggplot(df,aes(x = Var1,y = Var2,fill = z)) + 
  geom_tile() + 
  scale_fill_gradient(low = "white", high = "red")

And I can't use brewer scales at all (though I think I'm missing something simple based on the error).

ggplot(df,aes(x = Var1,y = Var2,fill = z)) + 
  geom_tile() + 
  scale_fill_brewer("Greens")

Error: Continuous value supplied to discrete scale

Should I just replace 0 with NA? Any help would be appreciated.

解决方案

You can use scale_fill_gradientn():

ggplot(df,aes(x = Var1,y = Var2, fill = z)) + 
  geom_tile() + 
  scale_fill_gradientn(colours = c("white", "green", "red"), values = c(0,0.1,1))

这篇关于geom_tile单色为0,然后是色阶的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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