Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
787 views
in Technique[技术] by (71.8m points)

r - ggplot2 make missing value in geom_tile not blank

In the geom_tile() layer in the ggplot2 data visualization package for R, when a cell contains no data it is not drawn. E.g. http://docs.ggplot2.org/current/geom_tile.html and search for "missing value".

I would like to change this behavior to show the minimum value over all the tiles. Is this possible and if so how?

Additional context: when I use

stat_density2d(aes(x=x,y=y, fill=..density..), geom="tile", contour=FALSE)

I would like the regions with no density to look very similar to the regions with very little density. As it is now, if say the color spectrum is from blue to red and the background is white, then there when there is no data in a tile it is white and when there is a single data point in a tile is blue.

Adding a pseudo count to the data seems possible, but how do I know in advance how to distribute the pseudo-counts? and in the case when there are faceting?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If your data is a grid-like data, how about adding another geom_tile() for NA by subset()?

# Generate data
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
}
pp20 <- pp(20)[sample(20*20, size = 200),]

df_grid  <- expand.grid(x = unique(pp20$x), y = unique(pp20$x))
df_merge <- merge(pp20, df_grid, by = c("x", "y"), all = TRUE)

# Missing values
ggplot(df_merge, aes(x = x, y = y)) +
  geom_tile(data = subset(df_merge, !is.na(z)), aes(fill = z)) +
  geom_tile(data = subset(df_merge,  is.na(z)), aes(colour = NA),
    linetype = 0, fill = "pink", alpha = 0.5)

an example


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...