For plotting graphs in R, I usually use basic plot plot()
, barplot()
, boxplot()
... functions from the package graphics
. But ggplot
seemed to be used more often.
What are the main differences between the two types of graphics to take into account when choosing which one to use ?
The base plotting paradigm is "ink on paper" whereas the
lattice
andggplot
paradigms are basically writing a program that uses the grid-package to accomplish the low-level output to the target graphics devices. Theggplot
-paradigm has the "Grammar of Graphics" design which tries to integrate a variety of different plotting functions into one coherent package. It does require loading theggplot2
package, whereas R starts up with thegraphics
andgrDevices
packages already loaded. Bothggplot2
andlattice
functions require the use of an explicitprint
call when they are used inside a function.With
ggplot2
you assign the result of that function to an object name and then further modify it. When it's ready for "publication" you get the output processed and sent to a device with print. "ggplot" graphics often get progressively modified by adding "layers" to a base plot created withqplot
orggplot
through the use of the+.gg
-function.In the case of
base
-graphics there is no R object that holds results. The commands get processed immediately and inscribed on the "paper" of the current device. You then issue further commands to augment the output on that device. Theplotrix
package gives a good example of the development of advanced plotting facilities using thebase
-graphics paradigm.One major limitation of
ggplot2
-functions versusbase
andlattice
graphics functions is that ggplot2 does not have any 3D plotting functions. Thelattice
-package, however, is not being actively maintained, but it seemed fairly mature at the point that active development was stopped and if you find a bug it will probably be fixed. There are both thegridExtra
andlatticeExtra
packages that extend lattice and ggplot2 capabilities. There is now also agridBase
package that supports saving base plotting results as a grid "grob" and then merging base and grid, i.e.lattice
orggplot
, output. It is certainly true that "ggplot"-paradigm seems to be the target of more sustained activity in recent years.