Get startedGet started for free

Modifying aesthetics

1. Modifying Aesthetics

Now that we know what aesthetics are and have some idea about choosing them appropriately, let's explore how to modify them.

2. Positions

A common adjustment is the position. Position specifies how ggplot will adjust for overlapping bars or points on a single layer. For example, we have identity, dodge, stack, fill, jitter, jitterdodge, and nudge. Let's take a look.

3. position = "identity" (default)

The most straightforward position is identity, which we've actually already seen. It's the default position for our scatter plots. "Identity" means that the value in the data frame is exactly where the value will be positioned in the plot. This basically means, don't do anything, just put the information where the data says to put the information.

4. position = "identity" (default)

We could have written it explicitly, but it's not necessary. There is an issue with the precision in this data set. Our sepals were measured to the nearest millimeter. So although we only have 150 points, there is too much overplotting to distinguish them. To solve this, we need to add some random noise on both the x and y axes to see regions of high density - which is referred to as "jittering".

5. position = "jitter"

"jitter" can be used as an argument, but each position type can also be accessed as a function. For example,

6. position_jitter()

position jitter can be defined in a function before we call our plot, as shown here. This has two advantages.

7. position_jitter()

Now we can set specific arguments for the position, such as the width, which defines how much random noise should be added, and it allows us to use this parameter throughout our plotting functions so that we can maintain consistency across plots. This is available for all position attributes. We'll explore the other positions in the exercises.

8. Scale functions

Recall that each of the aesthetics is a scale which we mapped data onto, so color is just a scale, like x and y are scales. Appropriately enough, we can access all the scales with the scale underscore functions. The second part of the function defines which scale we want to modify. All the aesthetics we saw earlier have an associated scale function. The third part must match the type of data we are using. Here discrete means we are working with categorical data.

9. Scale functions

That means we have to choose our axis dependent on the type of data we have. Here, we'll consider the continuous x aesthetic and the categorical color aesthetic. Just as an aside, before we move on - don't let the naming conventions confuse you. Categorical variables are also called factors, discrete and qualitative depending on their context and who you're talking to.

10. scale_*_*()

There are many arguments for the scale functions. The first argument is always the name of the scale, after that most common are limits, breaks, expand and labels.

11. The limits argument

limits describe the scale's range.

12. The breaks argument

breaks control the tick mark positions.

13. The expand argument

expand is a numeric vector of length two, giving a multiplicative and additive constant used to expand the range of the scales so that there is a small gap between the data and the axes.

14. The labels argument

and labels adjust the category names.

15. labs()

Note that if we just want to quickly change the axis labels, we can do this with the labs function.

16. Let's try it out!

Alright, now let's try it out in the exercises.