views:

135

answers:

2

I am just starting out with R, and beginning to start producing charts. I am aware that there are at least three different plotting packages, the standard one, ggplot2 and lattice.

Are these packages complementary, or do they cover much the same ground? If they are complementary, when do I use each? If they cover the same ground, which one should I, as a new R user, put my energy into mastering?

+3  A: 

The three packages are actually three different plotting concepts. The standard plotting device goes fast if you know what you're doing, and is -in my eyes- rather intuitive in the sense that all commands deal with particular elements of a graph (plot, title, axis, labels, legend, ...). These graphs are pretty flexible in layout, and I find them useful for most of the standard statistical plotting and fairly straight-forward 2D graphs.

Lattice is a grid-based plotting device, and allows a lot more flexibility. You can basically plot anything you want, as shown on the R Graph Gallery. Graphs can also be annotated. Lattice is -again, in my eyes- very useful for customizable high-end graphs. I use it mainly when I'm making color maps, 3D-visualizations and combined graphs of different subsets of my data (e.g. in model building, for the effects of the different terms.). See also Using lattice graphics in R.

ggplot2 is actually an attempt to combine the good of both systems : you keep most of the flexibility of lattice, but you work on easily defined graph objects as you do with the standard plotting. The graphical output is in general better looking than the standard settings in the standard package, and with less hassle than with lattice. Personally, I have only little experience with ggplot2, but it looks definitely promising. I still prefer the base graph package, but that has probably more to do with the fact that I'm used to that one. Old monkeys and new tricks and all that...

Basically, I'd say to go with what you feel most comfortable with. I'd definitely learn at least the basics of the standard package, but from there I guess ggplot2 gives you everything you need if you're not going into high-profile graphics. If you really want to make customized complex graphs, getting to know lattice can only help you. But otherwise, the other two provide everything you need, and are fairly straight in use.

Joris Meys
Thanks for the in depth answer. Just one question - what do you mean by `high-profile graphics`?
fmark
Just a term to name the very fancy-looking complex graphs that can be found on the Graph Gallery for example. My choice of words isn't always the best, as english is not my mother tongue.
Joris Meys
Thanks for the clarification.
fmark
+6  A: 

There are 4 plotting systems. There is standard, grid, lattice, and ggplot2. The latter two are higher level systems built on the former two. Each has advantages and disadvantages.

Standard graphics gives you absolute control over plots and is great to make one plot just the way you like it. Lattice was developed to address situations where you want arrays of plots. It is very flexible and can plot most any function over you data and over any variable. If you want an arbitrary function applied to each subject's data and presented as a grid of plots, lattice is your baby. It's built on grid and almost the only way anyone uses the grid package.

The latest one, ggplot2, is both a graphing package and a new philosophy in graphing. It is based on "The Grammar of Graphics" by Wilkinson and attempts to do exactly that, generate a grammar for graphics. One merely has to learn the higher level syntax of terms like geom (what you plot), stat (statistics on the data), facet (individual panels), and you can construct very complex graphs. They generally come out quite lovely, especially for electronic distribution. Unfortunately, fine control of each individual detail is not available. There are certain things you simply cannot adjust. That said, many have come to the sane conclusion that it's a small price to pay for the easy way to describe high quality plots.

Have a look at some of the default and example graphs for ggplot2. If they appeal to you then I'd suggest you start there. If you can, try to learn to do everything through the basic grammar method. I personally think it's a mistake that Hadley has the convenience functions as the main help on the website. It seems to undermine the whole purpose of ggplot2. An abbreviated syntax is presented but an expanded vocabulary.

(I say that but I do most of my plotting in base graphics because I find it fun building every single component of the graphs.)

John
Which details, in particular are you referring to that cannot be controlled with ggplot2? I would agree that there are a lot of "hidden" options, but they are definitely there. (theme_get() , for example)
Brandon Bertelsen
I suppose I could expand on that even further... There's a level at which ggplot is well documented and used at which control is minimal. Then there's another where there's a little more, once you find many of the hidden options... and then there's things you just can't control like irregular faceting or the resolution and sidedness of smoothing functions (what if I want to fill the top red and bottom blue?). I really can't list everything you can't control. Not sure if this is changed but something as simple as label offset from axis *was* impossible.
John
That said, I do think it handles 95%+ of graphs better than people could handle it themselves and that these are relatively esoteric issues for many.
John