4

I am using R (3.0.1) and ggplot2 (ggplot2_0.9.3.1) with geom_tile and scale_fill_gradient. I am using the 'limits' option in scale_fill_gradient, as shown below. The problem is that I want to get rid of the grey background that appears outside of the specified limits. I want that background to be white just like the rest of the graph due to theme_bw(). I have been unable to figure out how to do it. Thanks.

 pp <- function (n,r=4) {
 x <- seq(-r*pi, r*pi, len=n)
 df <- expand.grid(x=x, y=x)
 df$r <- sqrt(df$x^2 + df$y^2)
 df$z <- cos(df$r^2)*exp(-df$r/6)
 df
}

 p <- ggplot(pp(20), aes(x=x,y=y))
 p + theme_bw() + geom_tile(aes(fill=z)) +
     scale_fill_gradient(low="green", high="red", limits = c(-0.1, 0.1))
4

1 回答 1

7

将参数添加na.value="transparent"scale_fill_gradient

p + theme_bw() + geom_tile(aes(fill=z)) +
     scale_fill_gradient(low="green", high="red",
                         limits=c(-0.1, 0.1), na.value="transparent")

在此处输入图像描述

于 2013-09-08T11:37:47.367 回答