[R] ggplot2 legend problem

Chris Friedl cfriedalek at gmail.com
Wed Aug 19 06:10:44 CEST 2009

Still struggling with this. A further example using a slightly different
organisation of the data. The factors "A" and "B" are included in the
dataframe in an attempt to get ggplot to generate a legend automatically.

x <- data.frame(value=rnorm(5000, mean=0), case="A")
y <- data.frame(value=rnorm(5000, mean=3), case="B")
xy <- rbind(x, y)
ggplot(xy, aes(x=value, fill=case, group=case)) +
ggplot(xy, aes(x=value, fill=case, group=case)) + geom_density(alpha=0.2)

Whilst the legend is generated as expected the histogram and density plots
are different. The density plots overlap each other whereas the histogram
plots stack. I'm trying the get the histogram plots to overlap, and retain
the legend. Is the histogram stacking by design? Can stacking be changed to

View this message in context: http://www.nabble.com/ggplot2-legend-problem-tp25036665p25037500.html
Sent from the R help mailing list archive at Nabble.com.

More information about the R-help mailing list