Skip to content

stat_binhex hexagons appear extremely stretched if all data points fall in a single bin on y dimension #4463

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ail24 opened this issue May 4, 2021 · 5 comments
Labels
bug an unexpected problem or unintended behavior

Comments

@ail24
Copy link

ail24 commented May 4, 2021

If all data points are in a single bin on the y axis (e.g. if they are all equal to 0) the hexagons appear extremely stretched vertically.

Minimal reprex:

data <- tibble(x = rnorm(1000, 20, 20), y = rep(0,1000))

data %>%
  ggplot(aes(x = x, y = y))+
  stat_binhex(bins = 100)+
  scale_x_continuous(limits = c(-100,100))+
  scale_y_continuous(limits = c(-100,100))

plot2

@smouksassi
Copy link

for stat_binhex the provided limits will be used for computing the ybns

ybnds <- hex_bounds(y, binwidth[2])

what output do you expect? you might want stat count where you are simply counting how many y at each unique x position

@clauswilke
Copy link
Member

Something is definitely wrong when the variation in the data is less than the bin width in that direction. Even if binwidth is provided explicitly the plot fails.

library(tidyverse)

set.seed(1234)
data <- tibble(
  x = rnorm(1000, 20, 20),
  y1 = rnorm(1000, 0, 2),
  y2 = rnorm(1000, 0, .2)
)

data %>%
  ggplot(aes(x = x, y = y1)) +
  stat_binhex() +
  scale_x_continuous(limits = c(-100, 100)) +
  scale_y_continuous(limits = c(-100, 100))

data %>%
  ggplot(aes(x = x, y = y2)) +
  stat_binhex() +
  scale_x_continuous(limits = c(-100, 100)) +
  scale_y_continuous(limits = c(-100, 100))

data %>%
  ggplot(aes(x = x, y = y1)) +
  stat_binhex(binwidth = c(5, 5)) +
  scale_x_continuous(limits = c(-100, 100)) +
  scale_y_continuous(limits = c(-100, 100))

data %>%
  ggplot(aes(x = x, y = y2)) +
  stat_binhex(binwidth = c(5, 5)) +
  scale_x_continuous(limits = c(-100, 100)) +
  scale_y_continuous(limits = c(-100, 100))

# without limits

data %>%
  ggplot(aes(x = x, y = y2)) +
  stat_binhex()

data %>%
  ggplot(aes(x = x, y = y2)) +
  stat_binhex(binwidth = c(5, 5))

Created on 2021-05-05 by the reprex package (v1.0.0)

@clauswilke clauswilke added the bug an unexpected problem or unintended behavior label May 5, 2021
@clauswilke
Copy link
Member

My previous post wasn't entirely correct. Manually setting the bin height does work when limits aren't set. I just had picked a poor value in my previous example.

library(tidyverse)

set.seed(1234)
data <- tibble(
  x = rnorm(1000, 20, 20),
  y1 = rnorm(1000, 0, 2),
  y2 = rnorm(1000, 0, .2)
)

data %>%
  ggplot(aes(x = x, y = y2)) +
  stat_binhex(binwidth = c(5, 0.08))

Created on 2021-05-05 by the reprex package (v1.0.0)

@thomasp85
Copy link
Member

Potentially related #4580 ?

@thomasp85
Copy link
Member

Fixed by #4671

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug an unexpected problem or unintended behavior
Projects
None yet
Development

No branches or pull requests

4 participants