Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Relationship between plotting packages in R

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?

like image 715
fmark Avatar asked Sep 20 '10 09:09

fmark


People also ask

What are the two packages R includes for plotting data?

When R is installed, you get from it two base packages, 'graphics' and 'grid', which produce graphics that are exported with the grDevices package.

Which package is used to plot graphs?

About: Plotly is an open-source R package for creating interactive web-based graphs via the open-source JavaScript graphing library plotly. js. The Plotly's R graphing library helps in creating interactive, publication-quality graphs including line plots, scatter plots, area charts, bar charts, error bars, etc.

What is the difference between plot and Ggplot in R?

Base R plots two vectors as x and y axes and allows modifications to that representation of data whereas ggplot2 derives graphics directly from the dataset. This allows faster fine-tuning of visualizations of data rather than representations of data stitched together in the Base R package1.

What is the purpose of plotting data?

The purpose of plotting scientific data is to visualize variation or show relationships between variables, but not all data sets require a plot. If there are only one or two points, it is easy to examine the numbers directly, and little or nothing is gained by putting them on a graph.


2 Answers

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 your 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 as the primary interface for the help but the book is all about the philosophy and fundamental grammar.

(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.)

like image 122
John Avatar answered Oct 25 '22 17:10

John


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.

like image 22
Joris Meys Avatar answered Oct 25 '22 17:10

Joris Meys