Search code examples
rggplot2

Adding normal density plot to histogram in ggplot


This is a followup question from my previous post in Plotting 2 histogram on top and bottom part of a single plot

I have drawn below pair of histograms

library(ggplot2)
set.seed(1)
dat = rbind(data.frame('val' = rnorm(100), 'met' = 'Metric1'), data.frame('val' = rt(100, 2), 'met' = 'Metric12'))

ggplot(dat, aes(x = val, fill = met)) + 
  geom_histogram(data    = dat[dat$met == 'Metric1',], 
                 breaks  = seq(-10, 10, 0.5),
                 mapping = aes(y = after_stat(density)),
                 colour  = "black",
                 alpha   = 0.3) +
  stat_function(fun = dnorm, args = list(mean = 0, sd = 1)) +
  geom_histogram(data    = dat[dat$met == 'Metric12', ],
                 breaks  = seq(-10, 10, 0.5),
                 mapping = aes(y = -after_stat(density)),
                 colour  = "black",
                 alpha   = 0.3)+
  coord_cartesian(xlim = c(-10, 10))

With this I could draw normal density plot for the first histogram, but could not overlay normal density curve for the second histogram.

Any suggestion how can I draw second normal density plot for the second histogram would be great.

Thanks for your time.


Solution

  • ggplot() + 
      geom_histogram(data    = dat[dat$met == 'Metric1',], 
                     breaks  = seq(-10, 10, 0.5),
                     mapping = aes(x = val, y = after_stat(density)),
                     colour  = "black",
                     alpha   = 0.3) +
      geom_histogram(data    = dat[dat$met == 'Metric12', ],
                     breaks  = seq(-10, 10, 0.5),
                     mapping = aes(x = val, y = -after_stat(density)),
                     colour  = "black",
                     alpha   = 0.3)+
      coord_cartesian(xlim = c(-10, 10))+ 
      geom_line(
        aes(
          x = dat[dat$met == 'Metric1',]$val,
          y = dnorm(dat[dat$met == 'Metric1',]$val),
          color = as.factor(1))
      )+
      geom_line(
        aes(
          x = dat[dat$met == 'Metric12',]$val,
          y = -dnorm(dat[dat$met == 'Metric12',]$val),
          color = as.factor(2))
      )