Skip to content
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

geom_miss_point() [but not geom_point()] misaligned with errorbars #237

Open
Aariq opened this issue Jul 18, 2019 · 2 comments
Open

geom_miss_point() [but not geom_point()] misaligned with errorbars #237

Aariq opened this issue Jul 18, 2019 · 2 comments

Comments

@Aariq
Copy link

Aariq commented Jul 18, 2019

This is a weird one that took me a long time to reproduce. I'm making a plot with jittered points with errorbars (geom_errorbar, geom_linerange, and geom_errorbarh all have the following problem). I do the jitter with the position argument so the errobars and points match up. With geom_point(), the errorbars are correctly placed. But with geom_miss_point() the errorbars are misplaced, but only when the axis labels aren't in the same order as in the dataframe (I think??). Here's a reprex:

library(tidyverse)
library(naniar)

df1 <- tibble(y = rnorm(20, mean = 0, sd = 0.1),
              x = rep(c("a", "b", "c", "d", "e"), each = 4))

# different order for x
df2 <- tibble(y = rnorm(20, mean = 0, sd = 0.1),
              x = rep(c("e", "a", "b", "c", "d"), each = 4))

#set up jitter
j <- position_jitter(height = 0, width = 0.2)

# This works:
p <- ggplot(df2, aes(x, y)) +
  geom_point(position = j) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)
p

# misaligned errorbars:
p_miss <- ggplot(df2, aes(x, y)) +
  geom_miss_point(position = j, jitter = 0) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)
p_miss

# but this is fine for some reason:
p_miss2 <- ggplot(df1, aes(x, y)) + 
  geom_miss_point(position = j, jitter = 0) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)
p_miss2

# I think it has to do with factor levels or something because this fixes the second plot.
df2 %>% 
  mutate(x = fct_inorder(x)) %>%
  ggplot(aes(x,y)) + 
  geom_miss_point(position = j, jitter = 0) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)

Created on 2019-07-19 by the reprex package (v0.3.0)

@Aariq
Copy link
Author

Aariq commented Jul 18, 2019

Yeah, I think it has to do with factor levels or something because this fixes the second plot.

df2 %>% 
  mutate(x = fct_inorder(x)) %>%
  ggplot(aes(x,y)) + 
  geom_miss_point(position = j, jitter = 0) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)

@njtierney
Copy link
Owner

Thanks for posting an issue, and for taking the time to work out this tricky behaviour.

This is indeed some strange behaviour! Do you think you could copy/paste my code below into your initial comment? It contains a reprex with the images, which shows us more clearly what is going wrong here:

``` r
library(tidyverse)
library(naniar)

df1 <- tibble(y = rnorm(20, mean = 0, sd = 0.1),
              x = rep(c("a", "b", "c", "d", "e"), each = 4))

# different order for x
df2 <- tibble(y = rnorm(20, mean = 0, sd = 0.1),
              x = rep(c("e", "a", "b", "c", "d"), each = 4))

#set up jitter
j <- position_jitter(height = 0, width = 0.2)

# This works:
p <- ggplot(df2, aes(x, y)) +
  geom_point(position = j) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)
p
```

![](https://i.imgur.com/aTKBcEn.png)

``` r

# misaligned errorbars:
p_miss <- ggplot(df2, aes(x, y)) +
  geom_miss_point(position = j, jitter = 0) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)
p_miss
```

![](https://i.imgur.com/tGz0LZj.png)

``` r

# but this is fine for some reason:
p_miss2 <- ggplot(df1, aes(x, y)) + 
  geom_miss_point(position = j, jitter = 0) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)
p_miss2
```

![](https://i.imgur.com/HIndl8Z.png)

``` r

# I think it has to do with factor levels or something because this fixes the second plot.
df2 %>% 
  mutate(x = fct_inorder(x)) %>%
  ggplot(aes(x,y)) + 
  geom_miss_point(position = j, jitter = 0) +
  geom_linerange(aes(ymin = y - 0.5, ymax = y + 0.5), position = j)
```

![](https://i.imgur.com/itH82np.png)

<sup>Created on 2019-07-19 by the [reprex package](https://reprex.tidyverse.org) (v0.3.0)</sup>

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

No branches or pull requests

2 participants