I'm trying to overlay two histograms using transparency to enable viewing of multiple distributions on a single scale. So far ggplot2 seems to do what I want. However I'm having a problem generating the legend coloring appropriate to each distribution in the plot. Here is a test case to show my best (failed) effort so far: library(ggplot2) x <- data.frame(X=rnorm(1000, mean=0)) y <- data.frame(Y=rnorm(1000, mean=3)) xy <- cbind(x, y) g <- ggplot(xy) g + geom_histogram(aes(x=X), colour="black", binwidth = 0.1, fill alpha("red", .5)) + geom_histogram(aes(x=Y), colour="black", binwidth = 0.1, fill alpha("blue", .5)) + scale_fill_manual("Case", values = alpha(c("red","blue"), 0.5), limits=c("A", "B")) + opts(title = "A & B distributions") + xlab("Value") The plot is just what I want and looks gorgeous on my screen. However the legend boxes labeled "A" and "B" respectively have no color and I have tried many variations to get legend color, to no avail. Based on what I could discern from the manual I thought this scale_ command might work: scale_fill_manual("Case", c("A" = alpha("red", 0.5), "B"=alpha("blue",0.5))) + However in this case I get no legend at all. I've spent hours on this and now I'm "dazed and confused". Hope someone can help me "through this land of confusion" .... (OK, some old song lyrics popped into my head ... ) Thanks Chris EDIT: forgot to add I'm using R2.9.0 and the latest ggplot2 (how do I find the version number of ggplot2?) on Windows Vista 64 -- View this message in context: http://www.nabble.com/ggplot2-legend-problem-tp25036665p25036665.html Sent from the R help mailing list archive at Nabble.com.
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)) + geom_histogram(binwidth=0.1) 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 overlapping? -- 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.
Trawling the online and pdf manuals for density plots further I found a reference to position and tried it with histograms. It worked! So here is an example that gives me overlapping histograms, alpha transparency so they can both be seen in the area of overlap, and with properly labelled and colored legend. require(ggplot2) 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)) + geom_histogram(alpha=0.5, binwidth=0.1, position="identity") hth -- View this message in context: http://www.nabble.com/ggplot2-legend-problem-tp25036665p25037667.html Sent from the R help mailing list archive at Nabble.com.