[R] [ggplot2] Densityplot, grouping and NAs

Weiss, Bernd bernd.weiss at uni-koeln.de
Wed Mar 25 07:36:11 CET 2009


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




More information about the R-help mailing list