Skip to content
Prev 174831 / 398503 Next

[ggplot2] Densityplot, grouping and NAs

Dear all,

I do not fully understand how ggplot2 handles NAs. See the following 
example:

library(ggplot2)
x <- rnorm(150)
g <- as.factor(c(rep(c(0,1,NA),50)))
mydf <- data.frame(x,g)

m <- ggplot(aes(x = x, group = g, color = g), data = mydf)
m + geom_density()


How do I get rid of the NAs (i.e. the blue colored curve)?

I thought

## m <- ggplot(aes(x = x, group = g, color = g, na.rm = TRUE),
## data = mydf)
## m + geom_density()

or

## m <- ggplot(aes(x = x, group = g, color = g), data = mydf)
## m + geom_density()
## m + stat_density(na.rm = TRUE)

etc.

would solve my problem but to no avail.


Thanks for your help,

Bernd