Multiple histograms in a plot using ggplot2.
ggplot(test.melt, aes(x = value, fill = sample)) + geom_histogram(alpha = 0.3, position = 'identity', aes(y = ..density..)) + geom_density(alpha = 0.3, position = 'identity')
Be careful. The default value for the
position
parameter is different the histogram and density plot. So set the parameter explicitly.
Identity: overlap the two histograms.
stack: stack the two histograms on top of another.
For more options, check Position adjustments in
http://docs.ggplot2.org/current/index.html
Default parameters of histogram and density.
geom_histogram(mapping = NULL, data = NULL, stat = "bin", position = "stack", ...)
stat_density(mapping = NULL, data = NULL, geom = "area", position = "stack", adjust = 1, kernel = "gaussian", trim = FALSE, na.rm = FALSE, ...)
geom_density(mapping = NULL, data = NULL, stat = "density", position = "identity", na.rm = FALSE, ...)
Leave a Reply