0

I'm using ggplot to create a facet wrap of histograms + density curves. This is the dataframe I'm plotting (~300 rows):

head(merged)
# tpl strand base score ipdRatio     motif
# 1  24501      0    A    51    3.108 AAGTACTCG
# 2  58809      0    A    69    4.095 GAGTACTAC
# 3  65614      0    A    61    3.341 TAGTACTCA
# 4  78494      0    A    92    4.968 GAGTACTAC
# 5  92127      0    A    23    1.702 AAGTACTTA
# 6 193102      0    A    96    5.255 GAGTACTCG

Here's a link to the csv file so that you can read the dataframe in yourselves: dropbox link

If I try this, with nothing but binwidth specified in geom_histogram:

# Graph Histogram
ggplot(merged, aes(score)) + 
  geom_histogram(binwidth=25) + 
  geom_density() +
  facet_wrap(~ motif,ncol=7) +
  labs(title=paste("MOTIF:",motif_f),x="Methylation Score",y="Frequency")

I get: enter image description here

However if I try to create density plots for each individual plot by adding aes(y=..density..) into geom_histogram like so:

# Graph Histogram
ggplot(merged, aes(score)) + 
  geom_histogram(aes(y=..density..),binwidth=25) + 
  geom_density() +
  facet_wrap(~ motif,ncol=7) +
  labs(title=paste("MOTIF:",motif_f),x="Methylation Score",y="Frequency")

I get an undesired result where the y parameters are stretched out for some plots and cramped for others: enter image description here

Any suggestions on how to overlay density plots on these plots while keeping the same y parameters as they are in the first image?

alki
  • 3,334
  • 5
  • 22
  • 45
  • Not clear what you mean by "the y parameters are stretched out for some plots and cramped for others" – Scransom Aug 11 '15 at 03:19
  • You can see that the y range strangely goes from 0 - 15 in the first image to 0 - 0.04 in the second image. And if you visually compare the plots in the two images, some plots seem vertically stretched out, while others seem vertically condensed – alki Aug 11 '15 at 03:23
  • 4
    density isn't on the same scale as counts because its integral will be (~) 1. – Rorschach Aug 11 '15 at 03:23
  • Do you see a way to fix this issue? – alki Aug 11 '15 at 04:06
  • 1
    What do you expect to fix? Within a density, the area under the curve will be 1. If you plot counts like in the first place, this has nothing todo with densities. Looks similar but is completely different... – drmariod Aug 11 '15 at 05:18
  • So is there no way to overlay one on top of the other while pertaining to the histogram y scale? – alki Aug 11 '15 at 05:22
  • as in a way to multiply the density factor to scale it up? – alki Aug 11 '15 at 05:32
  • 1
    You could convert your counts in percentage and plot the bars with `geom_bar(stat='identity')` together with your density. I guess this would give you the expected result. I would not scale the density to match the counts. – drmariod Aug 11 '15 at 06:40
  • Maybe something like [this](https://stat.ethz.ch/pipermail/r-help/2011-June/280588.html)? – aosmith Aug 11 '15 at 14:35

0 Answers0