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.
______________________________________________
R-h...@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
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
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
I'm skeptical that this will create a useful plot, but
geom_histogram(binwidth=0.1, position = "identity")
will do what you want. You might also want to look at geom_freqpoly.
Alternatively, to use your previous approach, you just need to make a
couple of small changes:
g + geom_histogram(aes(x=X, fill = "A"), colour="black", binwidth = 0.1) +
geom_histogram(aes(x=Y, fill = "B"), colour="black", binwidth = 0.1) +
scale_fill_manual("Case", c("A" = alpha("red", 0.5), "B"=alpha("blue",0.5)))
Previously you weren't supplying the fill aesthetic so the scale had
nothing to work with.
Hadley
First of all I would go for the density plot. The 'extra' info from the
histogram is just noise. So I guess you are not interessed in it.
ggplot(xy, aes(x=value, colour=case, group=case)) + geom_density()
But is you want to stick with a histogram then I would use one of the
two below
ggplot(xy, aes(x=value, fill=case, group=case)) +
geom_histogram(binwidth=0.1, position = "identity", alpha = 0.2)
ggplot(xy, aes(x=value, fill=case, group=case)) +
geom_histogram(binwidth=0.1, position = "dodge")
HTH,
Thierry
------------------------------------------------------------------------
----
ir. Thierry Onkelinx
Instituut voor natuur- en bosonderzoek / Research Institute for Nature
and Forest
Cel biometrie, methodologie en kwaliteitszorg / Section biometrics,
methodology and quality assurance
Gaverstraat 4
9500 Geraardsbergen
Belgium
tel. + 32 54/436 185
Thierry....@inbo.be
www.inbo.be
To call in the statistician after the experiment is done may be no more
than asking him to perform a post-mortem examination: he may be able to
say what the experiment died of.
~ Sir Ronald Aylmer Fisher
The plural of anecdote is not data.
~ Roger Brinner
The combination of some data and an aching desire for an answer does not
ensure that a reasonable answer can be extracted from a given body of
data.
~ John Tukey
-----Oorspronkelijk bericht-----
Van: r-help-...@r-project.org [mailto:r-help-...@r-project.org]
Namens hadley wickham
Verzonden: woensdag 19 augustus 2009 14:18
Aan: Chris Friedl
CC: r-h...@r-project.org
Onderwerp: Re: [R] ggplot2 legend problem
Hadley
Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer
en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is
door een geldig ondertekend document. The views expressed in this message
and any annex are purely those of the writer and may not be regarded as stating
an official position of INBO, as long as the message is not confirmed by a duly
signed document.
Be Well and Happy Always
Chris
--
View this message in context: http://www.nabble.com/ggplot2-legend-problem-tp25036665p25054892.html
Sent from the R help mailing list archive at Nabble.com.
______________________________________________