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 ?
We use the function ggplot() to produce the plots when using the package. Therefore, ggplot() is the command, and the whole package is called ggplot2. It is a part of the R tidyverse, an ecosystem of packages designed with common APIs. It is the most widely used alternative to base R graphics.
ggplot2 is a plotting package that provides helpful commands to create complex plots from data in a data frame. It provides a more programmatic interface for specifying what variables to plot, how they are displayed, and general visual properties.
The function qplot() [in ggplot2] is very similar to the basic plot() function from the R base package. It can be used to create and combine easily different types of plots. However, it remains less flexible than the function ggplot(). This chapter provides a brief introduction to qplot(), which stands for quick plot.
ggplot. ggplot2 [library(ggplot2)] ) is a plotting library for R developed by Hadley Wickham, based on Leland Wilkinson's landmark book The Grammar of Graphics ["gg" stands for Grammar of Graphics].
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. (See ?Devices
) The author of the grid
package has written an excellent book, "R Graphics". (See help(pac=grid)
) The ggplot
-paradigm has the "Grammar of Graphics" inspiration and tries to integrate a variety of different plotting functions into one logically 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. See ?ggplot2::`+.gg`
(For many years ggplot2's help pages would refer you to ?layer
but when you got there the was basically empty. They fixed that ... after about a decade.)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With