Most of the time when you are using ggplot, you'll probably use qplot to quickly get the graphic you want. If you want to exercise more precise control, you need to learn how to build up the plot piece by piece, as we will do in this example.
This data set was collected by a waiter. For each bill, he recorded the total, the amount of the tip and some information about the party: the number of people, the sex of the bill payer, and whether they were smoking or not. For this example, we're going to try and figure out who gives the best tips.
You can find out more about the data set with ?tips
One way to start is to create a scatterplot of total_bill vs the tip. It's easy to do this using qplot. We assign the result of qplot to a variable so we can modify it.
> p <- qplot(total_bill, tip, data = tips)![]()
If we want to investigate whether this same relationship holds within subsets of the data, we can use facetting to produce the same plot for each subset. We can also add margins so we can see both subsets and the whole dataset in each plot.
> setfacets(p, sex ~ .)> setfacets(p, . ~ smoker)
![]()
There are many ways to enhance the plot to make patterns easier to see. By default, qplot uses the point grob function (ggpoint). A grob function converts the data to some kind of graphic object. If we want to add more types of graphics to the plot, we need to use other grob functions. For example, to add a line with specified slope and intercept we can use the ggabline grob. The next example supplements the plot with lines showing some standard tipping rates.
> ggabline(p, slope = c(0.1, 0.15, 0.2))![]()
Note how we are operating on the plot object with a grob function. This is how you add more graphical objects to a plot. All grob functions start with gg and are singular (point, not points).
We can also supplement the plot with smooths, with ggsmooth. The default smooth is a loess, but for this case we're just going to a straight linear smooth.
> ggsmooth(p, method = lm)![]()
If we want to use both the smooth and the reference lines, we can do so as follows. Note how we assign a different colour to the reference lines
> p2 <- ggabline(ggsmooth(p, method = lm), slope = c(0.1, 0.15,
0.2), colour = "darkgreen")
Because all the information is stored in the plot object, we can perform any operation in any order. For example, it is trivial to take the above plot, and facet it on sex:
> setfacets(p2, sex ~ .)![]()