<fwrap-problem.png><fgrid-OK.png>
I'm pretty sure it's a bug in facet_wrap - I use merge to correctly
line up data in different facets and I forgot to take into account
that it messes with the order of the original data.
Hadley
I'm not entirely sure what you want. The following code would ensure
that you always get a 0 on the x-axis (as well as being a little more
efficient), but your sample data already has zeros.
dp <- ddply(d, .(gr, variable), function(df) {
xs <- unique(c(0, df$value))
data.frame(value = xs, ecdf = ecdf(df$value)(xs))
})
Hadley
In a simpler example with no groups/facets:
d = data.frame(x=2:7)
dd = transform(d, ecdf=ecdf(x)(x))
I'm referring to the difference between this:
qplot(x, ecdf, data=dd, geom='step') + ylim(c(0,1))
and this:
dd2 = rbind( data.frame(x=min(d$x), ecdf=0), dd )
qplot(x, ecdf, data=dd2, geom='step') + ylim(c(0,1))
I would prefer the 2nd, where the steps start from y=0.
From this point of view the previous solution given by David fits better:
resolution = 1000
rng = extendrange(d$x)
xsteps = seq(rng[1], rng[2], length = resolution)
dd3 = data.frame( x = xsteps, ecdf = ecdf(d$x)(xsteps) )
qplot(x, ecdf, data=dd3, geom='step') + ylim(c(0,1))
However, in this case setting resolution very low would distort the
plot, while having it very high might slow down the plot in case of many
variables (groups/facets).
Szilard
Ahhh, ok. In that case I think you want this:
dp <- ddply(d, .(gr, variable), function(df) {
xs <- unique(df$value)
data.frame(value = c(min(xs), xs), ecdf = c(0, ecdf(df$value)(xs)))
})
> resolution = 1000
> rng = extendrange(d$x)
> xsteps = seq(rng[1], rng[2], length = resolution)
> dd3 = data.frame( x = xsteps, ecdf = ecdf(d$x)(xsteps) )
>
> qplot(x, ecdf, data=dd3, geom='step') + ylim(c(0,1))
>
> However, in this case setting resolution very low would distort the
> plot, while having it very high might slow down the plot in case of many
> variables (groups/facets).
Using a grid with an ecdf isn't terribly appealing since you know
where all the changes are going to be.
Hadley
Szilard
hadley wickham wrote:
> > Ahhh, ok. In that case I think you want this:
> >
> > dp <- ddply(d, .(gr, variable), function(df) {
> > xs <- unique(df$value)
> > data.frame(value = c(min(xs), xs), ecdf = c(0, ecdf(df$value)(xs)))
> > })
> >