Creating a plot layer by layer with the ggplot function
We have already seen that qplot()
is a simple function that can be used to generate plots quickly, but for more detailed control of the plot layers, the ggplot2
package also provides a more sophisticated function, ggplot()
. Using this function, we first create a plot object in which we define the data and the aesthetic mapping we are interested in representing, and afterwards, we add elements such as geom
and stat that produce the actual plot. Just remember that aesthetic mapping not only includes the colors and sizes of plotting elements, but also the x-y mapping to the axis. In the previous chapter, we showed an example of creating a scatterplot with the ToothGrowth
dataset, where it described the effect of vitamin C on tooth growth in guinea pigs. We will now use the same dataset as an example and discuss how you can build the plot in Figure 2.14 of Chapter 2, Getting Started by adding one layer on top of the other.
First of all, we...