[R] overlaying frequency histograms or density plots in R

Rui Barradas ru|pb@rr@d@@ @end|ng |rom @@po@pt
Thu Feb 25 11:59:05 CET 2021


Hello,

First of all, I believe you want argument fill, not colour. In ggplot2 
colour is about the border and fill about the interior.

As for the question,

1. Create a basic plot with the common aesthetics.


library(ggplot2)

pp_ALL <- iris[c(1, 5)]
names(pp_ALL) <- c("VALUE", "EXP")

p <- ggplot(data = pp_ALL, mapping = aes(x = VALUE, fill = EXP))



2. geom_density should use alpha transparency, since the densities 
overlap. colour = NA removes the densities black border.


p + geom_density(alpha = 0.5, colour = NA)


3. y = ..density.. plots relative frequencies histograms, for the 
default absolute frequencies or counts, comment the mapping out.

position = position_dodge() allows for extra goodies, such as to change 
the space between bars, their width or to keep empty spaces when some 
factor levels are missing (preserve = "single").

For the test data, with 50 elements per factor level, use a much smaller 
number of bins.

Package scales has functions to display labels in percent format, there 
is no need to multiply by 100.


p + geom_histogram(
   mapping = aes(y = ..density..),
   position = position_dodge(),
   bins = 10)

p + geom_histogram(
   mapping = aes(y = ..density..),
   position = position_dodge(),
   bins = 10) +
   scale_y_continuous(labels = scales::label_percent())


Hope this helps,

Rui Barradas


Às 07:43 de 25/02/21, Bogdan Tanasa escreveu:
> Thanks a lot Petr !
> 
> shall i uses "dodge" also for the RELATIVE FREQUENCY HISTOGRAMS :
> 
> p <- ggplot(iris, aes(x=Sepal.Length, y=..count../sum(..count..)*100,
> colour=Species))
> p+geom_histogram(position="dodge")
> 
> or is there any other way to display the RELATIVE FREQUENCY HISTOGRAMS ?
> 
> thanks again !
> 
> On Wed, Feb 24, 2021 at 11:00 PM PIKAL Petr <petr.pikal using precheza.cz> wrote:
> 
>> 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 using r-project.org> On Behalf Of Bogdan Tanasa
>>> Sent: Wednesday, February 24, 2021 11:07 PM
>>> To: r-help <r-help using 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 using 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.
>>
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help using 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.
>



More information about the R-help mailing list