Coordinates

1. The Coordinates Layer

The coord layer controls the dimensions of your plot.

2. Coordinates layer

The coord layer is composed of functions named coord_. Let's explore their properties The most commonly used function is coord_cartesian, which controls the x-y Cartesian plane of your plot.

3. Zooming in

We can use coord_cartesian to zoom in on a specific part of a plot. Alternatively, we could set the limits argument in scale_x_continuous or scale_y_continuous, or use an xlim or ylim function directly.

4. Original plot

Consider the following plot of the iris data. We've seen this plot in previous lessons. It plots sepal width against sepal length and draws a loess curve for each of our three iris species.

5. scale_x_continuous()

When we "zoomed in" to a small part of the x-axis using scale_x_continuous, we're presented with some important warning messages. 95 rows have been removed. This happened because the limits we set in scale_x_continuous were a smaller range than the data and thus values were filtered out.

6. scale_x_continuous()

We can see this on the new zoomed-in plot, since the LOESS model is only defined for the points shown, although there is data beyond this region. That's also why the models look different.

7. xlim()

A quick and dirty alternative is to call xlim as a function itself. It has the same effect.

8. coord_cartesian()

Contrast this to really zooming in using the coord_cartesian function. You can see the zoom because the LOESS curve continues past the data presented, and the models look the same as in the original plot. We haven't filtered the data set, so if we did inadvertently cut off data values, it would not be clear simply from looking at the plot -- unless we had some indicator such as a smoothing function. Changing the x and y limits can lead to unexpected consequences and should always be used with caution. Don't recycle or hard code axis limits until you've seen the raw data.

9. Aspect ratio

Aside from zooming in, another common role that you'll achieve with the coordinates layer is changing the aspect ratio. When we say 'aspect ratio', we are referring to the height-to-width aspect ratio. Changing the aspect ratio of a plot is one of the most common ways in which people either inadvertently or purposely deceive -- or are deceived -- with visualizations. There is no systematic method for choosing an appropriate aspect ratio. The only rule to follow in this area is that, typically, we should use a 1:1 aspect ratio when the units of measure are the same, although there are some exceptions to this rule - for example, when the scales are the same, but their ranges differ widely. The aspect ratio is particularly important when it changes our perception or interpretation of the data.

10. Sunspots

In this plot of over 250 years of sun spots data, there are three key trends. First, sun spots follow an 11-year oscillating cycle. Second, sun spot numbers also change over longer periods. However there is a third and subtle patten present in this time series, but it's very difficult to see in this format. The aspect ratio here is 1:1, there are 250 units on the y and 265 units on the x. The physical distance for each unit in the same.

11. Sunspots

However, if we reduce the aspect ratio to something very low, like 0.055, we flatten the entire plot and notice something else. Sunspots arise more quickly than they disappear, a pattern that is more prevalent the higher the peak intensity in a given cycle is. Different trends are emphasized and the aspect ratio depends on what we are investigating or communicating.

12. Practice time!

OK, let's head over to the exercises and look at coordinates in more detail.