[R] ggplot2 legend problem

Chris Friedl cfriedalek at gmail.com
Thu Aug 20 04:01:11 CEST 2009


Thanks Hadley, Thierry for clearing up my misuse of the command syntax.
Thierry, the more I try the density plot, the more I see that it is right
for my application. Good call.

Be Well and Happy Always

Chris


ONKELINX, Thierry wrote:
> 
> Dear Chris,
> 
> 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.Onkelinx at 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-bounces at r-project.org [mailto:r-help-bounces at r-project.org]
> Namens hadley wickham
> Verzonden: woensdag 19 augustus 2009 14:18
> Aan: Chris Friedl
> CC: r-help at r-project.org
> Onderwerp: Re: [R] ggplot2 legend problem
> 
> On Tue, Aug 18, 2009 at 11:10 PM, Chris Friedl<cfriedalek at gmail.com>
> wrote:
>>
>> 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?
> 
> 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
> 
> --
> http://had.co.nz/
> 
> ______________________________________________
> R-help at 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.
> 
> 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.
> 
> ______________________________________________
> R-help at 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.
> 
> 

-- 
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.




More information about the R-help mailing list