A.4 Printing

In R’s base graphics, the graphing functions tell R to draw plots to the output device (the screen or a file). ggplot2 is a little different. The commands don’t directly draw to the output device. Instead, the functions build plot objects, and the plots aren’t drawn until you use the print() function, as in print(object). You might be thinking, “But wait, I haven’t told R to print anything, yet it’s made these plots!” Well, that’s not exactly true. In R, when you issue a command at the prompt, it really does two things: first it runs the command, then it calls print() with the result returned from that command.

The behavior at the interactive R prompt is different from when you run a script or function. In scripts, commands aren’t automatically printed. The same is true for functions, but with a slight catch: the result of the last command in a function is returned, so if you call the function from the R prompt, the result of that last command will be printed because it’s the result of the function.

Note

Some introductions to ggplot2 make use of a function called qplot(), which is intended as a convenient interface for making graphs. It does require a little less typing than using ggplot() plus a geom, but I’ve found it a bit confusing to use because it has a different way of specifying certain parameters. It’s simpler and to just use ggplot().