Skip to content

position_dodge2 with NA values causes errors #2905

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
espher1987 opened this issue Sep 20, 2018 · 3 comments · Fixed by #4408
Closed

position_dodge2 with NA values causes errors #2905

espher1987 opened this issue Sep 20, 2018 · 3 comments · Fixed by #4408
Labels
bug an unexpected problem or unintended behavior positions 🥇
Milestone

Comments

@espher1987
Copy link

I have one problem with geom_boxplot works. First I am using date type data and try not to plot all my data, only a part.

First I load my packages

library(dplyr)
library(ggplot2)
library(tibble)
library(quantmod)

I am using getSymbols to get my data

yahoo <- getSymbols(auto.assign = F,Symbols = "QQQ")
chartSeries(yahoo,subset = "last 3 month")

I shape my data to use it in ggplot

df <- yahoo %>%
  as.data.frame() %>% 
  rownames_to_column() %>% 
  rename("time" = "rowname") %>% 
  mutate(time = as.Date(time))

I set my date limits

l <- c(df$time[10],df$time[100])

Then I try to plot it

ggplot(data = df) +
  geom_boxplot(aes(x = time,
                   lower  = QQQ.Close,
                   upper  = QQQ.Open,
                   ymin   = QQQ.Low,
                   ymax   = QQQ.High,
                   middle = QQQ.Open),
               stat = "identity") +
  scale_x_date(limits = l)

and I get error:

Error in if (df$xmin[i] >= df$xmax[i - 1]) { :
valor ausente donde TRUE/FALSE es necesario


i get this error using ggplot2 3.0.0. I try this code in ggplot2 2.2.1 and i don't get any error. As far i search this can be related to "position_dodge2".

@batpigandme
Copy link
Contributor

Could you please turn this into a reprex (short for minimal reproducible example)? It's especially helpful for ggplot2, since it automatically generates and uploads the plots (or lack thereof), which makes it much easier to go through at a glance.

If you've never heard of a reprex before, you might want to start by reading the tidyverse.org help page.

Thanks

@batpigandme
Copy link
Contributor

If you're looking for a workaround, your code works if you filter on dates before plotting, and don't set limits in scale_x_date()

library(dplyr)
library(ggplot2)
library(tibble)
library(quantmod)
df <- yahoo %>%
  as.data.frame() %>% 
  rownames_to_column() %>% 
  rename("time" = "rowname") %>% 
  mutate(time = as.Date(time))

l <- c(df$time[10],df$time[100])

df_filtered <- df %>%
  filter(time >= l[1]) %>%
  filter(time <= l[2])

ggplot(data = df_filtered) +
  geom_boxplot(aes(x = time,
                   lower  = QQQ.Close,
                   upper  = QQQ.Open,
                   ymin   = QQQ.Low,
                   ymax   = QQQ.High,
                   middle = QQQ.Open),
               stat = "identity") +
  scale_x_date()

Created on 2018-09-25 by the reprex package (v0.2.1.9000)

@paleolimbot
Copy link
Member

I think this is a potential bug in position_dodge2() when xmin or xmax are NA.

library(ggplot2)
df <- data.frame(
  x_date = as.Date(c("1999-01-01", NA)),
  x_chr = c("char", NA),
  x_num = c(1, NA),
  y = 1
)
p <- ggplot(df, aes(y = y))

# these work
p + aes(xmin = x_date, xmax = x_date) + geom_errorbarh(position = "dodge")
#> Warning: Removed 1 rows containing missing values (geom_errorbarh).

p + aes(xmin = x_chr, xmax = x_chr) + geom_errorbarh(position = "dodge2")

# these fail
p + aes(xmin = x_date, xmax = x_date) + geom_errorbarh(position = "dodge2")
#> Error in if (df$xmin[i] >= df$xmax[i - 1]) {: missing value where TRUE/FALSE needed
p + aes(xmin = x_num, xmax = x_num) + geom_errorbarh(position = "dodge2")
#> Error in if (df$xmin[i] >= df$xmax[i - 1]) {: missing value where TRUE/FALSE needed

Created on 2019-05-24 by the reprex package (v0.2.1)

In the original dataset for this issue there weren't any NAs until the values were censored by scale_x_date() with limits. The error is being generated here:

ggplot2/R/position-dodge2.r

Lines 132 to 143 in 1f6f0cb

find_x_overlaps <- function(df) {
overlaps <- numeric(nrow(df))
overlaps[1] <- counter <- 1
for (i in seq_asc(2, nrow(df))) {
if (df$xmin[i] >= df$xmax[i - 1]) {
counter <- counter + 1
}
overlaps[i] <- counter
}
overlaps
}

@paleolimbot paleolimbot added bug an unexpected problem or unintended behavior positions 🥇 labels May 24, 2019
@paleolimbot paleolimbot changed the title Error in Setting date limits inside geom_boxplot in ggplot2 position_dodge2 with NA values causes errors May 24, 2019
@thomasp85 thomasp85 added this to the ggplot2 3.3.4 milestone Mar 25, 2021
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 positions 🥇
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants