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

scale_fill* doesn't see NaN as NA #1482

Closed
cmcoffman opened this Issue Jan 11, 2016 · 1 comment

Comments

Projects
None yet
2 participants
@cmcoffman

cmcoffman commented Jan 11, 2016

Had some weirdness in my plots and eventually tracked it down. the <na.value> parameter in the various scale_fill* scales doesn't recognize <NaN> and instead fills them white.

Example:

# four points, top ones are NA and NaN
df <- data.frame(
  x = c(1, 2, 1, 2),
  y = c(1, 1, 2, 2),
  z = c(1, 2, NA, NaN)
)

# ggplot sees top left as NA and colors red,
# but the NaN on top right colors white (which
# is invisible if the background is white)
ggplot(df, aes(x, y)) +
  geom_point(aes(colour = z), size = 50) +
  scale_colour_gradient(na.value = "red")

image

I don't think this is the desired behavior.

It's also difficult to debug why this is happening because many R functions will put <NaN> where a user might expect <NA>. For example <mean(c(NA, NA), na.rm = TRUE)> returns <NaN>. I ran into this when doing some ddply on a large dataset. The problem was difficult to isolate because <all.equal()> can't always tell <NA> from <NaN>:

all.equal(
  a <- mean(c(NA, 10), na.rm = FALSE),
  b <- mean(c(NA, NA), na.rm = TRUE)
)

returns <TRUE>.

Also, while the View() function in Rgui will show <NaN> the View function in R-Studio will replace it with <NA>. The <write.csv()> function will also convert <NaN> to <NA>.

@hadley

This comment has been minimized.

Member

hadley commented Jan 12, 2016

This is definitely a bug

@lock lock bot locked as resolved and limited conversation to collaborators Jun 19, 2018

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