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 ...

Get ggplot2 Essentials now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.