Skip to content

overlaying frequency histograms or density plots in R

6 messages · Bogdan Tanasa, PIKAL Petr, Rui Barradas

#
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 ...
#
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
LEVELS ;
#
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 at precheza.cz> wrote:

            

  
  
#
Hi.



My understanding is that position dodge places each bar in each histogram 
beside each other and position stack places all respective bars atop each 
other.



Relative frequency is something different.



Cheers

Petr



From: Bogdan Tanasa <tanasa at gmail.com>
Sent: Thursday, February 25, 2021 8:43 AM
To: PIKAL Petr <petr.pikal at precheza.cz>
Cc: r-help <r-help at r-project.org>
Subject: Re: [R] overlaying frequency histograms or density plots in R



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 at precheza.cz
<mailto:petr.pikal at 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
LEVELS ;
#
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:
#
Dear Rui, and Petr,

many many thanks for your time and advice ! 'm still exploring the R code
that you have suggested !
On Thu, Feb 25, 2021 at 2:59 AM Rui Barradas <ruipbarradas at sapo.pt> wrote: