I really enjoyed Hadley Wickham's talk for the Bay Area UseR group last week. I've really been getting into ggplot2 lately, but it was Hadley's example of plotting housing sales data for the San Francisco area really made it click for me. When you first start using ggplot2, the syntax can seem a little, well, arcane: rather than using separate commands to build up your plot as with traditional R graphics, you string elements together with the '+' operator. The example Hadley showed, plotting time-series data, laying on a line, and then panelling the data by city, really made sense of the "grammar of graphics" concept for me.
It looks like the folks at Biogeeks have had a similar epiphany recently:
Say you want to make 12 dose-response plots of various compounds tested in various cell lines. With basic R this would require writing a for-loop and fidling around a lot with axis and plot labelling and the par()-function to make them fit on one page. With basic R you would have be extremely careful to make the code general and reusable for next time when you have different compounds and different cell lines. Enter ggplot2 and the grammar of graphics. ggplot2 is a package for implementing the grammar of graphics, which allows you to write extremely succinct and natural languages like code that produces stunning visualizations.
Here's 6 lines of code in ggplot2, and the graph it creates:
So how does the grammar of graphics help, here? I liked the way the Biogeeks summed it up: "If you compare the code and the plot you will realize that the code contains about the words that you would use if you were told to briefly describe the plot using English." Indeed!p = qplot(Concentration, Percent.of.control, data=screening_data, geom=c("point", "smooth"), colour=Response.type) + scale_x_log10() + facet_grid(Compound ~ Cell.line) + coord_cartesian(ylim=c(-10, 110)) print(p)
Biogeeks: Power plotting with ggplot2
A very nice example, and a beautiful figure. One question - what about lattice? Conditioning has long been a part of lattice, no for-loops required. With the new lattice book just out, I'm beginning to question how complete the overlap between ggplot and lattice is, and whether I'll be switching graphics systems *again*, or if the ywo packages are synergistic.
Posted by: Helmingstay | September 22, 2009 at 12:02
I personally still like lattice. Tons of flexibility - I make lots of non-traditional plots that I wouldn't want to begin to describe with the grammar of graphics. Some would probably say that if this is the case, these must not be good plots, but I know what I'm doing.
Don't get me wrong - I think ggplot is a nice piece of software but I don't see it as a replacement for lattice -- just another way to do things. I hope all the hype ggplot is getting doesn't send lattice into oblivion. One problem I have with it is that it's pitched as being superior to lattice instead of an alternative - "taking all the good and leaving the bad"...
The arguments made against lattice don't really add up. One of these is that "lattice lacks a formal model, which can make it hard to extend" - actually, a formal model can be constraining. Also, "lattice uses a formula-based interface... formula does not generalise well to more complicated situations" - unless I'm missing something, ggplot plots are always of the form qplot(x, y, ...) + faceting, etc. How is this any different from y ~ x | var? Any "complicated" stuff can be handled with a panel function, or in ggplot, by adding more elements. In my experience, lattice deals very well with complicated situations.
Posted by: anon | September 23, 2009 at 07:42
Lattice is more mature. At the moment, it is considerably faster (although HW has said that he hopes to work on this). It has some capabilities that ggplot does not (3d plots, control of aspect ratio/"banking", ...) It is probably easier to customize -- I find the guts of ggplot pretty hard to comprehend.
Besides its shiny newness, the thing I like most about ggplot is that one can generally go a lot farther with the built-in functionality before starting to write custom panel functions (as in lattice). I especially like the ability to combine different data sets/data sets and model predictions on a single plot, which I have always found difficult in lattice.
Posted by: Ben Bolker | September 23, 2009 at 09:15