Chapter 5. Controlling Plot Details

Although, by now, you have gained a good understanding of how to make plots, you certainly realize that we did not discuss how to change the default details of the plot, which are automatically set up by ggplot2. In this chapter, we will go through how to personalize details such as the plot title and axis, axis scales, plot background, and the legend details. We will then see examples of of layout changes that can be applied to facet plots.

Exploring scales

We already discussed scales in different situations but, in this case, we will focus on certain general aspects that will turn out to be essential for you to understand how to fully utilize the different scales available. Generally speaking, the scales are assigned during the aesthetic mapping and are then used to define guides to trace from the represented objects back to the data by creating legends as well as the plot axis. You can add a scale to your plot or modify the default values of the scale using one of the scale functions. These functions have the general structure of scale_aesthetic_scale, where aesthetic represents the aesthetic on which the scale is applied and scale represents the name of the scale on which it is used. Typical aesthetic values can be, for instance, color, x, y, fill, shape, or size, while examples of scales are hue, brewer, gradient, and log10. So, for instance, the scale_color_gradient() function defines the gradient of colors. Take for example a case where different shades of a certain color are used to map data, as we did in Figure 5.1. Another example is scale_color_hue(), which is used for equally spaced colors, for instance, for mapping categorical values to the color aesthetic, while scale_fill_hue() is the corresponding scale for the fill aesthetic. You also have the option to define your own scale for discrete data using the scale_manual() function. Also, keep in mind that if you want to set one of the aesthetics to a certain value, you need to use the I() operator, so to use only the color red, you can use the code color=I("red") as we already described in Chapter 2, Getting Started.

In this chapter, we will see several examples of scales and their use, but we will not go into all the details as we shall see from our examples that in many cases their usage is almost the same. If you want an overview of the different scales available, you can have a look at them on the ggplot2 website documentation at http://docs.ggplot2.org.

Tip

Grayscale plots for black-and-white publications

In some cases, you may need to produce plots using only black-and-white colors. You can convert your plot to grayscale simply using the scale_color_grey() and scale_fill_grey()functions depending on the aesthetic. For instance, the scatterplot from the previous chapter can be rendered in grayscale with the following command:

myScatter + scale_color_grey()
..................Content has been hidden....................

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