Graphics Reference
In-Depth Information
Notice that we've used + to join together the pieces. In this last example, we ended a line with + ,
then added more on the next line. If you are going to have multiple lines, you have to put the +
at the end of each line, instead of at the beginning of the next line. Otherwise, R's parser won't
know that there's more stuff coming; it'll think you've finished the expression and evaluate it.
Printing
In R's base graphics, the graphing functions tell R to draw graphs 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 graphs 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 any-
thing, yet it's made these graphs!” 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 runs print() with the
returned result of 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 func-
tion.
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 slightly different way of specifying certain graphing
parameters. I think it's simpler and easier to just use ggplot() .
Stats
Sometimes your data must be transformed or summarized before it is mapped to an aesthetic.
This is true, for example, with a histogram, where the samples are grouped into bins and
counted. The counts for each bin are then used to specify the height of a bar. Some geoms, like
geom_histogram() , automatically do this for you, but sometimes you'll want to do this your-
self, using various stat_ xx functions.
Themes
Some aspects of a graph's appearance fall outside the scope of the grammar of graphics. These
include the color of the background and grid lines in the graphing area, the fonts used in the axis
Search WWH ::




Custom Search