What's the difference between ggplot and basic

2020-06-16 03:50发布

问题:

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 ?

回答1:

The base plotting paradigm is "ink on paper" whereas the lattice and ggplot paradigms are basically writing a program that uses the grid-package to accomplish the low-level output to the target graphics devices. The ggplot-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 the ggplot2 package, whereas R starts up with the graphics and grDevices packages already loaded. Both ggplot2 and lattice functions require the use of an explicit print 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 with qplot or ggplot 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. The plotrix package gives a good example of the development of advanced plotting facilities using the base-graphics paradigm.

One major limitation of ggplot2-functions versus base and lattice graphics functions is that ggplot2 does not have any 3D plotting functions. The lattice-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 the gridExtra and latticeExtra packages that extend lattice and ggplot2 capabilities. There is now also a gridBase package that supports saving base plotting results as a grid "grob" and then merging base and grid, i.e. lattice or ggplot, output. It is certainly true that "ggplot"-paradigm seems to be the target of more sustained activity in recent years.