Reputation: 6528
Using ggplot2:
qplot(carat, price, data = diamonds) + facet_grid(cut ~ color ~ clarity)
Not quite what I hoped. How could something like this be done, except producing separate grids of plots per level of clarity, e.g.
qplot(carat, price, data = diamonds[diamonds$clarity=="SI2", ]) + facet_grid(cut ~ color)
qplot(carat, price, data = diamonds[diamonds$clarity=="VS1", ]) + facet_grid(cut ~ color)
and so on.
Something using cast would be perfect.
Upvotes: 0
Views: 214
Reputation: 121157
For three facetting variables, try facet_wrap
instead.
facet_wrap(~ cut + color + clarity)
I've reread the question. If you actually want multiple plots (it wasn't that clear from the phrasing), then just loop over the levels of clarity
.
for(clarity in levels(diamonds$clarity))
{
p <- qplot(carat, price, data = diamonds[diamonds$clarity == clarity, ]) +
facet_grid(cut ~ color)
print(p)
}
Or, if you are for
-loop-phobic,
l_ply(
levels(diamonds$clarity),
function(clarity)
{
qplot(carat, price, data = diamonds[diamonds$clarity == clarity, ]) +
facet_grid(cut ~ color)
}
)
If you are printing to the screen, turn on history recording first. Otherwise include a call to ggsave
in your loop.
Upvotes: 2
Reputation: 77116
Here's what I would do:
base = qplot(carat, price, data = diamonds) + facet_grid(cut ~ color)
lp = dlply(diamonds, "clarity", `%+%`, e1 = base)
library(gridExtra)
do.call(grid.arrange, lp) # all in one page
# or multiple pages (with layout passed to grid.arrange)
all = do.call(marrangeGrob, c(lp, ncol=2, nrow=1))
ggsave("multipage.pdf", all, width=12)
Upvotes: 1