Skip to content
Back to formatted view

Raw Message

Message-ID: <f06ecd145485480db4d7bfbd7e25c193@SRVEXCHCM1302.precheza.cz>
Date: 2021-02-25T07:00:03Z
From: PIKAL Petr
Subject: overlaying frequency histograms or density plots in R
In-Reply-To: <CA+JEM03pOWd8Msmw_FpQWOMnxE3SyfFGsDX87YapezRLrU=6UA@mail.gmail.com>

Hi

You should use position dodge.

p <- ggplot(iris, aes(x=Sepal.Length, colour=Species))
p+geom_density()
p <- ggplot(iris, aes(x=Sepal.Length, y=..density.., colour=Species))
p+geom_histogram(position="dodge")

Cheers
Petr
> -----Original Message-----
> From: R-help <r-help-bounces at r-project.org> On Behalf Of Bogdan Tanasa
> Sent: Wednesday, February 24, 2021 11:07 PM
> To: r-help <r-help at r-project.org>
> Subject: [R] overlaying frequency histograms or density plots in R
> 
> Dear all, we do have a dataframe with a FACTOR called EXP that has 3
LEVELS ;
> 
>  head(pp_ALL)
>     VALUE  EXP
> 1 1639742 DMSO
> 2 1636822 DMSO
> 3 1634202 DMSO
> 
> shall i aim to overlay the relative frequency histograms, or the density
> histograms for the FACTOR LEVELS,
> 
> please would you let me know why the following 2 pieces of R code show
> very different results :
> 
> ggplot(pp_ALL, aes(x=VALUE, colour=EXP)) + geom_density()
> 
> versus
> 
> ggplot(data=pp_ALL) +
>        geom_histogram(mapping=aes(x=VALUE, y=..density.., colour=EXP),
>  bins=1000)
> 
> thanks,
> 
> bogdan
> 
> ps : perhaps i shall email to the folks on ggplot2 mailing list too ...
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.