我想制作一个热图,其中颜色托盘为绿色到红色,但0的值为白色.我开始使用基于因子的不同高填充颜色的geom_tile热图,以及其他SO,但是不能完全得到我需要的东西.例如,使用以下数据库:
df <- data.frame(expand.grid(1:10,1:10))
df$z <- sample(0:10, nrow(df), replace=T)
Run Code Online (Sandbox Code Playgroud)
我可以创建这个情节:
ggplot(df,aes(x = Var1,y = Var2,fill = z)) +
geom_tile() +
scale_fill_gradient(low = "green", high = "red")
Run Code Online (Sandbox Code Playgroud)
ggplot(df,aes(x = Var1,y = Var2,fill = z)) +
geom_tile() +
scale_fill_gradient(low="green", high="red", limits=c(1, 10))
Run Code Online (Sandbox Code Playgroud)
而白色为0,但我失去了绿色到红色:
ggplot(df,aes(x = Var1,y = Var2,fill = z)) +
geom_tile() +
scale_fill_gradient(low = "white", high = "red")
Run Code Online (Sandbox Code Playgroud)
而且我根本不能使用brewer scale(尽管我认为我错过了一些基于错误的简单).
ggplot(df,aes(x = Var1,y = Var2,fill = z)) +
geom_tile() +
scale_fill_brewer("Greens")
Run Code Online (Sandbox Code Playgroud)
我应该用NA替换0吗?任何帮助,将不胜感激.