overlaying frequency histograms or density plots in R

104 views
Skip to first unread message

Bogdan Tanasa

unread,
Feb 24, 2021, 5:08:20 PM2/24/21
to ggplot2

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) 

and which is the correct way to display the data ? thanks, 

bogdan 

ps : i have emailed to the folks on R-help too ..

Bogdan Tanasa

unread,
Feb 24, 2021, 5:23:38 PM2/24/21
to Ram Siwakoti, ggplot2
Dear Ram, thank you for your quick email. 

here i have attached the figures please : 

<> figure1 : ggplot(pp_ALL, aes(x=VALUE, colour=EXP)) + geom_density()

<> figure2 : ggplot(data=pp_ALL) +
+        geom_histogram(mapping=aes(x=VALUE, y=..density.., colour=EXP),  bins=1000)

in terms of data interpretation, the figure1 makes more sense, i would say. thanks :)

-- bogdan
figure1.pngfigure2.png

On Wed, Feb 24, 2021 at 2:17 PM Ram Siwakoti <rsiwa...@gmail.com> wrote:
Hi Bogdan, 

The first code may not be creating the histogram at all. If you want a histogram with density plot overlayed as you mentioned, the second code should produce the correct plot you are looking for, no?

-- Ram 

--
--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

---
You received this message because you are subscribed to the Google Groups "ggplot2" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ggplot2/CA%2BJEM00CY9vWWzAU%2BoKsq-VsGH_%2B4WU2K1n5MSY_Nyr-LVL7VA%40mail.gmail.com.

Ram Siwakoti

unread,
Feb 24, 2021, 9:01:43 PM2/24/21
to Bogdan Tanasa, ggplot2
Hi Bogdan, 

The first code may not be creating the histogram at all. If you want a histogram with density plot overlayed as you mentioned, the second code should produce the correct plot you are looking for, no?

-- Ram 

On Wed, Feb 24, 2021, 5:08 PM Bogdan Tanasa <tan...@gmail.com> wrote:
--
Reply all
Reply to author
Forward
0 new messages