Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

stat_bin2d with drop=FALSE still drops some 0-count cells #2202

Open
alchenist opened this Issue Jul 12, 2017 · 0 comments

Comments

Projects
None yet
2 participants

alchenist commented Jul 12, 2017 edited

The drop parameter in stat_bin2d does not appear to work as advertised: docs state that "if [doc is] TRUE removes all cells with 0 counts." Given this, my expected behavior when drop = FALSE was something akin to the bottom plot. Instead, drop = FALSE results in the second plot, which does keep 0-count cells in x- or y-bins with some values but omits them in bins with no values.

Is this expected behavior? Or should bin indices that don't appear at all in the data be added back into the output of ulevels applied to bin indices in tapply_df?

library(dplyr)
library(tidyr)
library(ggplot2)

data(iris)

iris %>%
  ggplot(aes(Petal.Length, Petal.Width)) + stat_bin2d(aes(fill=..count..), drop=TRUE)

iris %>%
  ggplot(aes(Petal.Length, Petal.Width)) + stat_bin2d(aes(fill=..count..), drop=FALSE)

iris %>% 
  mutate(
    bin_x = cut(Petal.Length, breaks = 30),
    bin_y = cut(Petal.Width, breaks = 30)
  ) %>% 
  count(bin_x, bin_y) %>% 
  tidyr::complete(bin_x, bin_y, fill = list(n = 0)) %>% 
  ggplot(aes(bin_x, bin_y, fill = n)) + geom_tile()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment