Skip to content
Prev 299405 / 398503 Next

How to compare stacked histograms/datasets

Hi,

Probably easier to work with the raw data, but whatever.  If your data
is in a data frame, dat,

## create row index
dat$x <- 1:21

## load packages
require(ggplot2)
require(reshape2)

## melt the data frame to be long, long dat, ldat for short
ldat <- melt(dat, id.vars="x")

## plot the distributions
ggplot(ldat, aes(x, value, colour = variable)) + geom_line()

## they don't really look on the same scale
## we could scale the data first to have equal mean and variance
dat2 <- as.data.frame(scale(dat))
## remake index so it is not scaled
dat2$x <- 1:21

ldat2 <- melt(dat2, id.vars="x")
ggplot(ldat2, aes(x, value, colour = variable)) + geom_line()

which yields the attached PDF (maybe scrubbed on the official list as
most file extensions are, but should go through to you personally via
gmail).  I'm not sure it's the greatest approach ever, but it gives
you a sense if they go up and down together or at different points.

Cheers,

Josh
On Fri, Jul 6, 2012 at 1:55 PM, Atulkakrana <atulkakrana at gmail.com> wrote: