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 will create our basic plot object containing the data and aesthetic mapping. The following code shows this:

myPlot <-ggplot(data=ToothGrowth, aes(x=dose, y=len, col=supp))

As illustrated in this case, aesthetic mapping is provided using the aes() (aesthetics) function within the body of the ggplot() function. If you try to call the object just created, myPlot, you will get an empty window and an error message specifying that there is no actual layer in the plot. This object, in fact, contains the basic information of the plot, but it does not produce any output since it does not yet contain any geom assignment, so there is no actual visualization attribute assigned to the plot. The myPlot object is an R S3 object of the class ggplot consisting of a component named data and other components containing information about the plot. The plot details contained in such objects can be accessed using the summary() function. The following code shows this:

summary(myPlot)

The output will be as follows:

data: len, supp, dose [60x3]
mapping:  x = dose, y = len, color = supp
faceting: facet_null()

As illustrated, in this way we can keep track of exactly which data was used in the plot and how the mapping was realized; this is particularly useful if you save the plot objects in your workspace and then would like to check their content.

So, let's now add a layer to our basic plot object and create a basic plot. As we will see in the Layers in ggplot2 section, a basic plot can be realized using data, aesthetic mapping, and geometry, so in this case, we are only missing this last component. In the ggplot() function, additional layers can be added using the + operator followed by the function defining the layer to be added. So, for a scatterplot with points, we have the following code:

myPlot + geom_point()

Tip

How to separate code in multiple lines

When you use the + operator to add plot elements, you may end up with quite long lines of code which would benefit some organizations. One common way to structure code is by dividing it into multiple lines; just keep in mind that the + operator should be always on the right-hand side of the previous line so you could divide the preceding code into two lines, for instance, in this way:

myPlot +

geom_point()

In this case, we used the object created previously and which contained the plot information, and added the geometry; one other option is to use all the code together:

ggplot(data=ToothGrowth, aes(x=dose, y=len, col=supp)) + geom_point()

With both options, you will obtain the same results. Using assignment to objects can be useful, in particular if you have a long series of plots pointing to the same data. In this case, you would have shorter and cleaner code that recalls a previously created plot object.

Also, in this case, we could create a new object, this time containing the final plot, and again using the summary() function, we can get access to its content. The following code shows this:

myNewPlot <- myPlot +geom_point()
summary(myNewPlot)

The output will be as follows:

data: len, supp, dose [60x3]
mapping:  x = dose, y = len, color = supp
faceting: facet_null()
-----------------------------------
geom_point: na.rm = FALSE
stat_identity:  
position_identity: (width = NULL, height = NULL)

You have seen how all the main details of the plot are accessible, in particular the geom and stat details used.

The layers created can also be overwritten, so, for instance, we can take away the aesthetic mapping of colors on the supp variable. This can be easily done by setting the aesthetic argument col to NULL. The following code shows this:

myNewPlot + geom_point(aes(col=NULL))

As illustrated in the resulting plot represented in Figure 3.3, we overwrote the aesthetic of color assignment, but the legend, which was created with the previous call, is still present since the legend represents the aesthetic scale that was generated in the first layer by calling myNewPlot, which contains the aesthetic assignment. These examples show us how aesthetic mapping applied to a layer only affect that layer, while scales and legends will remain at their default values unless manually modified.

Creating a plot layer by layer with the ggplot function

Figure 3.3: This is an example of color aesthetic mapping overwritten without overwriting the legend argument

In order to take away the legend in this example, we will have to overwrite that component directly with the following code:

myNewPlot + geom_point(aes(col=NULL)) + theme(legend.position="none")

In these latest examples, we have seen something very important concerning ggplot2 and the way the plotting is implemented. In fact, in this graphical package the plots can be saved as R objects, which can then be called to produce the visualization of the graph and stored within the workspace. More importantly, these objects can also be used to produce new plots by updating the layers that compose the original picture. This is a very useful functionality that differentiates ggplot2 from the paradigm implemented in other graphical packages such as graphics or lattice.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset